Skip to content
Browse files

Add simulation loader to master.

  • Loading branch information...
1 parent 0c0260e commit 60762871fa6db8b80f87efd36876b3233870a934 Brian Guthrie committed Jul 28, 2012
View
17 Server/src/cs412/dinghyprop/Master.java
@@ -4,7 +4,6 @@
import cs412.dinghyprop.genetics.IPopulationObserver;
import cs412.dinghyprop.genetics.Program;
import cs412.dinghyprop.simulator.ISimulator;
-import cs412.dinghyprop.simulator.SimulatorRandom;
import java.net.MalformedURLException;
import java.rmi.RemoteException;
@@ -271,16 +270,22 @@ public String toString() {
/**
* Register a new {@code Master} with RMI.
- * @param args ignored
+ * @param args One argument: the directory name where simulator files
+ * are stored
* @throws Exception
*/
public static void main(String[] args) throws Exception {
+ if (args.length != 1) {
+ System.err.println("Usage: Master <simulation_directory>");
+ System.exit(-1);
+ }
+
+ SimulationDirLoader sdl = new SimulationDirLoader(args[0]);
+ ISimulator[] simulators = sdl.load();
+
GeneticProgram gp = new GeneticProgram(POPULATION_SIZE,
GeneticProgram.INIT_POP_METHOD.RHALF_AND_HALF, 3);
- ISimulator[] simulators = {new SimulatorRandom(10, 10, 6).getSimulator(),
- new SimulatorRandom(10, 10, 6).getSimulator(),
- new SimulatorRandom(10, 10, 6).getSimulator()
- };
+
new Master(gp, simulators, GENERATIONS).runGP();
}
View
66 Server/src/cs412/dinghyprop/SimulationDirLoader.java
@@ -0,0 +1,66 @@
+package cs412.dinghyprop;
+
+import cs412.dinghyprop.simulator.Simulator;
+import cs412.dinghyprop.simulator.SimulatorFile;
+
+import java.io.File;
+import java.io.FileFilter;
+import java.util.LinkedList;
+import java.util.List;
+
+/**
+ * Simulation directory loader
+ */
+public class SimulationDirLoader {
+
+ private File directory;
+
+ /**
+ * Create a new directory loader.
+ * @param directory The directory to load simulators from
+ */
+ public SimulationDirLoader(String directory) {
+ this.directory = new File(directory);
+ if (!this.directory.isDirectory()) {
+ throw new IllegalArgumentException(directory + "is not a directory.");
+ }
+ }
+
+ /**
+ * Load the simulators from the files in the directory.
+ * @return An array of the loaded simulators
+ */
+ public Simulator[] load() {
+ File[] files = getDirectoryFiles();
+
+ List<Simulator> simulators = new LinkedList<Simulator>();
+ for (File file : files) {
+ simulators.add(loadFile(file));
+ }
+
+ return simulators.toArray(new Simulator[simulators.size()]);
+ }
+
+ /**
+ * Get a listing of plain files in this loaders directory.
+ * @return All non-directories in {@code this.directory}
+ */
+ private File[] getDirectoryFiles() {
+ return directory.listFiles(new FileFilter() {
+ @Override
+ public boolean accept(File pathname) {
+ return pathname.isFile();
+ }
+ });
+ }
+
+ private Simulator loadFile(File file) {
+ SimulatorFile sf = new SimulatorFile(file);
+ return sf.getSim();
+ }
+
+ @Override
+ public String toString() {
+ return "SimulationDirLoader(" + directory + ')';
+ }
+}
View
8 Simulator/src/cs412/dinghyprop/simulator/SimulatorFile.java
@@ -54,6 +54,14 @@ public SimulatorFile(File filename) {
}
/**
+ * Obtain the simulator.
+ * @return The simulator constructed from the file
+ */
+ public Simulator getSim() {
+ return sim;
+ }
+
+ /**
* Sets the size of the simulation environment by reading from the file
*/
private void setSize(){

0 comments on commit 6076287

Please sign in to comment.
Something went wrong with that request. Please try again.