diff --git a/diversim/diversim.sh b/diversim/diversim.sh index 3c43c93..546c492 100644 --- a/diversim/diversim.sh +++ b/diversim/diversim.sh @@ -1,11 +1,14 @@ #!/bin/bash -binDir=$HOME/workspace/diversim/bin # where are the binaries -libDir=$HOME/Diversify/mason # where are the libs +# where are the binaries +binDir=$HOME/ownCloud/neutral_model/diversify/diversim/src/main/java + +# where are the libs +libDir=$HOME/ownCloud/tcd/mason libs=$libDir/jmf.jar:$libDir/itext-1.2.jar:$libDir/portfolio.jar:$libDir/jcommon-1.0.16.jar:$libDir/jfreechart-1.0.13.jar:$libDir/jar/mason.17.jar -diversim_cmdLine="java -Xmx256M -cp $binDir:$libs diversim.BipartiteGraph" # WithUI +diversim_cmdLine="java -Xmx256M -cp $binDir:$libs diversim.BipartiteGraphWithUI" # WithUI diversim_params="-for 2000" # 2>/dev/null -seed 1 -time 1000 -for 200000 if [ $# -lt 1 ] ; then diff --git a/diversim/src/main/java/diversim/App.java b/diversim/src/main/java/diversim/App.java index b580354..daed46a 100644 --- a/diversim/src/main/java/diversim/App.java +++ b/diversim/src/main/java/diversim/App.java @@ -2,53 +2,147 @@ import java.util.List; +import java.util.ArrayList; +import java.util.HashSet; +import java.util.Set; // Interface +import diversim.strategy.extinction.AppExtinctionStrategy; +import diversim.strategy.reproduction.AppReproductionStrategy; +import ec.util.MersenneTwisterFast; import sim.engine.SimState; /** - * Apps must be injected in the simulation via the createApp() method in the BipartiteGraph class. - * - * @author Marco Biazzini + * Apps rely on specific services to function + * @author Vivek Nallur + * + * In each step, an App has the authority may choose to reproduce itself, either via clone or + * speciation, decided by the {@link #reproducers}, which are all instances of + * {@ AppReproductionStrategy}. + * + * An App dies if a instance of {@ AppExtinctionStrategy} in {@link #killers} suggests so. A + * {@link #dead} app will not reproduce, and will also be removed from the bipartite graph. * + * @author Hui Song */ public class App extends Entity { - -double redundancy = 0; - - -public double getRedundancy() { - return redundancy; -} - - -public App(int id, List servs) { - super(id); - for (Service s : servs) { - BipartiteGraph.addUnique(services, s); - } -} - - -/* - * (non-Javadoc) - * @see diversim.Entity#step(sim.engine.SimState) - */ -@Override -public void step(SimState state) { - BipartiteGraph graph = (BipartiteGraph) state; - -// TODO something - - redundancy = ((double)degree) / graph.numPlatforms; - System.out.println("Step " + state.schedule.getSteps() + " : " + toString()); -} - - -@Override -public String toString() { - String res = super.toString(); - res += " ; redundancy = " + redundancy; - return res; -} - + + + double redundancy = 0; + + public boolean dead = false; + + public List platforms = new ArrayList(); + + + public double getRedundancy() { + return redundancy; + } + + List reproducers; + List killers; + + + public List getDependencies(){ + return this.services; + } + + public void addDependencies(List new_deps){ + Set all_services = new HashSet(this.services); + all_services.addAll(new_deps); + this.services.clear(); + this.services.addAll(all_services); + } + + public void removeDependencies(List obs_deps){ + // The removeAll method should ideally execute in O(n) time + // however at least as of java 1.7, it does not. Inserting + // the obsolete_dependencies into a HashSet ensures that array + // compaction happens only once, and thus is reasonably optimal + + this.services.removeAll(new HashSet(obs_deps)); + } + + + + + public List reproduce(BipartiteGraph state){ + List result = new ArrayList(); + for(AppReproductionStrategy reproducer : reproducers){ + result.addAll(reproducer.reproduce(this, state)); + } + return result; + } + + + public App(int id, List service_dependencies) { + super(id); + this.services = new ArrayList (service_dependencies); + + // TODO: Now only use AppSpeciationReproduction, may need to include others + //Not initialized from 0, so that new Apps will always survice to their first + // steps. + degree = -1; + } + + public void initStrategies(BipartiteGraph graph){ + this.reproducers = StrategyFactory.fINSTANCE.createAppReproductionStrategy(this, graph); + this.killers = StrategyFactory.fINSTANCE.createAppExtinctionStrategies(this, graph); + } + + + /* + * (non-Javadoc) + * @see diversim.Entity#step(sim.engine.SimState) + * + * This is where we want the command pattern to be invoked from the bipartite + * graph. + */ + @Override + public void step(SimState state) { + BipartiteGraph graph = (BipartiteGraph) state; + + // TODO something + + if(dieOrNot(graph)){ + return; + } + + MersenneTwisterFast rnd = new MersenneTwisterFast(System.nanoTime()); + + + List newApps = reproduce(graph); + for(App app : newApps){ + graph.addApp(app); + } + + redundancy = ((double)degree) / graph.numPlatforms; + if(redundancy < 0) redundancy = 0; + System.out.println("Step " + state.schedule.getSteps() + " : " + toString()); + } + + + @Override + public String toString() { + String res = super.toString(); + res += " ; redundancy = " + redundancy; + return res; + } + + /** + * Many killers, any of them could kill an App + * + * TODO: should introduce a "Die Strategy" + */ + public boolean dieOrNot(BipartiteGraph graph){ + if(dead) + return true; + for(AppExtinctionStrategy killer : killers){ + if( killer.die(this, graph)){ + this.dead = true; + return true; + } + } + return false; + } + } diff --git a/diversim/src/main/java/diversim/BipartiteGraph.java b/diversim/src/main/java/diversim/BipartiteGraph.java index 8ee4b0b..9f46c62 100644 --- a/diversim/src/main/java/diversim/BipartiteGraph.java +++ b/diversim/src/main/java/diversim/BipartiteGraph.java @@ -24,7 +24,27 @@ * consistent updating (see comments about the start() method). * More info in the comments of the methods. * + * In a nutshell: + * We have a two communities of species: platforms and apps + * Each specie of platform is description of the set of services it supports. + * Each individual platform-instance is an individual of a particular specie. + * There can be many species, but a specie with no individual + * platform-instances is extinct. + * + * Each specie of app is a description of the set of services it needs. Each + * app-instance is an individual of a particular specie. If an app-instance + * cannot find a platform-instance that supports at least its required set of + * services, it dies. + * + * Each platform-instance supports one app-instance. + * + * For visualization purposes: + * We need to show specie-level interactions, perhaps(?) instead of individual + * level interactions. + * * @author Marco Biazzini + * @author Vivek Nallur + * @author Hui Song * */ public class BipartiteGraph extends SimState { @@ -42,7 +62,7 @@ public class BipartiteGraph extends SimState { /** * Initial total number of services */ -int initServices = 30; +public static int initServices = 30; /** * Max number of links a platform bears without triggering some diversification rule. @@ -54,6 +74,12 @@ public class BipartiteGraph extends SimState { */ int platformMinSize = 3; +/** + * x out of 100 possibility to allow an App to reproduce itself. + */ +int percentAppReproduce = 1; // 1% +int percentPlatformReproduce = 1; // 1% + /** * Current number of platforms. */ @@ -102,6 +128,9 @@ public class BipartiteGraph extends SimState { private int aCounter; public boolean changed; +public static BipartiteGraph INSTANCE = null; + + /** * Getters and setters. @@ -234,24 +263,28 @@ private void init() { public BipartiteGraph(long seed) { super(seed); init(); + INSTANCE = this; } public BipartiteGraph(MersenneTwisterFast random) { super(random); init(); + INSTANCE = this; } public BipartiteGraph(MersenneTwisterFast random, Schedule schedule) { super(random, schedule); init(); + INSTANCE = this; } public BipartiteGraph(long seed, Schedule schedule) { super(seed, schedule); init(); + INSTANCE = this; } @@ -279,21 +312,25 @@ public void start() { services.add(new Service(++sCounter)); numServices++; } + + // create platforms for (int i = 0; i < initPlatforms; i++) { // all services to each platform - createPlatform(services); + Platform pltf = createPlatform(services); + pltf.initStategies(this); } // create apps for (int i = 0; i < initApps; i++) { - createApp(selectServices()); + App app = createApp(selectServices()); + app.initStrategies(this); } // create the fate agent - fate = new Fate(random); - schedule.scheduleRepeating(schedule.getTime() + 1.2, fate, 1.0); + //fate = new Fate(random); + //schedule.scheduleRepeating(schedule.getTime() + 1.2, fate, 1.0); // define initial network: // link every platform to all apps that use at least one of its services @@ -308,12 +345,15 @@ public void start() { // then the network printout, then fate. Steppable print = new Steppable() { public void step(SimState state) { + //System.out.println("Step " + state.schedule.getSteps() + " : " + "what happened?"); if (changed) printoutNetwork(); changed = false; } }; schedule.scheduleRepeating(schedule.getTime() + 1.1, print, 1.0); + schedule.scheduleRepeating(schedule.getTime()+1, new ReConnect()); + } @@ -373,6 +413,21 @@ public App createApp(List servs) { return app; } +/** + * Same as above, but the App instance is created somewhere else + * @param app + * @return + */ +public App addApp(App app){ + bipartiteNetwork.addNode(app); + apps.add(app); + numApps++; + changed = true; + schedule.scheduleRepeating(app); + + return app; +} + /** * Update existing links that have the argument at one end. @@ -434,6 +489,13 @@ public void createLinks(Entity e, ArrayList entities) { } +public void setLink(App app, Platform pltf){ + bipartiteNetwork.addEdge(app, pltf, 1); + app.degree ++; + pltf.degree ++; + changed = true; +} + /** * Textual printout of the network @@ -520,5 +582,17 @@ static public void printAny(Object data, String trailer, PrintStream out) { out.flush(); } +public Platform addPlatform(Platform platform) { + bipartiteNetwork.addNode(platform); + platforms.add(platform); + numPlatforms++; + changed = true; + schedule.scheduleRepeating(platform); + return platform; + +} + + + } diff --git a/diversim/src/main/java/diversim/BipartiteGraphWithUI.java b/diversim/src/main/java/diversim/BipartiteGraphWithUI.java index ebfc2a2..8ff5c00 100644 --- a/diversim/src/main/java/diversim/BipartiteGraphWithUI.java +++ b/diversim/src/main/java/diversim/BipartiteGraphWithUI.java @@ -1,21 +1,30 @@ package diversim; +import java.awt.Color; +import java.awt.Graphics2D; +import java.awt.List; +import java.awt.Paint; +import java.awt.geom.Rectangle2D; + +import javax.swing.JFrame; + import sim.display.Console; import sim.display.Controller; import sim.display.Display2D; import sim.display.GUIState; import sim.engine.SimState; import sim.field.continuous.Continuous2D; -import sim.portrayal.continuous.*; -import sim.portrayal.simple.*; -import javax.swing.*; -import java.awt.Color; -import sim.portrayal.network.*; -import sim.portrayal.*; +import sim.portrayal.DrawInfo2D; +import sim.portrayal.Inspector; +import sim.portrayal.LocationWrapper; +import sim.portrayal.continuous.ContinuousPortrayal2D; +import sim.portrayal.network.NetworkPortrayal2D; +import sim.portrayal.network.SimpleEdgePortrayal2D; +import sim.portrayal.network.SpatialNetwork2D; +import sim.portrayal.simple.OvalPortrayal2D; +import sim.portrayal.simple.RectanglePortrayal2D; import sim.util.Double2D; -import java.awt.*; - /** * GUI of the BipartiteGraph simulation model. @@ -57,15 +66,18 @@ public static String getName() { private void setPositions() { BipartiteGraph graph = (BipartiteGraph)state; + + sysSpace.clear(); + int i = 1; Double2D pos; - double dist = sysSpace.getWidth() / (graph.numApps + 1); + double dist = sysSpace.getWidth() / (graph.apps.size()+1); for (Object obj : graph.apps) { pos = new Double2D((dist * i++), sysSpace.getHeight() * 0.35); sysSpace.setObjectLocation(obj, pos); } i = 1; - dist = sysSpace.getWidth() / (graph.numPlatforms + 1); + dist = sysSpace.getWidth() / (graph.platforms.size() + 1); for (Object obj : graph.platforms) { pos = new Double2D((dist * i++), sysSpace.getHeight() * 0.65); sysSpace.setObjectLocation(obj, pos); @@ -99,25 +111,37 @@ App.class, new RectanglePortrayal2D() {// inline subclass to override draw() public void draw(Object object, Graphics2D graphics, DrawInfo2D info) { App app = (App)object; paint = new Color(0, 0, (int)(255 * app.getRedundancy())); - double dist = sysSpace.getWidth() / (((BipartiteGraph)state).numApps + 1); - info.draw.width = ((double)app.getSize()) / ((BipartiteGraph)state).numServices * dist; + //paint = new Color(0,0, (int) (255 * app.services.size() / BipartiteGraph.initServices)); + double dist = sysSpace.getWidth() / (((BipartiteGraph)state).apps.size() + 1); + info.draw.width = ((double)app.getSize()) / ((BipartiteGraph)state).apps.size() * dist; if (info.draw.width >= dist) info.draw.width = dist * 0.9; if (info.draw.width < (dist * 0.2)) info.draw.width = dist * 0.25; info.draw.height = 50; + + + super.draw(object, graphics, info); + drawServices(graphics, info.draw, app.services, BipartiteGraph.initServices); + } + public Inspector getInspector(LocationWrapper wrapper, GUIState state){ + Inspector insp = super.getInspector(wrapper, state); + App app = (App) wrapper.getObject(); + return insp; } }); entitiesPortrayal.setPortrayalForClass( Platform.class, new OvalPortrayal2D() {// inline subclass to override draw() public void draw(Object object, Graphics2D graphics, DrawInfo2D info) { Platform plat = (Platform)object; - paint = new Color((int)(255 * plat.getPressure()), 0, 0); - double dist = sysSpace.getWidth() / (((BipartiteGraph)state).numPlatforms + 1); + paint = new Color((int)(255 * plat.getLoadingFactor()), 0, 0); + //paint = new Color((int)(255 * plat.services.size() / BipartiteGraph.initServices), 0, 0); + double dist = sysSpace.getWidth() / (((BipartiteGraph)state).platforms.size() + 1); info.draw.width = ((double)plat.getSize()) / ((BipartiteGraph)state).numServices * dist; if (info.draw.width >= dist) info.draw.width = dist * 0.9; if (info.draw.width < (dist * 0.2)) info.draw.width = dist * 0.25; info.draw.height = 50; super.draw(object, graphics, info); + drawServices(graphics, info.draw, plat.services, BipartiteGraph.initServices); } }); linksPortrayal.setField(new SpatialNetwork2D(sysSpace, graph.bipartiteNetwork)); @@ -147,7 +171,12 @@ public void init(Controller c) { public boolean step() { super.step(); setPositions(); - return !state.schedule.scheduleComplete(); + BipartiteGraph graph = (BipartiteGraph) state; + if(graph.apps.size()==0 || graph.platforms.size()==0) + return false; + else + return true; + //return !state.schedule.scheduleComplete(); } @@ -164,5 +193,15 @@ public Object getSimulationInspectedObject() { return state; } +public void drawServices(Graphics2D graphics, Rectangle2D.Double draw, java.util.List dna, int numAllServices){ + + Paint original = graphics.getPaint(); + graphics.setPaint(new Color(255,255,255)); + //graphics.drawLine(0,0,50,50); + graphics.drawLine((int)(draw.x), (int)(draw.y - draw.height / 2), (int)(draw.x), (int)(draw.y - draw.height/2 + draw.height * dna.size() / BipartiteGraph.initServices)); + graphics.setPaint(original); + +} + } diff --git a/diversim/src/main/java/diversim/Entity.java b/diversim/src/main/java/diversim/Entity.java index f2f823a..e3f3306 100644 --- a/diversim/src/main/java/diversim/Entity.java +++ b/diversim/src/main/java/diversim/Entity.java @@ -4,6 +4,7 @@ import java.util.Arrays; import java.util.Collections; +import diversim.strategy.matching.MatchingStrategy; import sim.engine.SimState; import sim.engine.Steppable; import sim.field.network.Edge; @@ -22,46 +23,58 @@ */ abstract public class Entity implements Steppable { -/** - * See BipartiteGraph.start(). - */ -int ID; - -/** - * All services hosted by the entity. - */ -ArrayList services; - -/** - * The number of link touching the entity in the bipartite graph. - */ -int degree; - - -Entity(int id) { - ID = id; - services = new ArrayList(); - degree = 0; -} - - -public int getDegree() { - return degree; -} - - -public int getSize() { - return services.size(); -} - - -public String getComposition() { - String res = ""; - for (Service s : services) { - res += s.ID + "-"; - } - return res; -} + /** + * See BipartiteGraph.start(). + */ + int ID; + + /** + * All services hosted by the entity. + */ + public ArrayList services; + + /** + * The number of link touching the entity in the bipartite graph. + */ + public int degree; + + /** + * The matching strategy employed by this entity + */ + MatchingStrategy matcher; + + + Entity(int id) { + ID = id; + services = new ArrayList(); + degree = 0; + } + + public boolean matches(Entity target){ + return this.matcher.matches(this, target); + } + + public void setMatchingStrategy(MatchingStrategy ms){ + this.matcher = ms; + } + + public int getDegree() { + return degree; + } + + + public int getSize() { + return services.size(); + } + + + public String getComposition() { + String res = ""; + for (Service s : services) { + res += s.ID + "-"; + } + return res; + } /** * This method is called at any scheduled step by the simulation engine @@ -125,7 +138,6 @@ public String toString() { String res = ""; res += this.getClass().getSimpleName() + " " + ID - + " : degree = " + degree + " ; size = " + getSize() + " ; composition = " + getComposition(); return res; diff --git a/diversim/src/main/java/diversim/Fate.java b/diversim/src/main/java/diversim/Fate.java index cf29345..760f353 100644 --- a/diversim/src/main/java/diversim/Fate.java +++ b/diversim/src/main/java/diversim/Fate.java @@ -17,8 +17,11 @@ * This agent can be used to inject in the simulation all the events that are * 'external' to the other entities. E.g.: arrival of new apps, failure of apps or services, * runtime change of some configuration parameter, etc. + * + * Now, as apps and platforms evolve themselves, fate is not really useful. * * @author Marco Biazzini + * @author Hui Song * */ public class Fate implements Steppable { diff --git a/diversim/src/main/java/diversim/Platform.java b/diversim/src/main/java/diversim/Platform.java index 0913962..3ed52ef 100644 --- a/diversim/src/main/java/diversim/Platform.java +++ b/diversim/src/main/java/diversim/Platform.java @@ -2,7 +2,13 @@ import java.util.ArrayList; import java.util.List; +import java.util.Set; +import java.util.HashSet; +import diversim.strategy.extinction.AppExtinctionStrategy; +import diversim.strategy.extinction.PlatformExtinctionStrategy; +import diversim.strategy.reproduction.PlatformReproductionStrategy; +import ec.util.MersenneTwisterFast; import sim.engine.SimState; import sim.util.Bag; import sim.field.network.*; @@ -15,127 +21,119 @@ * which should be updated accordingly. * * @author Marco Biazzini + * @author Vivek Nallur + * + * Platforms reproduce or kill themselves in the same way as Apps, controlled + * by the corresponding strategies stored in {@ #reproducers} and {@ #killers}, + * respectively. * */ public class Platform extends Entity { - -double pressure = 0; -String action; - - -public double getPressure() { - return pressure; -} - -public String getAction() { - return action; -} - - -public Platform(int id, List servs) { - super(id); - for (Service s : servs) { - BipartiteGraph.addUnique(services, s); - } - action = "none"; -} - - -/* - * (non-Javadoc) - * @see diversim.Entity#step(sim.engine.SimState) - */ -@Override -public void step(SimState state) { - BipartiteGraph graph = (BipartiteGraph)state; - - action = "none"; - if (degree > graph.platformMaxLoad) - if (getSize() >= 2 * graph.platformMinSize) { - split_Part(graph); - action = "split_part"; - } - else if (getSize() > graph.platformMinSize) { - clone_Mutate(graph); - action = "clone_mutate"; - } - pressure = ((double)degree) / graph.platformMaxLoad; - if (pressure > 1.0) pressure = 1.0; - - System.out.println("Step " + state.schedule.getSteps() + " : " + toString()); -} - - -/** - * Split this platform in two and partition the services, so that - * the most common services among the linked application are kept in this - * instance and the other half of the services are removed and assigned - * to the newly created platform. - * - * @param graph - */ -private void split_Part(BipartiteGraph graph) { - Bag out = graph.bipartiteNetwork.getEdges(this, null); // read-only! - Edge[] edges = (Edge[])out.toArray(new Edge[0]); - // get the services used by the apps, sorted from the most to the least common - ArrayList sortedServices = sortServices(out); - - // split the platform and keep here only the most shared half of the services - Platform p = graph.createPlatform( - sortedServices.subList(sortedServices.size() / 2, sortedServices.size())); - ArrayList ents = new ArrayList(); - for (Edge e : edges) { - ents.add((Entity)e.getOtherNode(this)); - } - graph.createLinks(p, ents); - System.out.println("Step " + graph.schedule.getSteps() + " : NEW " + p.toString()); - for (int i = sortedServices.size() / 2; i < sortedServices.size(); i++) { - services.remove(sortedServices.get(i)); - } - graph.updateLinks(this); -} - - -/** - * Clone this instance and mutate both this instance and the clone, so that - * one randomly chosen service in each instance (not the same in both) is removed. - * Then update the network so that the apps link to the proper instance(s). - * - * @param graph - */ -private void clone_Mutate(BipartiteGraph graph) { - int r1, r2; - Bag out = graph.bipartiteNetwork.getEdges(this, null); // read-only! - Edge[] edges = (Edge[])out.toArray(new Edge[0]); - ArrayList ents = new ArrayList(); - for (Edge e : edges) { - ents.add((Entity)e.getOtherNode(this)); - } - - r1 = graph.random.nextInt(services.size()); - do - r2 = graph.random.nextInt(services.size()); - while (r1 == r2); - - // generate a clone that has all the services of this platform but one. - ArrayList servs = new ArrayList(services); - servs.remove(r2); - Platform p = graph.createPlatform(servs); - graph.createLinks(p, ents); - - // remove a service from this platform - services.remove(r1); - graph.updateLinks(p); -} - - -@Override -public String toString() { - String res = super.toString(); - res += " ; pressure = " + pressure - + " ; action = " + action; - return res; -} - + // how many apps can one service on this platform support. + private int APP_PER_SERVICE = 1; + + public boolean dead = false; + + public List app = new ArrayList(); + + List reproducers; + List killers; + // ArrayList supportedServices; + + + public List reproduce(BipartiteGraph state){ + List result = new ArrayList(); + for(PlatformReproductionStrategy reproducer : reproducers){ + result.addAll(reproducer.reproduce(this, state)); + } + return result; + } + + public void setLoadingFactor(int load){ + this.APP_PER_SERVICE = load; + } + + public int getLoadingFactor(){ + return this.APP_PER_SERVICE; + } + + public List getSupportedServices(){ + return this.services; + } + + public void setSupportedServices(List other_services){ + this.services = new ArrayList(other_services); + } + + public void addSupportedServices(List other_services){ + Set current_services = new HashSet(this.services); + for (Service srv: other_services){ + current_services.add(srv); + } + this.services = new ArrayList (current_services); + } + + public Platform(int id, List servs) { + super(id); + this.services = new ArrayList (servs); + } + + public Platform(int id, List servs, int loading_factor) { + super(id); + this.services = new ArrayList (servs); + this.APP_PER_SERVICE = loading_factor; + } + + + /* + * (non-Javadoc) + * @see diversim.Entity#step(sim.engine.SimState) + */ + @Override + public void step(SimState state) { + + + BipartiteGraph graph = (BipartiteGraph)state; + + if(dieOrNot(graph)) + return; + + MersenneTwisterFast rnd = new MersenneTwisterFast(System.nanoTime()); + List pltfs = reproduce(graph); + for(Platform pltf : pltfs){ + graph.addPlatform(pltf); + } + + + System.out.println("Step " + state.schedule.getSteps() + " : " + toString()); + } + + + @Override + public String toString() { + String res = super.toString(); + return res; + } + + public void initStategies(BipartiteGraph graph){ + this.reproducers = StrategyFactory.fINSTANCE + .createPlatformReproductionStrategy(this, graph); + this.killers = StrategyFactory.fINSTANCE + .createPlatformExtinctionStrategies(this, graph); + } + + public boolean dieOrNot(BipartiteGraph graph){ + if(dead) + return true; + for(PlatformExtinctionStrategy killer : killers){ + if( killer.die(this, graph)){ + this.dead = true; + return true; + } + } + return false; + } + + } diff --git a/diversim/src/main/java/diversim/ReConnect.java b/diversim/src/main/java/diversim/ReConnect.java new file mode 100644 index 0000000..6fabe1a --- /dev/null +++ b/diversim/src/main/java/diversim/ReConnect.java @@ -0,0 +1,71 @@ +package diversim; + +import java.util.ArrayList; +import java.util.List; + +import diversim.strategy.matching.AllMatchingService; +import diversim.strategy.matching.MatchingStrategy; +import sim.engine.SimState; +import sim.engine.Steppable; + +/** + * + * ReConnect-ing happens before the re-drawing of the GUI, which remove dead + * Apps and Platforms, and calculate the links between them based on the matching + * Strategies. + * + * @author Hui Song + * + */ +public class ReConnect implements Steppable { + MatchingStrategy matcher = StrategyFactory.fINSTANCE.createMatchingStrategy(); + + @Override + public void step(SimState state) { + BipartiteGraph graph = (BipartiteGraph) state; + + List dead = new ArrayList(); + for(App a : graph.apps){ + if(a.dead){ + dead.add(a); + graph.bipartiteNetwork.removeNode(a); + + } + } + + graph.apps.removeAll(dead); + + List deadp = new ArrayList(); + for(Platform p : graph.platforms){ + if(p.dead){ + deadp.add(p); + graph.bipartiteNetwork.removeNode(p); + } + } + + graph.platforms.removeAll(deadp); + System.out.println(String.format("Step %d : apps: %d, platforms: %d", + graph.schedule.getSteps(), graph.apps.size(), graph.platforms.size())); + + + + + for(App app:graph.apps) + app.degree = 0; + for(Platform pltf : graph.platforms) + pltf.degree = 0; + + graph.bipartiteNetwork.removeAllEdges(); + + for(App app : graph.apps) + for(Platform pltf : graph.platforms) + if(matcher.matches(app, pltf)){ + graph.setLink(app, pltf); + } + + + } + + + +} diff --git a/diversim/src/main/java/diversim/Service.java b/diversim/src/main/java/diversim/Service.java index 5b7e60c..31b0c39 100644 --- a/diversim/src/main/java/diversim/Service.java +++ b/diversim/src/main/java/diversim/Service.java @@ -5,7 +5,15 @@ * (see the constructor of the entities, e.g. {@code Platform#Platform(int, java.util.List)}) * to optimize the access and speed up the comparison of the entities. * + * A service is just a label at this point (for the neutral model). It doesn't + * have any actual functionality. It only exists as a marker for something a + * Platform supports, and an App needs. Perhaps a biological analogy is a + * nutrient that is present in a particular plant/food source. Thus, a food + * source may have multiple types of nutrients. But, a nutrient in one food + * source is equivalent to the same nutrient in another food source. + * * @author Marco Biazzini + * @author Vivek Nallur * */ public class Service implements Comparable { diff --git a/diversim/src/main/java/diversim/StrategyFactory.java b/diversim/src/main/java/diversim/StrategyFactory.java new file mode 100644 index 0000000..098836b --- /dev/null +++ b/diversim/src/main/java/diversim/StrategyFactory.java @@ -0,0 +1,127 @@ +package diversim; + +import java.util.ArrayList; +import java.util.List; + +import diversim.strategy.extinction.AgingExtinctionStrategy; +import diversim.strategy.extinction.AppExtinctionStrategy; +import diversim.strategy.extinction.AppOrphanExtinctionStrategy; +import diversim.strategy.extinction.PlatformExtinctionStrategy; +import diversim.strategy.matching.AllMatchingService; +import diversim.strategy.matching.MatchingStrategy; +import diversim.strategy.reproduction.AppClonalReproduction; +import diversim.strategy.reproduction.AppReproductionStrategy; +import diversim.strategy.reproduction.AppReproductionWithPossibility; +import diversim.strategy.reproduction.AppSpeciationReproductionByDNA; +import diversim.strategy.reproduction.DNAExtensionSpeciation; +import diversim.strategy.reproduction.DNAReductionSpeciation; +import diversim.strategy.reproduction.PlatformReproductionStrategy; +import diversim.strategy.reproduction.PlatformReproductionWithPossibility; +import diversim.strategy.reproduction.PlatformSpeciationReproductionByDNA; +import ec.util.MersenneTwisterFast; + +/** + * + * A StrategyFactory is the nexus between entities and strategies. This is the only + * where you need to decide what strategies the apps or platforms need to follow. + * + * It is worth noticing that some of strategies are general, such as the current + * reproduction ones, whereas some others are specific to entity instances, such as + * the {@ AgingExtinctionStrategy} (because it need to record the birthday of each + * entity}. For the former one, it is encouraged to utilize static fields to record + * the instances produced before. + * + * @author Hui Song + * + */ +public class StrategyFactory { + + public final static int APP_MAX_LIFE = 100; + public final static int PLATFORM_MAX_LIFE = 23; + + public static StrategyFactory fINSTANCE = new StrategyFactory(); + + protected StrategyFactory(){ + + } + + //private static AppReproductionStrategy appReproductionStrategy = null; + /** + * Currently, reproduction strategy is general, so only one instance is enough + * @param graph + * @return + */ + public List createAppReproductionStrategy(App app, BipartiteGraph graph){ + + if(APP_REPRODUCTION_STRATEGIES != null) + return APP_REPRODUCTION_STRATEGIES; + + List result = new ArrayList(); + result.add(new AppReproductionWithPossibility(0.01, new AppClonalReproduction())); + result.add(new AppReproductionWithPossibility(0.01, + new AppSpeciationReproductionByDNA( + new DNAExtensionSpeciation() + ))); + result.add(new AppReproductionWithPossibility(0.01, + new AppSpeciationReproductionByDNA( + new DNAReductionSpeciation() + ))); + APP_REPRODUCTION_STRATEGIES = result; + return result; + } + private static List APP_REPRODUCTION_STRATEGIES = null; + + /** + * Now, the platforms always generate a "degenerated child"; + * @param allServices + * @return + */ + public List createPlatformReproductionStrategy(Platform platform, BipartiteGraph state){ + if(PLATFORM_REPRODUCTION_STRATEGIES != null) + return PLATFORM_REPRODUCTION_STRATEGIES; + + List result = new ArrayList(); + + + + PlatformReproductionStrategy reducer = new PlatformReproductionWithPossibility(0.1, + new PlatformSpeciationReproductionByDNA( + new DNAReductionSpeciation() + //new DNAExtensionSpeciation() + ) + ); + + //result.add(reducer); + + PLATFORM_REPRODUCTION_STRATEGIES = result; + return result; + + } + private static List PLATFORM_REPRODUCTION_STRATEGIES = null; + + public List createAppExtinctionStrategies(App app, BipartiteGraph graph){ + MersenneTwisterFast rnd = new MersenneTwisterFast(System.nanoTime()); + List killers = new ArrayList(); + killers.add(new AgingExtinctionStrategy(app, graph, APP_MAX_LIFE)); + killers.add(new AppOrphanExtinctionStrategy()); + return killers; + + } + + public List createPlatformExtinctionStrategies(Platform platform, BipartiteGraph graph){ + MersenneTwisterFast rnd = new MersenneTwisterFast(System.nanoTime()); + List killers = new ArrayList(); + + PlatformExtinctionStrategy lifekiller = new AgingExtinctionStrategy(platform, graph, PLATFORM_MAX_LIFE); + + //-killers.add(lifekiller); + + return killers; + } + + public MatchingStrategy createMatchingStrategy(){ + return new AllMatchingService(); + } + + +} diff --git a/diversim/src/main/java/diversim/TestingEntity.java b/diversim/src/main/java/diversim/TestingEntity.java new file mode 100644 index 0000000..2d6bc4e --- /dev/null +++ b/diversim/src/main/java/diversim/TestingEntity.java @@ -0,0 +1,157 @@ +//package diversim; +// +//import java.util.ArrayList; +//import java.util.List; +// +//import diversim.strategy.reproduction.AppClonalReproduction; +//import diversim.strategy.reproduction.AppReproductionStrategy; +//import diversim.strategy.reproduction.AppSpeciationReproductionByDNA; +//import diversim.strategy.reproduction.DNAExtensionSpeciation; +//import diversim.strategy.reproduction.PlatformClonalReproduction; +//import diversim.strategy.reproduction.PlatformReproductionStrategy; +//import diversim.strategy.reproduction.DNAReductionSpeciation; +//import diversim.strategy.reproduction.DNASpeciation; +//import sim.engine.SimState; +//import sim.util.Bag; +//import sim.field.network.*; +//// for testing only +//import ec.util.MersenneTwisterFast; +// +// +///** +// * TestingEntity is used to test any subclass of Entity to check if they support +// * the concrete implementations of all the interfaces/strategies that an Entity +// * is required to support +// * +// * @author Vivek Nallur +// * +// */ +//public class TestingEntity extends Entity { +// +// MatchingStrategy matcher; +// DNASpeciation mutator; +// ArrayList services; +// +// public TestingEntity(int id, List servs, MatchingStrategy ms) { +// super(id); +// this.matcher = ms; +// this.services = new ArrayList(servs); +// } +// +// public void addSpeciationStrategy(DNASpeciation ss){ +// this.mutator = ss; +// } +// +// +// /* +// * (non-Javadoc) +// * @see diversim.Entity#step(sim.engine.SimState) +// */ +// @Override +// public void step(SimState state) { +// BipartiteGraph graph = (BipartiteGraph)state; +// // do nothing right now +// } +// +// @Override +// public String toString() { +// String res = super.toString(); +// return res; +// } +// +// public void reproduce(List all_services){ +// MersenneTwisterFast rnd = new MersenneTwisterFast(System.nanoTime()); +// if (rnd.nextBoolean()){ +// System.out.println(this.getClass().getSimpleName() + "speciating using Reduction strategy"); +// this.addSpeciationStrategy(new DNAReductionSpeciation()); +// +// }else{ +// System.out.println(this.getClass().getSimpleName() + "speciating using Extension strategy"); +// this.addSpeciationStrategy(new DNAExtensionSpeciation()); +// } +// this.services = new ArrayList (this.mutator.speciate(this.services, all_services)); +// } +// +// public static void main(String[] args){ +// Service a = new Service(1); +// Service b = new Service(2); +// Service c = new Service(3); +// Service d = new Service(4); +// Service e = new Service(5); +// MatchingStrategy allMatcher = new AllMatchingService(); +// MatchingStrategy anyMatcher = new AnyMatchingService(); +// ArrayList allServices = new ArrayList(); +// allServices.add(a); +// allServices.add(b); +// allServices.add(c); +// allServices.add(d); +// allServices.add(e); +// ArrayList someServices = new ArrayList(); +// someServices.add(a); +// someServices.add(c); +// ArrayList diffServices = new ArrayList(); +// diffServices.add(b); +// diffServices.add(d); +// ArrayList moreServices = new ArrayList(); +// moreServices.add(e); +// moreServices.add(c); +// +// AppReproductionStrategy ars = new AppClonalReproduction(); +// AppSpeciationReproductionByDNA asr = new AppSpeciationReproductionByDNA(); +// +// asr.getStrategies().add(new DNAReductionSpeciation()); +// asr.getStrategies().add(new DNAExtensionSpeciation()); +// asr.setAllServices(allServices); +// PlatformReproductionStrategy prs = new PlatformClonalReproduction(); +// +// Platform platform = new Platform(10, allServices); +// platform.setMatchingStrategy(anyMatcher); +// platform.setReproductionStrategy(prs); +// +// App app = new App(20, someServices); +// app.setMatchingStrategy(allMatcher); +// app.setReproductionStrategy(ars); +// +// App app1 = new App(30, diffServices); +// app1.setMatchingStrategy(allMatcher); +// app1.setReproductionStrategy(asr); +// +// App app2 = new App(40, moreServices); +// app2.setMatchingStrategy(allMatcher); +// app2.setReproductionStrategy(asr); +// +// List all_apps = new ArrayList(3); +// all_apps.add(app); +// all_apps.add(app1); +// all_apps.add(app2); +// +// System.out.println("Platform matches app?: " + platform.matches(app)); +// System.out.println("App matches Platform?: " + app.matches(platform)); +// System.out.println("Platform always matches platform: " + platform.matches(platform)); +// System.out.println("App always matches app: " + app.matches(app)); +// +// List child_apps = app.reproduce(all_apps); +// List child_platforms = platform.reproduce(null); +// +// for (App child: child_apps){ +// System.out.println(child); +// } +// for (Platform child: child_platforms){ +// System.out.println(child); +// } +// +// List children_app1 = app1.reproduce(all_apps); +// List children_app2 = app2.reproduce(all_apps); +// +// System.out.println("-- now comes app1"); +// for (App child: children_app1){ +// System.out.println(child); +// } +// +// for (App child: children_app2){ +// System.out.println(child); +// } +// +// } +// +//} diff --git a/diversim/src/main/java/diversim/strategy/extinction/AgingExtinctionStrategy.java b/diversim/src/main/java/diversim/strategy/extinction/AgingExtinctionStrategy.java new file mode 100644 index 0000000..fe68012 --- /dev/null +++ b/diversim/src/main/java/diversim/strategy/extinction/AgingExtinctionStrategy.java @@ -0,0 +1,46 @@ +package diversim.strategy.extinction; + +import diversim.App; +import diversim.BipartiteGraph; +import diversim.Entity; +import diversim.Platform; + +public class AgingExtinctionStrategy implements AppExtinctionStrategy, PlatformExtinctionStrategy { + + private Entity entity = null; + private int expectedAge = 0; + private long born = 0; + + public AgingExtinctionStrategy(Entity entity, BipartiteGraph graph, int expectedAge){ + + this.born = graph.schedule.getSteps(); + this.expectedAge = expectedAge; + this.entity = entity; + + } + + + public boolean die(BipartiteGraph graph) { + long steps = graph.schedule.getSteps(); + if( steps - born >= expectedAge){ + return true; + } + else + return false; + + } + + + @Override + public boolean die(Platform platform, BipartiteGraph graph) { + return die(graph); + } + + + @Override + public boolean die(App app, BipartiteGraph graph) { + // TODO Auto-generated method stub + return die(graph); + } + +} diff --git a/diversim/src/main/java/diversim/strategy/extinction/AppExtinctionStrategy.java b/diversim/src/main/java/diversim/strategy/extinction/AppExtinctionStrategy.java new file mode 100644 index 0000000..c9a54de --- /dev/null +++ b/diversim/src/main/java/diversim/strategy/extinction/AppExtinctionStrategy.java @@ -0,0 +1,9 @@ +package diversim.strategy.extinction; + +import diversim.App; +import diversim.BipartiteGraph; +import diversim.Entity; + +public interface AppExtinctionStrategy { + public boolean die(App app, BipartiteGraph graph); +} diff --git a/diversim/src/main/java/diversim/strategy/extinction/AppOrphanExtinctionStrategy.java b/diversim/src/main/java/diversim/strategy/extinction/AppOrphanExtinctionStrategy.java new file mode 100644 index 0000000..8dd41e0 --- /dev/null +++ b/diversim/src/main/java/diversim/strategy/extinction/AppOrphanExtinctionStrategy.java @@ -0,0 +1,19 @@ +package diversim.strategy.extinction; + +import diversim.App; +import diversim.BipartiteGraph; +import diversim.Entity; + +public class AppOrphanExtinctionStrategy implements AppExtinctionStrategy { + + + @Override + public boolean die(App app, BipartiteGraph graph) { + if(app.services.size() == 0) + return true; + if(app.degree == 0) + return true; + return false; + } + +} diff --git a/diversim/src/main/java/diversim/strategy/extinction/PlatformExtinctionStrategy.java b/diversim/src/main/java/diversim/strategy/extinction/PlatformExtinctionStrategy.java new file mode 100644 index 0000000..3106420 --- /dev/null +++ b/diversim/src/main/java/diversim/strategy/extinction/PlatformExtinctionStrategy.java @@ -0,0 +1,10 @@ +package diversim.strategy.extinction; + +import diversim.App; +import diversim.BipartiteGraph; +import diversim.Entity; +import diversim.Platform; + +public interface PlatformExtinctionStrategy { + public boolean die(Platform platform, BipartiteGraph graph); +} diff --git a/diversim/src/main/java/diversim/strategy/matching/AllMatchingService.java b/diversim/src/main/java/diversim/strategy/matching/AllMatchingService.java new file mode 100644 index 0000000..d34c3b0 --- /dev/null +++ b/diversim/src/main/java/diversim/strategy/matching/AllMatchingService.java @@ -0,0 +1,23 @@ +package diversim.strategy.matching; + +import java.util.*; + +import diversim.Entity; +import diversim.Service; + +/** + * An implementation of the MatchingStrategy interface, this concrete + * implementation returns true only if all of the services required by the + * source, are found in the target. That is, if source is a subset of target. + * + * @author Vivek Nallur + */ +public class AllMatchingService implements MatchingStrategy{ + public boolean matches(Entity source, Entity target){ + Set target_services = new HashSet(target.services); + if (target_services.containsAll(new HashSet(source.services))){ + return true; + } + return false; + } +} diff --git a/diversim/src/main/java/diversim/strategy/matching/AnyMatchingService.java b/diversim/src/main/java/diversim/strategy/matching/AnyMatchingService.java new file mode 100644 index 0000000..d5427df --- /dev/null +++ b/diversim/src/main/java/diversim/strategy/matching/AnyMatchingService.java @@ -0,0 +1,40 @@ +package diversim.strategy.matching; + +import java.util.*; + +import diversim.Entity; +import diversim.Service; + +/** + * An implementation of the MatchingStrategy interface, this concrete + * implementation returns true, if any of the services in the source, + * are found in the target. + * + * @author Vivek Nallur + */ +public class AnyMatchingService implements MatchingStrategy{ + public boolean matches(Entity source, Entity target){ + Set all_services = new HashSet(source.services); + /* + * Should be inside a logger + + System.out.println("Services in entity " + source.ID + "are: "); + for (Service s: all_services){ + System.out.println(s.ID); + } + */ + // Now try to add services from the target to all_services + // If there's a single failure, then at least one service from + // the target matches one service in the source + + for (Service srv: target.services){ + if(all_services.add(srv)){ +// System.out.println("Did not find service: " + srv.ID); + }else{ +// System.out.println("Already had service: " + srv.ID); + return true; + } + } + return false; + } +} diff --git a/diversim/src/main/java/diversim/strategy/matching/MatchingStrategy.java b/diversim/src/main/java/diversim/strategy/matching/MatchingStrategy.java new file mode 100644 index 0000000..3301803 --- /dev/null +++ b/diversim/src/main/java/diversim/strategy/matching/MatchingStrategy.java @@ -0,0 +1,18 @@ +package diversim.strategy.matching; + +import diversim.Entity; + +/** + * This interface defines the basic method for determining whether an entity + * matches another. A concrete sub-class is expected to define a particular + * context in which one entity decides that another entity provides what it + * requires. For example, an application may decide that a platform 'matches', + * if it provides at least one of the services required by the application. Or + * it may decide that a platform only matches if it provides *all* the services + * required by the application. This, obviously, is dependent on the concrete + * implementation of the strategy. + */ + +public interface MatchingStrategy{ + public boolean matches(Entity source, Entity target); +} diff --git a/diversim/src/main/java/diversim/strategy/reproduction/AppClonalReproduction.java b/diversim/src/main/java/diversim/strategy/reproduction/AppClonalReproduction.java new file mode 100644 index 0000000..6c1c720 --- /dev/null +++ b/diversim/src/main/java/diversim/strategy/reproduction/AppClonalReproduction.java @@ -0,0 +1,29 @@ +package diversim.strategy.reproduction; + +import java.util.List; +import java.util.ArrayList; + +import diversim.App; +import diversim.BipartiteGraph; +import ec.util.MersenneTwisterFast; + +/** + * This class implements the clonal reproduction strategy + * i.e., the App child that is created is exactly the same as the parent App + * This strategy ignores the second parameter, and merely concerns itself with + * the parent. Also, it returns only one child, as a clone. + * + * @author Vivek Nallur + */ +public class AppClonalReproduction implements AppReproductionStrategy{ + + @Override + public List reproduce(App parent, BipartiteGraph state) { + MersenneTwisterFast rnd = new MersenneTwisterFast(System.nanoTime()); + App child = new App(rnd.nextInt(), parent.getDependencies()); + child.initStrategies(state); + ArrayList children = new ArrayList(); + children.add(child); + return children; + } +} diff --git a/diversim/src/main/java/diversim/strategy/reproduction/AppReproductionStrategy.java b/diversim/src/main/java/diversim/strategy/reproduction/AppReproductionStrategy.java new file mode 100644 index 0000000..8887100 --- /dev/null +++ b/diversim/src/main/java/diversim/strategy/reproduction/AppReproductionStrategy.java @@ -0,0 +1,15 @@ +package diversim.strategy.reproduction; + +import java.util.List; + +import diversim.App; +import diversim.BipartiteGraph; + +/** + * This is the interface that all App reproduction strategies must implement + * @author Vivek Nallur + * @author Hui Song + */ +public interface AppReproductionStrategy{ + public List reproduce(App parent, BipartiteGraph state); +} diff --git a/diversim/src/main/java/diversim/strategy/reproduction/AppReproductionWithPossibility.java b/diversim/src/main/java/diversim/strategy/reproduction/AppReproductionWithPossibility.java new file mode 100644 index 0000000..a7fa0a1 --- /dev/null +++ b/diversim/src/main/java/diversim/strategy/reproduction/AppReproductionWithPossibility.java @@ -0,0 +1,29 @@ +package diversim.strategy.reproduction; + +import java.util.Collections; +import java.util.List; + +import diversim.App; +import diversim.BipartiteGraph; +import ec.util.MersenneTwisterFast; + +public class AppReproductionWithPossibility implements AppReproductionStrategy{ + + private double possibility = 0; + private AppReproductionStrategy reproducer = null; + + public AppReproductionWithPossibility(double possiblility, AppReproductionStrategy reproducer){ + this.possibility = possiblility; + this.reproducer = reproducer; + } + + @Override + public List reproduce(App parent, BipartiteGraph state) { + MersenneTwisterFast rnd = new MersenneTwisterFast(System.nanoTime()); + if(rnd.nextDouble() < possibility){ + return reproducer.reproduce(parent, state); + } + else + return Collections.emptyList(); + } +} diff --git a/diversim/src/main/java/diversim/strategy/reproduction/AppSpeciationReproductionByDNA.java b/diversim/src/main/java/diversim/strategy/reproduction/AppSpeciationReproductionByDNA.java new file mode 100644 index 0000000..ae5ec56 --- /dev/null +++ b/diversim/src/main/java/diversim/strategy/reproduction/AppSpeciationReproductionByDNA.java @@ -0,0 +1,43 @@ +package diversim.strategy.reproduction; + +import java.util.ArrayList; +import java.util.List; + +import diversim.App; +import diversim.BipartiteGraph; +import ec.util.MersenneTwisterFast; + + +/** + * Randomly choose one of the registered speciation strategies + * to change its dna + * + * @author hui song + * + */ + +public class AppSpeciationReproductionByDNA implements AppReproductionStrategy { + + DNASpeciation speciator = null; + + public AppSpeciationReproductionByDNA(DNASpeciation speciator){ + this.speciator = speciator; + } + + + public List reproduce(App parent, BipartiteGraph state) { + MersenneTwisterFast rnd = new MersenneTwisterFast(System.nanoTime()); + + App child = new App(rnd.nextInt(), + this.speciator.speciate(parent.getDependencies(), state.services) + ); + + child.initStrategies(state); + ArrayList children = new ArrayList(); + children.add(child); + return children; + } + + + +} diff --git a/diversim/src/main/java/diversim/strategy/reproduction/DNAExtensionSpeciation.java b/diversim/src/main/java/diversim/strategy/reproduction/DNAExtensionSpeciation.java new file mode 100644 index 0000000..180135e --- /dev/null +++ b/diversim/src/main/java/diversim/strategy/reproduction/DNAExtensionSpeciation.java @@ -0,0 +1,29 @@ +package diversim.strategy.reproduction; + +import java.util.List; +import java.util.ArrayList; +import java.util.Set; +import java.util.HashSet; + +import diversim.BipartiteGraph; +import diversim.Service; +import ec.util.MersenneTwisterFast; +import sim.util.Bag; +/** + * This class implements the Extension speciation strategy + * @author Vivek Nallur + */ + +public class DNAExtensionSpeciation implements DNASpeciation{ + public List speciate(List current_dna, List all_services){ + if(current_dna.size() == BipartiteGraph.initServices) + return new ArrayList (current_dna); + Set current_services = new HashSet(current_dna); + Service new_service; + MersenneTwisterFast rnd = new MersenneTwisterFast(System.nanoTime()); + do { + new_service = all_services.get(rnd.nextInt(all_services.size())); + }while(!current_services.add(new_service)); + return new ArrayList (current_services); + } +} diff --git a/diversim/src/main/java/diversim/strategy/reproduction/DNAReductionSpeciation.java b/diversim/src/main/java/diversim/strategy/reproduction/DNAReductionSpeciation.java new file mode 100644 index 0000000..e25390e --- /dev/null +++ b/diversim/src/main/java/diversim/strategy/reproduction/DNAReductionSpeciation.java @@ -0,0 +1,21 @@ +package diversim.strategy.reproduction; + +import java.util.List; +import java.util.ArrayList; + +import diversim.Service; +import ec.util.MersenneTwisterFast; +import sim.util.Bag; +/** + * This class implements the Reduction speciation strategy + * @author Vivek Nallur + */ + +public class DNAReductionSpeciation implements DNASpeciation{ + public List speciate(List current_dna, List all_services){ + ArrayList current_services = new ArrayList (current_dna); + MersenneTwisterFast rnd = new MersenneTwisterFast(System.nanoTime()); + current_services.remove(rnd.nextInt(current_services.size())); + return new ArrayList (current_services); + } +} diff --git a/diversim/src/main/java/diversim/strategy/reproduction/DNASpeciation.java b/diversim/src/main/java/diversim/strategy/reproduction/DNASpeciation.java new file mode 100644 index 0000000..b429fc5 --- /dev/null +++ b/diversim/src/main/java/diversim/strategy/reproduction/DNASpeciation.java @@ -0,0 +1,16 @@ +package diversim.strategy.reproduction; + +import java.util.List; + +import diversim.Service; + +/** + * + * @author Vivek Nallur + * + * This is the base interface for all speciation strategies + */ +public interface DNASpeciation{ + public List speciate(List current_dna, List all_services); +} + diff --git a/diversim/src/main/java/diversim/strategy/reproduction/PlatformClonalReproduction.java b/diversim/src/main/java/diversim/strategy/reproduction/PlatformClonalReproduction.java new file mode 100644 index 0000000..5d82fe8 --- /dev/null +++ b/diversim/src/main/java/diversim/strategy/reproduction/PlatformClonalReproduction.java @@ -0,0 +1,24 @@ +package diversim.strategy.reproduction; + +import java.util.List; +import java.util.ArrayList; + +import diversim.BipartiteGraph; +import diversim.Platform; +import ec.util.MersenneTwisterFast; + +/** + * This class implements the ReproductionStrategy with clonal reproduction + * i.e., the Platform child that is created is exactly the same as the parent Platform. Also, this strategy results in only one child. + * @author Vivek Nallur + */ +public class PlatformClonalReproduction implements PlatformReproductionStrategy{ + public List reproduce(Platform parent, BipartiteGraph state){ + MersenneTwisterFast rnd = new MersenneTwisterFast(System.nanoTime()); + Platform child = new Platform(rnd.nextInt(), parent.getSupportedServices()); + child.setLoadingFactor(parent.getLoadingFactor()); + ArrayList children = new ArrayList(); + children.add(child); + return children; + } +} diff --git a/diversim/src/main/java/diversim/strategy/reproduction/PlatformReproductionStrategy.java b/diversim/src/main/java/diversim/strategy/reproduction/PlatformReproductionStrategy.java new file mode 100644 index 0000000..c898383 --- /dev/null +++ b/diversim/src/main/java/diversim/strategy/reproduction/PlatformReproductionStrategy.java @@ -0,0 +1,15 @@ +package diversim.strategy.reproduction; + +import java.util.List; + +import diversim.BipartiteGraph; +import diversim.Platform; +import ec.util.MersenneTwisterFast; + +/** + * This interface must be implemented by all reproduction strategies of Platform + * @author Vivek Nallur + */ +public interface PlatformReproductionStrategy{ + public List reproduce(Platform parent, BipartiteGraph state); +} diff --git a/diversim/src/main/java/diversim/strategy/reproduction/PlatformReproductionWithPossibility.java b/diversim/src/main/java/diversim/strategy/reproduction/PlatformReproductionWithPossibility.java new file mode 100644 index 0000000..a7f4f8e --- /dev/null +++ b/diversim/src/main/java/diversim/strategy/reproduction/PlatformReproductionWithPossibility.java @@ -0,0 +1,30 @@ +package diversim.strategy.reproduction; + +import java.util.Collections; +import java.util.List; + +import diversim.App; +import diversim.BipartiteGraph; +import diversim.Platform; +import ec.util.MersenneTwisterFast; + +public class PlatformReproductionWithPossibility implements PlatformReproductionStrategy{ + + private double possibility = 0; + private PlatformReproductionStrategy reproducer = null; + + public PlatformReproductionWithPossibility(double possiblility, PlatformReproductionStrategy reproducer){ + this.possibility = possiblility; + this.reproducer = reproducer; + } + + @Override + public List reproduce(Platform parent, BipartiteGraph state) { + MersenneTwisterFast rnd = new MersenneTwisterFast(System.nanoTime()); + if(rnd.nextDouble() < possibility){ + return reproducer.reproduce(parent, state); + } + else + return Collections.emptyList(); + } +} diff --git a/diversim/src/main/java/diversim/strategy/reproduction/PlatformSpeciationReproductionByDNA.java b/diversim/src/main/java/diversim/strategy/reproduction/PlatformSpeciationReproductionByDNA.java new file mode 100644 index 0000000..efd1107 --- /dev/null +++ b/diversim/src/main/java/diversim/strategy/reproduction/PlatformSpeciationReproductionByDNA.java @@ -0,0 +1,33 @@ +package diversim.strategy.reproduction; + +import java.util.ArrayList; +import java.util.List; + +import diversim.BipartiteGraph; +import diversim.Platform; +import diversim.Service; +import ec.util.MersenneTwisterFast; + +public class PlatformSpeciationReproductionByDNA + implements PlatformReproductionStrategy { + + private DNASpeciation speciator; + + public PlatformSpeciationReproductionByDNA(DNASpeciation speciator){ + this.speciator = speciator; + } + + @Override + public List reproduce(Platform parent, + BipartiteGraph state) { + // TODO Auto-generated method stub + MersenneTwisterFast rnd = new MersenneTwisterFast(System.nanoTime()); + List services = speciator.speciate(parent.services, state.services); + List children = new ArrayList(); + Platform pltf = new Platform(rnd.nextInt(), services, parent.getLoadingFactor()); + pltf.initStategies(state); + children.add(pltf); + return children; + } + +} diff --git a/diversim/src/main/java/diversim/strategy/reproduction/ReproductionStrategy.java b/diversim/src/main/java/diversim/strategy/reproduction/ReproductionStrategy.java new file mode 100644 index 0000000..d39b509 --- /dev/null +++ b/diversim/src/main/java/diversim/strategy/reproduction/ReproductionStrategy.java @@ -0,0 +1,11 @@ +package diversim.strategy.reproduction; + +import java.util.List; + +import diversim.BipartiteGraph; +import diversim.Entity; +import diversim.Service; + +public interface ReproductionStrategy { + public List reproduce(Entity parent, BipartiteGraph state); +} diff --git a/gitout.txt b/gitout.txt new file mode 100644 index 0000000..e69de29