From 4f47b1c6d4df22aa6930035d8444fde99834864a Mon Sep 17 00:00:00 2001 From: Jochen Keil Date: Fri, 2 Jul 2010 20:42:23 +0200 Subject: [PATCH] this basically does the as AntMain but the static methods return an environment object which is runnable --- src/aco/EnvironmentFactory.java | 309 ++++++++++++++++++++++++++++++++++++++++ 1 file changed, 309 insertions(+) create mode 100644 src/aco/EnvironmentFactory.java diff --git a/src/aco/EnvironmentFactory.java b/src/aco/EnvironmentFactory.java new file mode 100644 index 0000000..e5fea24 --- /dev/null +++ b/src/aco/EnvironmentFactory.java @@ -0,0 +1,309 @@ +package aco; + +import java.io.IOException; + +import aco.antview.*; +import aco.graph.*; +import aco.graph.data.*; +import aco.strategy.*; +import aco.strategy.acs.*; +import aco.strategy.as.*; +import aco.strategy.as.eas.*; +import aco.strategy.as.asrank.*; +import aco.strategy.as.simpleas.*; +import aco.mediator.acs.*; +import aco.mediator.as.eas.*; +import aco.mediator.as.asrank.*; +import aco.mediator.as.simpleas.*; +import aco.parameter.acs.*; +import aco.parameter.as.eas.*; +import aco.parameter.as.asrank.*; +import aco.parameter.as.simpleas.*; +import aco.environment.*; +import aco.environment.as.*; +import aco.environment.acs.*; +import aco.tsplibreader.*; + +public class EnvironmentFactory { + + public static Environment acs(String filename, + double beta, + double qzero, + double roh, + int cl, + int numofants, + int runs) { + + ACSMediator acom = new ACSMediator(new ACSParameter()); + ACOGraph acog = new ACOGraph(acom); + acom.setACOGraph(acog); + + /* these are the most important */ + acom.setACOStrategy(new ACSStrategy(acom)); + acom.setTauZeroStrategy(new ACSTauZeroStrategy(acom)); + acom.setPheromoneStrategy(new ACSPheromoneStrategy(acom)); + acom.setChoiceInformationStrategy(new ACSChoiceInformationStrategy(acom)); + + /* these are not very likely to change */ + acom.setGraphStrategy(new GraphStrategy(acom)); + acom.setHeuristicInformationStrategy(new HeuristicInformationStrategy(acom)); + + acom.setGlobalBestTourMapSize(1); + acom.setMaxNumOfTours(runs); + acom.setBeta(beta); + acom.setRoh(roh); + acom.setQZero(qzero); + acom.setNearestNeighbourListDepth(cl); + + try { + TSPLibReader tsplr = new TSPLibReader(filename); + CoordinateData coordinates = new CoordinateData(tsplr); + + acom.setDistanceStrategy(DistanceStrategy.getStrategyFromTSPLibReader(tsplr, acom)); + acog.initializeFromCoordinates(coordinates); + + } catch (IOException e) { + System.out.println(e.toString() + "(" + e.getClass() + "): " + e); + return null; + } + + /* after the graph is initialized we can set the Number of Ants + * or leave it to the method parameter */ + if (numofants == 0) { + acom.setNumOfAnts(acom.getNumOfCities()); + } else { + acom.setNumOfAnts(numofants); + } + /* after the graph is initialized we can compute TauZero.. */ + acom.computeTauZero(); + /* ..then we can set the initial pheromone value.. */ + acom.setInitialPheromones(acom.getTauZero()); + /* ..and finally compute the initial Choice Information */ + acom.computeChoiceInformation(); + + System.out.println(acom.getACOParameter()); + + ACSEnvironment env = new ACSEnvironment(acom); + acom.setEnvironment(env); + + AntView av = new AntView(acom); + new Thread(av).start(); + env.addObserver(av); + + return (Environment)env; + } + + public static Environment as(String filename, + double alpha, + double beta, + double tauzero, + double roh, + int numofants, + int runs) { + SimpleASMediator acom = new SimpleASMediator(new SimpleASParameter()); + ACOGraph acog = new ACOGraph(acom); + acom.setACOGraph(acog); + + /* these are the most important */ + acom.setACOStrategy(new ASStrategy(acom)); + acom.setTauZeroStrategy(new SimpleASTauZeroStrategy(acom)); + acom.setPheromoneStrategy(new SimpleASPheromoneStrategy(acom)); + acom.setChoiceInformationStrategy(new ASChoiceInformationStrategy(acom)); + + /* these are not very likely to change */ + acom.setGraphStrategy(new GraphStrategy(acom)); + acom.setHeuristicInformationStrategy(new HeuristicInformationStrategy(acom)); + + acom.setGlobalBestTourMapSize(1); + acom.setMaxNumOfTours(runs); + acom.setAlpha(alpha); + acom.setBeta(beta); + acom.setRoh(roh); + + try { + TSPLibReader tsplr = new TSPLibReader(filename); + CoordinateData coordinates = new CoordinateData(tsplr); + + acom.setDistanceStrategy(DistanceStrategy.getStrategyFromTSPLibReader(tsplr, acom)); + acog.initializeFromCoordinates(coordinates); + + } catch (IOException e) { + System.out.println(e.toString() + "(" + e.getClass() + "): " + e); + return null; + } + + /* after the graph is initialized we can set the Number of Ants + * or leave it to the method parameter */ + if (numofants == 0) { + acom.setNumOfAnts(acom.getNumOfCities()); + } else { + acom.setNumOfAnts(numofants); + } + /* after the graph is initialized we can compute TauZero.. */ + acom.computeTauZero(); + /* ..then we can set the initial pheromone value.. */ + acom.setInitialPheromones(acom.getTauZero()); + /* ..and finally compute the initial Choice Information */ + acom.computeChoiceInformation(); + + System.out.println(acom.getACOParameter()); + + ASEnvironment env = new ASEnvironment(acom); + acom.setEnvironment(env); + + AntView av = new AntView(acom); + new Thread(av).start(); + env.addObserver(av); + + return (Environment)env; + } + + public static Environment eas(String filename, + double alpha, + double beta, + double tauzero, + double roh, + double e, + int numofants, + int runs) { + EASMediator acom = new EASMediator(new EASParameter()); + ACOGraph acog = new ACOGraph(acom); + acom.setACOGraph(acog); + + /* these are the most important */ + acom.setACOStrategy(new ASStrategy(acom)); + acom.setTauZeroStrategy(new EASTauZeroStrategy(acom)); + acom.setPheromoneStrategy(new EASPheromoneStrategy(acom)); + acom.setChoiceInformationStrategy(new ASChoiceInformationStrategy(acom)); + + /* these are not very likely to change */ + acom.setGraphStrategy(new GraphStrategy(acom)); + acom.setHeuristicInformationStrategy(new HeuristicInformationStrategy(acom)); + + acom.setGlobalBestTourMapSize(1); + acom.setMaxNumOfTours(runs); + acom.setAlpha(alpha); + acom.setBeta(beta); + acom.setRoh(roh); + + try { + TSPLibReader tsplr = new TSPLibReader(filename); + CoordinateData coordinates = new CoordinateData(tsplr); + + acom.setDistanceStrategy(DistanceStrategy.getStrategyFromTSPLibReader(tsplr, acom)); + acog.initializeFromCoordinates(coordinates); + + } catch (IOException exception) { + System.out.println(exception.toString() + + "(" + exception.getClass() + "): " + exception); + return null; + } + + if (e == 0.0) { + acom.setE((double)acom.getNumOfCities()); + } else { + acom.setE(e); + } + + /* after the graph is initialized we can set the Number of Ants + * or leave it to the method parameter */ + if (numofants == 0) { + acom.setNumOfAnts(acom.getNumOfCities()); + } else { + acom.setNumOfAnts(numofants); + } + + /* after the graph is initialized we can compute TauZero.. */ + acom.computeTauZero(); + /* ..then we can set the initial pheromone value.. */ + acom.setInitialPheromones(acom.getTauZero()); + /* ..and finally compute the initial Choice Information */ + acom.computeChoiceInformation(); + + System.out.println(acom.getACOParameter()); + + ASEnvironment env = new ASEnvironment(acom); + acom.setEnvironment(env); + + AntView av = new AntView(acom); + new Thread(av).start(); + env.addObserver(av); + + return (Environment)env; + } + + public static Environment asrank(String filename, + double alpha, + double beta, + double tauzero, + double roh, + double w, + int numofants, + int runs) { + ASRankMediator acom = new ASRankMediator(new ASRankParameter()); + ACOGraph acog = new ACOGraph(acom); + acom.setACOGraph(acog); + + /* these are the most important */ + acom.setACOStrategy(new ASStrategy(acom)); + + acom.setTauZeroStrategy(new ASRankTauZeroStrategy(acom)); + acom.setPheromoneStrategy(new ASRankPheromoneStrategy(acom)); + + /* these are not very likely to change */ + acom.setGraphStrategy(new GraphStrategy(acom)); + acom.setChoiceInformationStrategy(new ASChoiceInformationStrategy(acom)); + acom.setHeuristicInformationStrategy(new HeuristicInformationStrategy(acom)); + + acom.setGlobalBestTourMapSize(1); + acom.setMaxNumOfTours(runs); + acom.setAlpha(alpha); + acom.setBeta(beta); + acom.setRoh(roh); + + try { + TSPLibReader tsplr = new TSPLibReader(filename); + CoordinateData coordinates = new CoordinateData(tsplr); + + acom.setDistanceStrategy(DistanceStrategy.getStrategyFromTSPLibReader(tsplr, acom)); + acog.initializeFromCoordinates(coordinates); + + } catch (IOException e) { + System.out.println(e.toString() + "(" + e.getClass() + "): " + e); + return null; + } + + if (w == 0.0) { + acom.setW((double)acom.getNumOfCities()); + } else { + acom.setW(w); + } + + /* after the graph is initialized we can set the Number of Ants + * or leave it to the method parameter */ + if (numofants == 0) { + acom.setNumOfAnts(acom.getNumOfCities()); + } else { + acom.setNumOfAnts(numofants); + } + + /* after the graph is initialized we can compute TauZero.. */ + acom.computeTauZero(); + /* ..then we can set the initial pheromone value.. */ + acom.setInitialPheromones(acom.getTauZero()); + /* ..and finally compute the initial Choice Information */ + acom.computeChoiceInformation(); + + System.out.println(acom.getACOParameter()); + + ASEnvironment env = new ASEnvironment(acom); + acom.setEnvironment(env); + + AntView av = new AntView(acom); + new Thread(av).start(); + env.addObserver(av); + + return (Environment)env; + } + +} -- 2.11.4.GIT