Skip to content

Commit

Permalink
Add cli support (J-morag#31)
Browse files Browse the repository at this point in the history
* Added a CLI
* Fixed OS dependent paths
* Better default results output
  • Loading branch information
J-morag authored Feb 13, 2023
1 parent 45da63d commit 0e290c8
Show file tree
Hide file tree
Showing 14 changed files with 329 additions and 51 deletions.
9 changes: 8 additions & 1 deletion pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@
<groupId>MAPF</groupId>
<artifactId>MAPF</artifactId>
<version>1.0-SNAPSHOT</version>
<packaging>jar</packaging>
<dependencies>
<dependency>
<groupId>org.junit.jupiter</groupId>
Expand All @@ -29,11 +30,17 @@
<artifactId>commons-collections4</artifactId>
<version>4.4</version>
</dependency>
<!-- https://mvnrepository.com/artifact/commons-cli/commons-cli -->
<dependency>
<groupId>commons-cli</groupId>
<artifactId>commons-cli</artifactId>
<version>1.5.0</version>
</dependency>
<!-- https://mvnrepository.com/artifact/org.json/json -->
<dependency>
<groupId>org.json</groupId>
<artifactId>json</artifactId>
<version>20211205</version>
<version>20220320</version>
</dependency>
<dependency>
<groupId>org.jetbrains</groupId>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -136,13 +136,12 @@ private MAPF_Instance getInstance(String instanceName, InstanceManager.InstanceP
public void prepareInstances(String instanceName, InstanceManager.InstancePath instancePath, InstanceProperties instanceProperties){

MAPF_Instance mapf_instance = this.getInstance(instanceName, instancePath, instanceProperties);
if ( mapf_instance != null ){
if ( mapf_instance != null &&
(instanceProperties == null || instanceProperties.regexPattern.matcher(mapf_instance.extendedName).matches())){
this.instanceStack.push(mapf_instance);
}
}



@Override
public MAPF_Instance getNextExistingInstance(){
if( ! this.instanceStack.empty() ){
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -78,7 +78,7 @@ public InstanceBuilder_MovingAI(Priorities priorities) {
}

@Override
public void prepareInstances(String instanceName, InstanceManager.InstancePath instancePath, InstanceProperties instanceProperties) {
public void prepareInstances(String mapName, InstanceManager.InstancePath instancePath, InstanceProperties instanceProperties) {

if (!(instancePath instanceof InstanceManager.Moving_AI_Path)) { return; }

Expand All @@ -101,11 +101,13 @@ public void prepareInstances(String instanceName, InstanceManager.InstancePath i

Agent[] agents = getAgents(agentLines,numOfAgentsFromProperties[i]);

if (instanceName == null || agents == null) { continue; /* Invalid parameters */ }
if (mapName == null || agents == null) { continue; /* Invalid parameters */ }

mapf_instance = makeInstance(instanceName, graphMap, agents, moving_ai_path);
mapf_instance.setObstaclePercentage(instanceProperties.obstacles.getReportPercentage());
this.instanceList.add(mapf_instance);
mapf_instance = makeInstance(mapName, graphMap, agents, moving_ai_path);
if (instanceProperties.regexPattern.matcher(mapf_instance.extendedName).matches()){
mapf_instance.setObstaclePercentage(instanceProperties.obstacles.getReportPercentage());
this.instanceList.add(mapf_instance);
}
}
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -52,7 +52,7 @@ public InstanceBuilder_Warehouse(Boolean dropDisabledEdges) {
}

@Override
public void prepareInstances(String instanceName, InstanceManager.InstancePath instancePath, InstanceProperties instanceProperties) {
public void prepareInstances(String mapName, InstanceManager.InstancePath instancePath, InstanceProperties instanceProperties) {
if (!(instancePath instanceof InstanceManager.Moving_AI_Path)) { return; }

InstanceManager.Moving_AI_Path moving_ai_path = (InstanceManager.Moving_AI_Path) instancePath;
Expand All @@ -73,11 +73,13 @@ public void prepareInstances(String instanceName, InstanceManager.InstancePath i

Agent[] agents = getAgents(agentLines, numOfAgentsFromProperties[i]);

if (instanceName == null || agents == null) { continue; /* Invalid parameters */ }
if (mapName == null || agents == null) { continue; /* Invalid parameters */ }

mapf_instance = makeInstance(instanceName, graphMap, agents, moving_ai_path);
mapf_instance.setObstaclePercentage(instanceProperties.obstacles.getReportPercentage());
this.instanceList.add(mapf_instance);
mapf_instance = makeInstance(mapName, graphMap, agents, moving_ai_path);
if (instanceProperties.regexPattern.matcher(mapf_instance.extendedName).matches()){
mapf_instance.setObstaclePercentage(instanceProperties.obstacles.getReportPercentage());
this.instanceList.add(mapf_instance);
}
}
}

Expand Down
10 changes: 7 additions & 3 deletions src/main/java/BasicMAPF/Instances/InstanceManager.java
Original file line number Diff line number Diff line change
@@ -1,11 +1,13 @@
package BasicMAPF.Instances;

import BasicMAPF.Instances.InstanceBuilders.I_InstanceBuilder;
import Environment.IO_Package.IO_Manager;

import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;
import java.util.Objects;
import java.util.regex.Pattern;

public class InstanceManager {

Expand Down Expand Up @@ -47,11 +49,13 @@ public InstanceManager(String sourceDirectory, I_InstanceBuilder instanceBuilder

public MAPF_Instance getSpecificInstance(InstancePath currentPath){

String regexSeparator = "\\\\"; // this is actually: '\\'
String[] splitedPath = currentPath.path.split(regexSeparator);
String instanceName = splitedPath[splitedPath.length-1];
String regexSeparator = IO_Manager.pathSeparator;
String[] splitPath = currentPath.path.split(Pattern.quote(regexSeparator));
String instanceName = splitPath[splitPath.length-1];

// one instance can become many (different amounts of agents...)
this.instanceBuilder.prepareInstances(instanceName, currentPath, this.instanceProperties);

return this.instanceBuilder.getNextExistingInstance();
}

Expand Down
18 changes: 13 additions & 5 deletions src/main/java/BasicMAPF/Instances/InstanceProperties.java
Original file line number Diff line number Diff line change
Expand Up @@ -2,17 +2,19 @@

import BasicMAPF.Instances.Maps.MapDimensions;

import java.util.Objects;
import java.util.regex.Pattern;

public class InstanceProperties {

public final MapDimensions mapSize;
public final ObstacleWrapper obstacles;
public final int[] numOfAgents;
public final Pattern regexPattern;


public InstanceProperties() {
this.mapSize = new MapDimensions();
this.obstacles = new ObstacleWrapper();
this.numOfAgents = new int[0];
this(null, -1, null, null);
}

public InstanceProperties(MapDimensions.Enum_mapOrientation enumMapOrientation){
Expand All @@ -21,20 +23,26 @@ public InstanceProperties(MapDimensions.Enum_mapOrientation enumMapOrientation){
}



public InstanceProperties(MapDimensions mapSize, double obstacleRate, int[] numOfAgents) {
this(mapSize, obstacleRate, numOfAgents, null);
}

/***
* Properties constructor
* @param mapSize - {@link MapDimensions} indicates the Axis lengths , zero for unknown
* @param obstacleRate - A double that indicates the obstacle rate of the map. For unknown obstacles enter -1
* @param numOfAgents - An array of different num of agents.
* @param instancesRegex - Only use instances matching this regular expression.
*/
public InstanceProperties(MapDimensions mapSize, double obstacleRate, int[] numOfAgents) {
public InstanceProperties(MapDimensions mapSize, double obstacleRate, int[] numOfAgents, String instancesRegex) {
this.mapSize = (mapSize == null ? new MapDimensions() : mapSize);
this.obstacles = (obstacleRate == -1 ? new ObstacleWrapper() : new ObstacleWrapper(obstacleRate));
this.numOfAgents = (numOfAgents == null ? new int[0] : numOfAgents);
this.regexPattern = Pattern.compile(Objects.requireNonNullElse(instancesRegex, ".*"));
}



public class ObstacleWrapper {

public static final double DEFAULT_OBSTACLE_RATE = -1;
Expand Down
28 changes: 25 additions & 3 deletions src/main/java/Environment/RunManagers/A_RunManager.java
Original file line number Diff line number Diff line change
Expand Up @@ -8,11 +8,13 @@
import Environment.Metrics.InstanceReport;
import Environment.Metrics.S_Metrics;

import java.io.File;
import java.io.FileOutputStream;
import java.io.IOException;
import java.text.DateFormat;
import java.util.ArrayList;
import java.util.List;
import java.util.Objects;

/**
* This in an abstract class that overcomes the need to comment out lines in the 'Main' method
Expand All @@ -24,7 +26,27 @@ public abstract class A_RunManager {
protected List<I_Solver> solvers = new ArrayList<>();
protected List<Experiment> experiments = new ArrayList<>();

protected String resultsOutputDir = IO_Manager.buildPath(new String[]{System.getProperty("user.home"), "CBS_Results"});
public static final String DEFAULT_RESULTS_OUTPUT_DIR = IO_Manager.buildPath(new String[]{System.getProperty("user.home"), "MAPF_Results"});
private final String resultsOutputDir;
protected String resultsFilePrefix = "results";

protected A_RunManager(String resultsOutputDir) {
this.resultsOutputDir = Objects.requireNonNullElse(resultsOutputDir, DEFAULT_RESULTS_OUTPUT_DIR);
verifyOutputPath(this.resultsOutputDir);
}

public static boolean verifyOutputPath(String path) {
File directory = new File(path);
if (! directory.exists()){
boolean created = directory.mkdir();
if(!created){
String errString = "Could not locate or create output directory.";
System.out.println(errString);
return false;
}
}
return true;
}

abstract void setSolvers();
abstract void setExperiments();
Expand Down Expand Up @@ -81,7 +103,7 @@ protected void setOutputStreamsBeforeRunning() {
sleepToAvoidOverridingPreviousResultsFiles();

DateFormat dateFormat = S_Metrics.defaultDateFormat;
String pathWithStartTime = resultsOutputDir + "\\results " + dateFormat.format(System.currentTimeMillis()) + " .csv";
String pathWithStartTime = IO_Manager.buildPath(new String[]{resultsOutputDir, "log " + resultsFilePrefix + " " + dateFormat.format(System.currentTimeMillis())}) + " .csv";
try {
S_Metrics.addOutputStream(new FileOutputStream((pathWithStartTime)));
} catch (IOException e) {
Expand All @@ -93,7 +115,7 @@ protected void setOutputStreamsBeforeRunning() {
protected void exportAllResults() {
sleepToAvoidOverridingPreviousResultsFiles();
DateFormat dateFormat = S_Metrics.defaultDateFormat;
String pathWithEndTime = resultsOutputDir + "\\results " + dateFormat.format(System.currentTimeMillis()) + " .csv";
String pathWithEndTime = IO_Manager.buildPath(new String[]{resultsOutputDir, "res " + resultsFilePrefix + " " + dateFormat.format(System.currentTimeMillis())}) + " .csv";
try {
S_Metrics.exportCSV(new FileOutputStream(pathWithEndTime) );
} catch (IOException e) {
Expand Down
56 changes: 56 additions & 0 deletions src/main/java/Environment/RunManagers/GenericRunManager.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,56 @@
package Environment.RunManagers;

import BasicMAPF.Instances.InstanceBuilders.I_InstanceBuilder;
import BasicMAPF.Instances.InstanceManager;
import BasicMAPF.Instances.InstanceProperties;
import BasicMAPF.Solvers.CBS.CBS_Solver;
import BasicMAPF.Solvers.PrioritisedPlanning.PrioritisedPlanning_Solver;
import Environment.Experiment;
import org.jetbrains.annotations.NotNull;

public class GenericRunManager extends A_RunManager {

private final String instancesDir;
private final int[] agentNums;
private final I_InstanceBuilder instanceBuilder;
private final String experimentName;
private final boolean skipAfterFail;
private final String instancesRegex;

public GenericRunManager(@NotNull String instancesDir, int[] agentNums, @NotNull I_InstanceBuilder instanceBuilder,
@NotNull String experimentName, boolean skipAfterFail, String instancesRegex,
String resultsOutputDir, String resultsFilePrefix) {
super(resultsOutputDir);
if (agentNums == null){
throw new IllegalArgumentException("AgentNums can't be null");
}
this.instancesDir = instancesDir;
this.agentNums = agentNums;
this.instanceBuilder = instanceBuilder;
this.experimentName = experimentName;
this.skipAfterFail = skipAfterFail;
this.instancesRegex = instancesRegex;
this.resultsFilePrefix = resultsFilePrefix;
}
@Override
void setSolvers() {
// TODO modular solvers?
super.solvers.add(new PrioritisedPlanning_Solver());
super.solvers.add(new CBS_Solver());
}

@Override
void setExperiments() {
/* = Set Properties = */
InstanceProperties properties = new InstanceProperties(null, -1, agentNums, instancesRegex);

/* = Set Instance Manager = */
InstanceManager instanceManager = new InstanceManager(instancesDir, instanceBuilder, properties);

/* = Add new experiment = */
Experiment experiment = new Experiment(experimentName, instanceManager);
experiment.skipAfterFail = this.skipAfterFail;
this.experiments.add(experiment);
}

}
4 changes: 4 additions & 0 deletions src/main/java/Environment/RunManagers/ModularRunManager.java
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,10 @@

public class ModularRunManager extends A_RunManager{

public ModularRunManager() {
super(null);
}

@Override
void setSolvers() {

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -6,14 +6,14 @@
import BasicMAPF.Solvers.CBS.CBS_Solver;
import BasicMAPF.Solvers.PrioritisedPlanning.PrioritisedPlanning_Solver;
import Environment.Experiment;
import java.util.stream.IntStream;

public class RunManagerMovingAIBenchmark extends A_RunManager{

private final String entireBenchmarkDir;
private final int[] agentNums;

public RunManagerMovingAIBenchmark(String entireBenchmarkDir, int[] agentNums) {
super(null);
this.entireBenchmarkDir = entireBenchmarkDir;
this.agentNums = agentNums;
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,10 @@

public class RunManagerSimpleExample extends A_RunManager {

public RunManagerSimpleExample() {
super(null);
}

/* = Set BasicCBS.Solvers = */
@Override
void setSolvers() {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -6,14 +6,14 @@
import BasicMAPF.Solvers.CBS.CBS_Solver;
import BasicMAPF.Solvers.PrioritisedPlanning.PrioritisedPlanning_Solver;
import Environment.Experiment;
import java.util.stream.IntStream;

public class RunManagerWarehouse extends A_RunManager{

private final String warehouseMaps;
private final int[] agentNums;

public RunManagerWarehouse(String warehouseMaps, int[] agentNums) {
super(null);
this.warehouseMaps = warehouseMaps;
this.agentNums = agentNums;
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,10 @@ public class TestingBenchmarkRunManager extends A_RunManager {
private final String path = IO_Manager.buildPath( new String[]{ IO_Manager.testResources_Directory,
"TestingBenchmark"});

public TestingBenchmarkRunManager() {
super(null);
}

@Override
void setSolvers() {
this.solvers.add( new CBS_Solver());
Expand Down
Loading

0 comments on commit 0e290c8

Please sign in to comment.