Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view

This file was deleted.

Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,4 @@ public record SimulationFrame(
String[][] grid,
List<PointDto> path,
int steps,
boolean pathFound,
Integer nodesVisited,
Integer frontierSize) {}
boolean pathFound) {}
Original file line number Diff line number Diff line change
Expand Up @@ -20,20 +20,13 @@
import com.algorithmrace.visualizer.utils.ComplexityCatalog;
import com.algorithmrace.visualizer.utils.MazeGenerator;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.Comparator;
import java.util.List;
import java.util.concurrent.ThreadLocalRandom;
import org.springframework.stereotype.Service;

@Service
public class SimulationService {
private static class FrameState {
int[] lastArray = null;
int[] lastHighlight = null;
int[] lastSearchPath = null;
}

private static final int MAX_FRAMES = 5000;
private static final int MAX_ARRAY_SIZE = 100;
private static final int MAX_LANES = 6;
Expand Down Expand Up @@ -138,13 +131,12 @@ private RaceLaneResponse simulateSortingLane(String name, int[] dataset) {
AlgorithmModel model = SortingAlgorithmFactory.create(name);
model.resetState(dataset);
List<SimulationFrame> frames = new ArrayList<>();
FrameState state = new FrameState();
frames.add(sortFrame(0, model, state));
frames.add(sortFrame(0, model));
int frame = 1;
while (!model.isDone() && frame < MAX_FRAMES) {
model.step();
model.setTimeMs((long) frame * SORT_FRAME_MS);
frames.add(sortFrame(frame, model, state));
frames.add(sortFrame(frame, model));
frame++;
}
LaneStats stats =
Expand All @@ -158,13 +150,12 @@ private RaceLaneResponse simulateSearchLane(String name, int[] dataset, int targ
model.resetState(dataset);
model.setTarget(target);
List<SimulationFrame> frames = new ArrayList<>();
FrameState state = new FrameState();
frames.add(searchFrame(0, model, state));
frames.add(searchFrame(0, model));
int frame = 1;
while (!model.isDone() && frame < MAX_FRAMES) {
model.step();
model.setTimeMs((long) frame * SEARCH_FRAME_MS);
frames.add(searchFrame(frame, model, state));
frames.add(searchFrame(frame, model));
frame++;
}
LaneStats stats =
Expand Down Expand Up @@ -245,17 +236,11 @@ private int[] resolveSearchingDataset(SearchingSimulationRequest request) {
return ArrayGenerator.generate(size, ArrayGenerator.ArrayType.RANDOM);
}

private SimulationFrame sortFrame(int frame, AlgorithmModel model, FrameState state) {
if (state.lastArray == null || !Arrays.equals(state.lastArray, model.getArray())) {
state.lastArray = model.getArray().clone();
}
if (state.lastHighlight == null || !Arrays.equals(state.lastHighlight, model.getHighlight())) {
state.lastHighlight = model.getHighlight().clone();
}
private SimulationFrame sortFrame(int frame, AlgorithmModel model) {
return new SimulationFrame(
frame,
state.lastArray,
state.lastHighlight,
model.getArray().clone(),
model.getHighlight().clone(),
model.getSortedBoundary(),
model.getPivotIndex(),
model.getMergeRegionStart(),
Expand All @@ -271,26 +256,14 @@ private SimulationFrame sortFrame(int frame, AlgorithmModel model, FrameState st
null,
List.of(),
0,
false,
null,
null);
false);
}

private SimulationFrame searchFrame(int frame, SearchModel model, FrameState state) {
if (state.lastArray == null || !Arrays.equals(state.lastArray, model.getArray())) {
state.lastArray = model.getArray().clone();
}
if (state.lastHighlight == null || !Arrays.equals(state.lastHighlight, model.getHighlight())) {
state.lastHighlight = model.getHighlight().clone();
}
if (state.lastSearchPath == null
|| !Arrays.equals(state.lastSearchPath, model.getSearchPath())) {
state.lastSearchPath = model.getSearchPath().clone();
}
private SimulationFrame searchFrame(int frame, SearchModel model) {
return new SimulationFrame(
frame,
state.lastArray,
state.lastHighlight,
model.getArray().clone(),
model.getHighlight().clone(),
model.getSortedBoundary(),
model.getPivotIndex(),
model.getMergeRegionStart(),
Expand All @@ -302,34 +275,14 @@ private SimulationFrame searchFrame(int frame, SearchModel model, FrameState sta
model.isDone(),
model.getStatus(),
model.getFoundIndex(),
state.lastSearchPath,
model.getSearchPath().clone(),
null,
List.of(),
0,
false,
null,
null);
false);
}

private SimulationFrame pathFrame(int frame, PathfindingModel model, long timeMs) {
GridCell[][] grid = model.getGrid();
String[][] states = new String[grid.length][grid[0].length];
int nodesVisited = 0;
int frontierSize = 0;

for (int r = 0; r < grid.length; r++) {
for (int c = 0; c < grid[r].length; c++) {
CellState cellState = grid[r][c].state;
states[r][c] = cellState.name();

if (cellState == CellState.VISITED || cellState == CellState.PATH) {
nodesVisited++;
} else if (cellState == CellState.FRONTIER) {
frontierSize++;
}
}
}

return new SimulationFrame(
frame,
new int[0],
Expand All @@ -346,12 +299,20 @@ private SimulationFrame pathFrame(int frame, PathfindingModel model, long timeMs
model.isDone() ? "Done" : "Running",
null,
new int[0],
states,
gridState(model.getGrid()),
model.getPath().stream().map(cell -> new PointDto(cell.row, cell.col)).toList(),
model.getSteps(),
model.isPathFound(),
nodesVisited,
frontierSize);
model.isPathFound());
}

private String[][] gridState(GridCell[][] grid) {
String[][] states = new String[grid.length][grid[0].length];
for (int r = 0; r < grid.length; r++) {
for (int c = 0; c < grid[r].length; c++) {
states[r][c] = grid[r][c].state.name();
}
}
return states;
}

private void markPath(PathfindingModel model) {
Expand Down
Loading
Loading