Skip to content

Commit

Permalink
feat: bootstrap plugin support
Browse files Browse the repository at this point in the history
  • Loading branch information
TurtIeSocks committed Dec 8, 2023
1 parent d23a2b1 commit 06780a0
Show file tree
Hide file tree
Showing 21 changed files with 268 additions and 54 deletions.
80 changes: 79 additions & 1 deletion .vscode/settings.json
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,85 @@
"editor.formatOnSave": true,
"rust-analyzer.showUnlinkedFileNotification": false,
"files.associations": {
"vector": "cpp"
"vector": "cpp",
"__bit_reference": "cpp",
"__bits": "cpp",
"__config": "cpp",
"__debug": "cpp",
"__errc": "cpp",
"__hash_table": "cpp",
"__locale": "cpp",
"__mutex_base": "cpp",
"__node_handle": "cpp",
"__nullptr": "cpp",
"__split_buffer": "cpp",
"__string": "cpp",
"__threading_support": "cpp",
"__tree": "cpp",
"__tuple": "cpp",
"any": "cpp",
"array": "cpp",
"atomic": "cpp",
"bit": "cpp",
"bitset": "cpp",
"cctype": "cpp",
"chrono": "cpp",
"cinttypes": "cpp",
"clocale": "cpp",
"cmath": "cpp",
"compare": "cpp",
"complex": "cpp",
"concepts": "cpp",
"condition_variable": "cpp",
"csignal": "cpp",
"cstdarg": "cpp",
"cstddef": "cpp",
"cstdint": "cpp",
"cstdio": "cpp",
"cstdlib": "cpp",
"cstring": "cpp",
"ctime": "cpp",
"cwchar": "cpp",
"cwctype": "cpp",
"deque": "cpp",
"exception": "cpp",
"forward_list": "cpp",
"fstream": "cpp",
"future": "cpp",
"initializer_list": "cpp",
"iomanip": "cpp",
"ios": "cpp",
"iosfwd": "cpp",
"iostream": "cpp",
"istream": "cpp",
"limits": "cpp",
"list": "cpp",
"locale": "cpp",
"map": "cpp",
"memory": "cpp",
"mutex": "cpp",
"new": "cpp",
"numeric": "cpp",
"optional": "cpp",
"ostream": "cpp",
"queue": "cpp",
"random": "cpp",
"ratio": "cpp",
"set": "cpp",
"sstream": "cpp",
"stack": "cpp",
"stdexcept": "cpp",
"streambuf": "cpp",
"string": "cpp",
"string_view": "cpp",
"system_error": "cpp",
"tuple": "cpp",
"type_traits": "cpp",
"typeinfo": "cpp",
"unordered_map": "cpp",
"unordered_set": "cpp",
"variant": "cpp",
"algorithm": "cpp"
},
"cmake.configureOnOpen": false
}
1 change: 1 addition & 0 deletions client/src/App.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -84,6 +84,7 @@ export default function App() {
setStatic('dangerous', res.dangerous || false)
setStatic('route_plugins', res.route_plugins || [])
setStatic('clustering_plugins', res.clustering_plugins || [])
setStatic('bootstrap_plugins', res.bootstrap_plugins || false)
if (!res.logged_in) {
router.navigate('/login')
}
Expand Down
1 change: 1 addition & 0 deletions client/src/assets/types.ts
Original file line number Diff line number Diff line change
Expand Up @@ -216,6 +216,7 @@ export interface Config {
dangerous: boolean
route_plugins: string[]
clustering_plugins: string[]
bootstrap_plugins: string[]
}

export type CombinedState = Partial<UsePersist> & Partial<UseStatic>
Expand Down
19 changes: 18 additions & 1 deletion client/src/components/drawer/Routing.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -50,13 +50,23 @@ export default function RoutingTab() {
)
const routePlugins = useStatic((s) => s.route_plugins)
const clusteringPlugins = useStatic((s) => s.clustering_plugins)
const bootstrapPlugins = useStatic((s) => s.bootstrap_plugins)

const sortByOptions = React.useMemo(() => {
return [...SORT_BY, ...routePlugins]
}, [routePlugins])
const clusterOptions = React.useMemo(() => {
return [...CLUSTERING_MODES, ...clusteringPlugins]
}, [clusteringPlugins])
const bootstrapOptions = React.useMemo(() => {
return [...CALC_MODE, ...bootstrapPlugins]
}, [bootstrapPlugins])

React.useEffect(() => {
if (!CALC_MODE.some((x) => x === calculation_mode)) {
usePersist.setState({ calculation_mode: 'Radius' })
}
}, [mode])

const fastest = cluster_mode === 'Fastest'
return (
Expand All @@ -76,11 +86,18 @@ export default function RoutingTab() {
</Collapse>
<MultiOptionList
field="calculation_mode"
buttons={CALC_MODE}
buttons={mode === 'bootstrap' ? bootstrapOptions : CALC_MODE}
label="Strategy"
hideLabel
type="select"
/>
<Collapse
in={
!CALC_MODE.some((x) => x === calculation_mode) && mode === 'bootstrap'
}
>
<UserTextInput field="bootstrap_args" helperText="--x 1 --y abc" />
</Collapse>
<Collapse in={calculation_mode === 'Radius'}>
<UserTextInput field="radius" />
</Collapse>
Expand Down
22 changes: 13 additions & 9 deletions client/src/components/drawer/inputs/MultiOptions.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,13 @@ interface Props<T extends FieldType, K extends UsePersist[T]> {
itemLabel?: (item: K) => string
}

const defaultLabel = (item: string | number) =>
typeof item === 'string'
? item.includes('_')
? fromSnakeCase(item)
: fromCamelCase(item)
: `${item}`

export default function MultiOptions<
T extends FieldType,
K extends UsePersist[T],
Expand All @@ -36,22 +43,17 @@ export default function MultiOptions<
type = 'button',
label = '',
hideLabel = !label,
itemLabel = (item: number | string) =>
typeof item === 'string'
? item.includes('_')
? fromSnakeCase(item)
: fromCamelCase(item)
: `${item}`,
itemLabel = defaultLabel,
}: Props<T, K>) {
const [value, setValue] = usePersist((s) => [s[field], usePersist.setState])
const value = usePersist((s) => s[field])

return type === 'button' ? (
<ToggleButtonGroup
size="small"
color="primary"
value={value}
exclusive
onChange={(_e, v) => setValue({ [field]: v })}
onChange={(_e, v) => usePersist.setState({ [field]: v })}
sx={{ mx: 'auto' }}
disabled={disabled}
>
Expand All @@ -72,7 +74,9 @@ export default function MultiOptions<
label={hideLabel ? undefined : label}
value={value}
color="primary"
onChange={({ target }) => setValue({ [field]: target.value as K })} // Mui y u like this
onChange={({ target }) =>
usePersist.setState({ [field]: target.value as K })
} // Mui y u like this
sx={{ mx: 'auto', minWidth: 150 }}
disabled={disabled}
>
Expand Down
4 changes: 3 additions & 1 deletion client/src/hooks/usePersist.ts
Original file line number Diff line number Diff line change
Expand Up @@ -68,12 +68,13 @@ export interface UsePersist {
save_to_scanner: boolean
skipRendering: boolean
fast: boolean
calculation_mode: typeof CALC_MODE[number]
calculation_mode: typeof CALC_MODE[number] | string
s2_level: typeof S2_CELL_LEVELS[number]
s2_size: typeof BOOTSTRAP_LEVELS[number]
max_clusters: number
routing_args: string
clustering_args: string
bootstrap_args: string
// generations: number | ''
// routing_time: number | ''
// devices: number | ''
Expand Down Expand Up @@ -157,6 +158,7 @@ export const usePersist = create(
spawnpointMaxAreaAutoCalc: 100,
routing_args: '',
clustering_args: '',
bootstrap_args: '',
setStore: (key, value) => set({ [key]: value }),
}),
{
Expand Down
2 changes: 2 additions & 0 deletions client/src/hooks/useStatic.ts
Original file line number Diff line number Diff line change
Expand Up @@ -34,6 +34,7 @@ export interface UseStatic {
selected: string[]
route_plugins: string[]
clustering_plugins: string[]
bootstrap_plugins: string[]
tileServers: KojiTileServer[]
kojiRoutes: { name: string; id: number; type: string }[]
scannerRoutes: { name: string; id: number; type: string }[]
Expand Down Expand Up @@ -114,6 +115,7 @@ export const useStatic = create<UseStatic>((set, get) => ({
},
route_plugins: [],
clustering_plugins: [],
bootstrap_plugins: [],
layerEditing: {
cutMode: false,
dragMode: false,
Expand Down
2 changes: 2 additions & 0 deletions client/src/services/fetches.ts
Original file line number Diff line number Diff line change
Expand Up @@ -135,6 +135,7 @@ export async function clusteringRouting({
max_clusters,
routing_args,
clustering_args,
bootstrap_args,
} = usePersist.getState()
const { geojson, setStatic, bounds } = useStatic.getState()
const { add, activeRoute } = useShapes.getState().setters
Expand Down Expand Up @@ -249,6 +250,7 @@ export async function clusteringRouting({
s2_size,
routing_args,
clustering_args,
bootstrap_args,
}),
},
)
Expand Down
38 changes: 25 additions & 13 deletions or-tools/tsp/tsp.cc
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@

typedef std::vector<std::vector<int64_t>> DistanceMatrix;
typedef std::vector<std::vector<double>> RawInput;
typedef std::vector<int> RawOutput;

namespace operations_research
{
Expand Down Expand Up @@ -96,25 +97,25 @@ namespace operations_research
//! @param[in] manager The manager of the routing problem.
//! @param[in] routing The routing model.
//! @param[in] solution The solution of the routing problem.
RawInput GetRoutes(const RoutingIndexManager &manager, const RoutingModel &routing, const Assignment &solution)
RawOutput GetRoutes(const RoutingIndexManager &manager, const RoutingModel &routing, const Assignment &solution)
{
RawInput routes(manager.num_vehicles());
for (double vehicle_id = 0; vehicle_id < manager.num_vehicles(); ++vehicle_id)
RawOutput routes(manager.num_vehicles());
for (int vehicle_id = 0; vehicle_id < manager.num_vehicles(); ++vehicle_id)
{
int64_t index = routing.Start(vehicle_id);
routes[vehicle_id].push_back(manager.IndexToNode(index).value());
routes.push_back(manager.IndexToNode(index).value());
while (!routing.IsEnd(index))
{
index = solution.Value(routing.NextVar(index));
routes[vehicle_id].push_back(manager.IndexToNode(index).value());
routes.push_back(manager.IndexToNode(index).value());
}
}
return routes;
}

//! @brief Solves the TSP problem.
//! @param[in] locations The [Lat, Lng] pairs.
RawInput Tsp(RawInput locations)
RawOutput Tsp(RawInput locations)
{
DataModel data;
data.distance_matrix = distanceMatrix(locations);
Expand Down Expand Up @@ -165,10 +166,23 @@ std::vector<std::string> split(const std::string &s, char delimiter)
return tokens;
}

double stodpre(std::string const &str, std::size_t const p)
{
std::stringstream sstrm;
sstrm << std::setprecision(p) << std::fixed << str << std::endl;

LOG(INFO) << "Stream: " << sstrm.str();
double d;
sstrm >> d;

return d;
}

int main(int argc, char *argv[])
{
std::map<std::string, std::string> args;
RawInput points;
std::vector<std::string> stringPoints;

for (int i = 1; i < argc; ++i)
{
Expand All @@ -188,6 +202,7 @@ int main(int argc, char *argv[])
double lat = std::stod(coordinates[0]);
double lng = std::stod(coordinates[1]);
points.push_back({lat, lng});
stringPoints.push_back(pointStr);
}
}
}
Expand All @@ -201,14 +216,11 @@ int main(int argc, char *argv[])
}
}

RawInput routes = operations_research::Tsp(points);
for (auto route : routes)
RawOutput routes = operations_research::Tsp(points);

for (auto point : routes)
{
for (auto node : route)
{
std::cout << node << ",";
}
std::cout << std::endl;
std::cout << stringPoints[point] << " ";
}

return EXIT_SUCCESS;
Expand Down
Loading

0 comments on commit 06780a0

Please sign in to comment.