From d7f7658062bd3927ff7d1ae5ee998f3f5446d7f3 Mon Sep 17 00:00:00 2001 From: Vivek Nallur Date: Fri, 2 Aug 2013 15:27:05 +0100 Subject: [PATCH 01/11] First commit to neutral_model: Only path inside starting shell script changed. Just to check if everything is still working after fork-n-branch --- diversim/diversim.sh | 5 +++-- 1 file changed, 3 insertions(+), 2 deletions(-) diff --git a/diversim/diversim.sh b/diversim/diversim.sh index 3c43c93..7617128 100644 --- a/diversim/diversim.sh +++ b/diversim/diversim.sh @@ -1,7 +1,8 @@ #!/bin/bash -binDir=$HOME/workspace/diversim/bin # where are the binaries -libDir=$HOME/Diversify/mason # where are the libs +binDir=$HOME/ownCloud/neutral_model/diversify/diversim/src/main/java # where +are the binaries +libDir=$HOME/ownCloud/tcd/mason # where are the libs 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 From d5e02ef42262bb18c714cc70ea65bdf8b655d785 Mon Sep 17 00:00:00 2001 From: Vivek Nallur Date: Fri, 2 Aug 2013 17:37:46 +0100 Subject: [PATCH 02/11] First commit on neutral model. Lots to go. Nothing much added. Only modified App to reproduce --- diversim/diversim.sh | 10 ++-- diversim/src/main/java/diversim/App.java | 52 ++++++++++++++++--- .../main/java/diversim/BipartiteGraph.java | 19 +++++++ diversim/src/main/java/diversim/Service.java | 8 +++ 4 files changed, 77 insertions(+), 12 deletions(-) diff --git a/diversim/diversim.sh b/diversim/diversim.sh index 7617128..546c492 100644 --- a/diversim/diversim.sh +++ b/diversim/diversim.sh @@ -1,12 +1,14 @@ #!/bin/bash -binDir=$HOME/ownCloud/neutral_model/diversify/diversim/src/main/java # where -are the binaries -libDir=$HOME/ownCloud/tcd/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..dc1be48 100644 --- a/diversim/src/main/java/diversim/App.java +++ b/diversim/src/main/java/diversim/App.java @@ -2,13 +2,15 @@ import java.util.List; +import java.util.HashSet; +import java.util.Set; // Interface 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, and if an App cannot find a + * Platform that provides at least these services, then it dies. + * @author Vivek Nallur * */ public class App extends Entity { @@ -20,18 +22,52 @@ public double getRedundancy() { return redundancy; } +List dependencies; + +public List getDependencies(){ + return this.dependencies; +} + +public void addDependencies(List new_deps){ + Set all_services = new HashSet(this.dependencies); + all_services.addAll(new_deps); + this.dependencies.clear(); + this.dependencies.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 -public App(int id, List servs) { - super(id); - for (Service s : servs) { - BipartiteGraph.addUnique(services, s); - } + this.dependencies.removeAll(new HashSet(obs_deps)); +} + + +public App(int id, List service_dependencies) { + super(id); + this.dependencies = service_dependencies; +} + +/** + * This method should really be called clone, since reproduction in this case + * is really just cloning of the object properties. But, we don't want to clash + * with java's own clone method inside Object + * + */ +public App reproduce(){ + App child = new App(this.ID, this.dependencies); + return child; } /* * (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) { diff --git a/diversim/src/main/java/diversim/BipartiteGraph.java b/diversim/src/main/java/diversim/BipartiteGraph.java index 8ee4b0b..c5b7a4f 100644 --- a/diversim/src/main/java/diversim/BipartiteGraph.java +++ b/diversim/src/main/java/diversim/BipartiteGraph.java @@ -24,7 +24,26 @@ * 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 * */ public class BipartiteGraph extends SimState { 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 { From d1e3da724da35d0d457d06015f9565ca858519e0 Mon Sep 17 00:00:00 2001 From: Vivek Nallur Date: Thu, 8 Aug 2013 17:28:27 +0100 Subject: [PATCH 03/11] Added matching strategies --- .../java/diversim/AllMatchingService.java | 20 ++++++++++++++ .../java/diversim/AnyMatchingService.java | 26 +++++++++++++++++++ .../java/diversim/ExtensionSpeciation.java | 9 +++++++ .../java/diversim/ExtinctionStrategy.java | 7 +++++ .../main/java/diversim/MatchingStrategy.java | 16 ++++++++++++ .../java/diversim/ReductionSpeciation.java | 9 +++++++ .../java/diversim/ReproductionStrategy.java | 7 +++++ .../java/diversim/SpeciationStrategy.java | 10 +++++++ 8 files changed, 104 insertions(+) create mode 100644 diversim/src/main/java/diversim/AllMatchingService.java create mode 100644 diversim/src/main/java/diversim/AnyMatchingService.java create mode 100644 diversim/src/main/java/diversim/ExtensionSpeciation.java create mode 100644 diversim/src/main/java/diversim/ExtinctionStrategy.java create mode 100644 diversim/src/main/java/diversim/MatchingStrategy.java create mode 100644 diversim/src/main/java/diversim/ReductionSpeciation.java create mode 100644 diversim/src/main/java/diversim/ReproductionStrategy.java create mode 100644 diversim/src/main/java/diversim/SpeciationStrategy.java diff --git a/diversim/src/main/java/diversim/AllMatchingService.java b/diversim/src/main/java/diversim/AllMatchingService.java new file mode 100644 index 0000000..582827a --- /dev/null +++ b/diversim/src/main/java/diversim/AllMatchingService.java @@ -0,0 +1,20 @@ +package diversim; + +import java.util.*; + +/** + * 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/AnyMatchingService.java b/diversim/src/main/java/diversim/AnyMatchingService.java new file mode 100644 index 0000000..33a6a16 --- /dev/null +++ b/diversim/src/main/java/diversim/AnyMatchingService.java @@ -0,0 +1,26 @@ +package diversim; + +import java.util.*; + +/** + * An implementation of the MatchingStrategy interface, this concrete + * implementation returns true, if any of the services required by 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); + // 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)){ + return true; + } + } + return false; + } +} diff --git a/diversim/src/main/java/diversim/ExtensionSpeciation.java b/diversim/src/main/java/diversim/ExtensionSpeciation.java new file mode 100644 index 0000000..d199546 --- /dev/null +++ b/diversim/src/main/java/diversim/ExtensionSpeciation.java @@ -0,0 +1,9 @@ +/** + * This class implements the Extension speciation strategy + * @author Vivek Nallur + */ + +public class ExtensionSpeciation implements SpeciationStrategy{ + public void speciate(){ + } +} diff --git a/diversim/src/main/java/diversim/ExtinctionStrategy.java b/diversim/src/main/java/diversim/ExtinctionStrategy.java new file mode 100644 index 0000000..490db47 --- /dev/null +++ b/diversim/src/main/java/diversim/ExtinctionStrategy.java @@ -0,0 +1,7 @@ +/** + * The interface for extinction strategies + * @author Vivek Nallur + */ +public interface ExtinctionStrategy { + public void die(); +} diff --git a/diversim/src/main/java/diversim/MatchingStrategy.java b/diversim/src/main/java/diversim/MatchingStrategy.java new file mode 100644 index 0000000..338e721 --- /dev/null +++ b/diversim/src/main/java/diversim/MatchingStrategy.java @@ -0,0 +1,16 @@ +package diversim; + +/** + * 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/ReductionSpeciation.java b/diversim/src/main/java/diversim/ReductionSpeciation.java new file mode 100644 index 0000000..008790d --- /dev/null +++ b/diversim/src/main/java/diversim/ReductionSpeciation.java @@ -0,0 +1,9 @@ +/** + * This class implements the Reduction speciation strategy + * @author Vivek Nallur + */ + +public class ReductionSpeciation implements SpeciationStrategy{ + public void speciate(){ + } +} diff --git a/diversim/src/main/java/diversim/ReproductionStrategy.java b/diversim/src/main/java/diversim/ReproductionStrategy.java new file mode 100644 index 0000000..ee66635 --- /dev/null +++ b/diversim/src/main/java/diversim/ReproductionStrategy.java @@ -0,0 +1,7 @@ +/** + * The interface for reproduction strategies + * @author Vivek Nallur + */ +public interface ReproductionStrategy{ + public void reproduce(); +} diff --git a/diversim/src/main/java/diversim/SpeciationStrategy.java b/diversim/src/main/java/diversim/SpeciationStrategy.java new file mode 100644 index 0000000..a84863e --- /dev/null +++ b/diversim/src/main/java/diversim/SpeciationStrategy.java @@ -0,0 +1,10 @@ +/** + * + * @author Vivek Nallur + * + * This is the base interface for all speciation strategies + */ +public interface SpeciationStrategy{ + public void speciate(); +} + From 63e53cb691fce58a4b2f8d4b4d2d460314611b5c Mon Sep 17 00:00:00 2001 From: Vivek Nallur Date: Mon, 19 Aug 2013 15:52:25 +0100 Subject: [PATCH 04/11] Implemented Speciation as a strategy --- .../main/java/diversim/AnyMatchingService.java | 2 +- diversim/src/main/java/diversim/Entity.java | 1 - .../java/diversim/ExtensionSpeciation.java | 18 +++++++++++++++++- .../java/diversim/ReductionSpeciation.java | 13 ++++++++++++- .../main/java/diversim/SpeciationStrategy.java | 6 +++++- 5 files changed, 35 insertions(+), 5 deletions(-) diff --git a/diversim/src/main/java/diversim/AnyMatchingService.java b/diversim/src/main/java/diversim/AnyMatchingService.java index 33a6a16..20f6b0a 100644 --- a/diversim/src/main/java/diversim/AnyMatchingService.java +++ b/diversim/src/main/java/diversim/AnyMatchingService.java @@ -4,7 +4,7 @@ /** * An implementation of the MatchingStrategy interface, this concrete - * implementation returns true, if any of the services required by the source, + * implementation returns true, if any of the services in the source, * are found in the target. * * @author Vivek Nallur diff --git a/diversim/src/main/java/diversim/Entity.java b/diversim/src/main/java/diversim/Entity.java index f2f823a..d09b9c2 100644 --- a/diversim/src/main/java/diversim/Entity.java +++ b/diversim/src/main/java/diversim/Entity.java @@ -125,7 +125,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/ExtensionSpeciation.java b/diversim/src/main/java/diversim/ExtensionSpeciation.java index d199546..f0f1b95 100644 --- a/diversim/src/main/java/diversim/ExtensionSpeciation.java +++ b/diversim/src/main/java/diversim/ExtensionSpeciation.java @@ -1,9 +1,25 @@ +package diversim; + +import java.util.List; +import java.util.ArrayList; +import java.util.Set; +import java.util.HashSet; + +import ec.util.MersenneTwisterFast; +import sim.util.Bag; /** * This class implements the Extension speciation strategy * @author Vivek Nallur */ public class ExtensionSpeciation implements SpeciationStrategy{ - public void speciate(){ + public List speciate(List current_dna, List all_services){ + Set current_services = new HashSet(current_dna); + Service new_service; + MersenneTwisterFast rnd = new MersenneTwisterFast(); + 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/ReductionSpeciation.java b/diversim/src/main/java/diversim/ReductionSpeciation.java index 008790d..a37d03e 100644 --- a/diversim/src/main/java/diversim/ReductionSpeciation.java +++ b/diversim/src/main/java/diversim/ReductionSpeciation.java @@ -1,9 +1,20 @@ +package diversim; + +import java.util.List; +import java.util.ArrayList; + +import ec.util.MersenneTwisterFast; +import sim.util.Bag; /** * This class implements the Reduction speciation strategy * @author Vivek Nallur */ public class ReductionSpeciation implements SpeciationStrategy{ - public void speciate(){ + public List speciate(List current_dna, List all_services){ + ArrayList current_services = new ArrayList (current_dna); + MersenneTwisterFast rnd = new MersenneTwisterFast(); + current_services.remove(rnd.nextInt(current_services.size())); + return new ArrayList (current_services); } } diff --git a/diversim/src/main/java/diversim/SpeciationStrategy.java b/diversim/src/main/java/diversim/SpeciationStrategy.java index a84863e..ac6ef72 100644 --- a/diversim/src/main/java/diversim/SpeciationStrategy.java +++ b/diversim/src/main/java/diversim/SpeciationStrategy.java @@ -1,3 +1,7 @@ +package diversim; + +import java.util.List; + /** * * @author Vivek Nallur @@ -5,6 +9,6 @@ * This is the base interface for all speciation strategies */ public interface SpeciationStrategy{ - public void speciate(); + public List speciate(List current_dna, List all_services); } From 02d2d14054072ace56fe0575807ee460317f9cf0 Mon Sep 17 00:00:00 2001 From: Vivek Nallur Date: Mon, 19 Aug 2013 15:57:08 +0100 Subject: [PATCH 05/11] Added TestingEntity, so that small changes can be tested easily --- .../src/main/java/diversim/TestingEntity.java | 107 ++++++++++++++++++ 1 file changed, 107 insertions(+) create mode 100644 diversim/src/main/java/diversim/TestingEntity.java diff --git a/diversim/src/main/java/diversim/TestingEntity.java b/diversim/src/main/java/diversim/TestingEntity.java new file mode 100644 index 0000000..177a07e --- /dev/null +++ b/diversim/src/main/java/diversim/TestingEntity.java @@ -0,0 +1,107 @@ +package diversim; + +import java.util.ArrayList; +import java.util.List; + +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; + SpeciationStrategy mutator; + + public TestingEntity(int id, List servs, MatchingStrategy ms) { + super(id); + for (Service s : servs) { + BipartiteGraph.addUnique(services, s); + } + this.matcher = ms; + } + + public boolean matches(Entity target){ + return this.matcher.matches(this, target); + } + + public void addSpeciationStrategy(SpeciationStrategy 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(); + if (rnd.nextBoolean()){ + System.out.println(this.getClass().getSimpleName() + "speciating using Reduction strategy"); + this.addSpeciationStrategy(new ReductionSpeciation()); + + }else{ + System.out.println(this.getClass().getSimpleName() + "speciating using Extension strategy"); + this.addSpeciationStrategy(new ExtensionSpeciation()); + } + 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); + MatchingStrategy allMatcher = new AllMatchingService(); + MatchingStrategy anyMatcher = new AnyMatchingService(); + ArrayList allServices = new ArrayList(); + allServices.add(a); + allServices.add(b); + allServices.add(c); + ArrayList someServices = new ArrayList(); + someServices.add(a); + someServices.add(c); + ArrayList diffServices = new ArrayList(); + diffServices.add(b); + diffServices.add(a); + + TestingEntity platform = new TestingEntity(10, allServices, anyMatcher); + TestingEntity app = new TestingEntity(20, someServices, allMatcher); + 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)); + + TestingEntity another_app = new TestingEntity(30, someServices, allMatcher); + TestingEntity yet_a_app = new TestingEntity(40, diffServices, allMatcher); + another_app.reproduce(allServices); + yet_a_app.reproduce(allServices); + + System.out.println(another_app); + System.out.println(yet_a_app); + + } + +} From 86e0381f416c06ed6906cc2a1d94536e3291e406 Mon Sep 17 00:00:00 2001 From: Vivek Nallur Date: Tue, 20 Aug 2013 13:23:54 +0100 Subject: [PATCH 06/11] Removed code irrelevant to speciation, etc. from BiPartiteGraph Finished ClonalReproduction as a strategy for both App and Platform TODO: Integrate Speciation as a reproduction strategy for both App and Platform Integrate App and Platform into BipartiteGraph --- .../java/diversim/AnyMatchingService.java | 15 +- diversim/src/main/java/diversim/App.java | 142 +++++++------ .../java/diversim/AppClonalReproduction.java | 24 +++ .../diversim/AppReproductionStrategy.java | 13 ++ .../java/diversim/BipartiteGraphWithUI.java | 2 +- diversim/src/main/java/diversim/Entity.java | 90 +++++---- .../java/diversim/ExtensionSpeciation.java | 2 +- diversim/src/main/java/diversim/Platform.java | 189 +++++++----------- .../diversim/PlatformClonalReproduction.java | 22 ++ .../PlatformReproductionStrategy.java | 13 ++ .../java/diversim/ReductionSpeciation.java | 2 +- .../java/diversim/ReproductionStrategy.java | 7 - .../src/main/java/diversim/TestingEntity.java | 44 ++-- 13 files changed, 303 insertions(+), 262 deletions(-) create mode 100644 diversim/src/main/java/diversim/AppClonalReproduction.java create mode 100644 diversim/src/main/java/diversim/AppReproductionStrategy.java create mode 100644 diversim/src/main/java/diversim/PlatformClonalReproduction.java create mode 100644 diversim/src/main/java/diversim/PlatformReproductionStrategy.java delete mode 100644 diversim/src/main/java/diversim/ReproductionStrategy.java diff --git a/diversim/src/main/java/diversim/AnyMatchingService.java b/diversim/src/main/java/diversim/AnyMatchingService.java index 20f6b0a..4e13774 100644 --- a/diversim/src/main/java/diversim/AnyMatchingService.java +++ b/diversim/src/main/java/diversim/AnyMatchingService.java @@ -12,14 +12,25 @@ 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)){ + 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/App.java b/diversim/src/main/java/diversim/App.java index dc1be48..9b4d358 100644 --- a/diversim/src/main/java/diversim/App.java +++ b/diversim/src/main/java/diversim/App.java @@ -2,6 +2,7 @@ import java.util.List; +import java.util.ArrayList; import java.util.HashSet; import java.util.Set; // Interface @@ -15,76 +16,73 @@ */ public class App extends Entity { -double redundancy = 0; - - -public double getRedundancy() { - return redundancy; -} - -List dependencies; - -public List getDependencies(){ - return this.dependencies; -} - -public void addDependencies(List new_deps){ - Set all_services = new HashSet(this.dependencies); - all_services.addAll(new_deps); - this.dependencies.clear(); - this.dependencies.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.dependencies.removeAll(new HashSet(obs_deps)); -} - - -public App(int id, List service_dependencies) { - super(id); - this.dependencies = service_dependencies; -} - -/** - * This method should really be called clone, since reproduction in this case - * is really just cloning of the object properties. But, we don't want to clash - * with java's own clone method inside Object - * - */ -public App reproduce(){ - App child = new App(this.ID, this.dependencies); - return child; -} - - -/* - * (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 - - 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 double getRedundancy() { + return redundancy; + } + + AppReproductionStrategy reproducer; + + 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 void setReproductionStrategy(AppReproductionStrategy rs){ + this.reproducer = rs; + } + + public List reproduce(){ + return this.reproducer.reproduce(this, null); + } + + + public App(int id, List service_dependencies) { + super(id); + this.services = new ArrayList (service_dependencies); + } + + + /* + * (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 + + 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; + } + } diff --git a/diversim/src/main/java/diversim/AppClonalReproduction.java b/diversim/src/main/java/diversim/AppClonalReproduction.java new file mode 100644 index 0000000..edd24e7 --- /dev/null +++ b/diversim/src/main/java/diversim/AppClonalReproduction.java @@ -0,0 +1,24 @@ +package diversim; + +import java.util.List; +import java.util.ArrayList; + +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{ + public List reproduce(App parent, List other_apps){ + MersenneTwisterFast rnd = new MersenneTwisterFast(System.nanoTime()); + App child = new App(rnd.nextInt(), parent.getDependencies()); + ArrayList children = new ArrayList(); + children.add(child); + return children; + } +} diff --git a/diversim/src/main/java/diversim/AppReproductionStrategy.java b/diversim/src/main/java/diversim/AppReproductionStrategy.java new file mode 100644 index 0000000..5ac6fa8 --- /dev/null +++ b/diversim/src/main/java/diversim/AppReproductionStrategy.java @@ -0,0 +1,13 @@ +package diversim; + +import java.util.List; + +import ec.util.MersenneTwisterFast; + +/** + * This is the interface that all App reproduction strategies must implement + * @author Vivek Nallur + */ +public interface AppReproductionStrategy{ + public List reproduce(App parent, List other_apps); +} diff --git a/diversim/src/main/java/diversim/BipartiteGraphWithUI.java b/diversim/src/main/java/diversim/BipartiteGraphWithUI.java index ebfc2a2..ea3ee61 100644 --- a/diversim/src/main/java/diversim/BipartiteGraphWithUI.java +++ b/diversim/src/main/java/diversim/BipartiteGraphWithUI.java @@ -111,7 +111,7 @@ public void draw(Object object, Graphics2D graphics, DrawInfo2D info) { 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); + paint = new Color((int)(255 * plat.getLoadingFactor()), 0, 0); double dist = sysSpace.getWidth() / (((BipartiteGraph)state).numPlatforms + 1); info.draw.width = ((double)plat.getSize()) / ((BipartiteGraph)state).numServices * dist; if (info.draw.width >= dist) info.draw.width = dist * 0.9; diff --git a/diversim/src/main/java/diversim/Entity.java b/diversim/src/main/java/diversim/Entity.java index d09b9c2..d8fd468 100644 --- a/diversim/src/main/java/diversim/Entity.java +++ b/diversim/src/main/java/diversim/Entity.java @@ -22,46 +22,56 @@ */ 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. + */ + ArrayList services; + + /** + * The number of link touching the entity in the bipartite graph. + */ + 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 diff --git a/diversim/src/main/java/diversim/ExtensionSpeciation.java b/diversim/src/main/java/diversim/ExtensionSpeciation.java index f0f1b95..bbafde7 100644 --- a/diversim/src/main/java/diversim/ExtensionSpeciation.java +++ b/diversim/src/main/java/diversim/ExtensionSpeciation.java @@ -16,7 +16,7 @@ public class ExtensionSpeciation implements SpeciationStrategy{ public List speciate(List current_dna, List all_services){ Set current_services = new HashSet(current_dna); Service new_service; - MersenneTwisterFast rnd = new MersenneTwisterFast(); + MersenneTwisterFast rnd = new MersenneTwisterFast(System.nanoTime()); do { new_service = all_services.get(rnd.nextInt(all_services.size())); }while(!current_services.add(new_service)); diff --git a/diversim/src/main/java/diversim/Platform.java b/diversim/src/main/java/diversim/Platform.java index 0913962..965fc72 100644 --- a/diversim/src/main/java/diversim/Platform.java +++ b/diversim/src/main/java/diversim/Platform.java @@ -2,6 +2,8 @@ import java.util.ArrayList; import java.util.List; +import java.util.Set; +import java.util.HashSet; import sim.engine.SimState; import sim.util.Bag; @@ -15,127 +17,76 @@ * which should be updated accordingly. * * @author Marco Biazzini + * @author Vivek Nallur * */ 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; + + PlatformReproductionStrategy reproducer; + // ArrayList supportedServices; + + public void setReproductionStrategy(PlatformReproductionStrategy rs){ + this.reproducer = rs; + } + + public List reproduce(){ + return this.reproducer.reproduce(this, null); + } + + 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; + + } + + + @Override + public String toString() { + String res = super.toString(); + return res; + } + } diff --git a/diversim/src/main/java/diversim/PlatformClonalReproduction.java b/diversim/src/main/java/diversim/PlatformClonalReproduction.java new file mode 100644 index 0000000..d77d1bc --- /dev/null +++ b/diversim/src/main/java/diversim/PlatformClonalReproduction.java @@ -0,0 +1,22 @@ +package diversim; + +import java.util.List; +import java.util.ArrayList; + +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, List other_platforms){ + 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/PlatformReproductionStrategy.java b/diversim/src/main/java/diversim/PlatformReproductionStrategy.java new file mode 100644 index 0000000..44a543f --- /dev/null +++ b/diversim/src/main/java/diversim/PlatformReproductionStrategy.java @@ -0,0 +1,13 @@ +package diversim; + +import java.util.List; + +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, List other_platforms); +} diff --git a/diversim/src/main/java/diversim/ReductionSpeciation.java b/diversim/src/main/java/diversim/ReductionSpeciation.java index a37d03e..a52362b 100644 --- a/diversim/src/main/java/diversim/ReductionSpeciation.java +++ b/diversim/src/main/java/diversim/ReductionSpeciation.java @@ -13,7 +13,7 @@ public class ReductionSpeciation implements SpeciationStrategy{ public List speciate(List current_dna, List all_services){ ArrayList current_services = new ArrayList (current_dna); - MersenneTwisterFast rnd = new MersenneTwisterFast(); + 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/ReproductionStrategy.java b/diversim/src/main/java/diversim/ReproductionStrategy.java deleted file mode 100644 index ee66635..0000000 --- a/diversim/src/main/java/diversim/ReproductionStrategy.java +++ /dev/null @@ -1,7 +0,0 @@ -/** - * The interface for reproduction strategies - * @author Vivek Nallur - */ -public interface ReproductionStrategy{ - public void reproduce(); -} diff --git a/diversim/src/main/java/diversim/TestingEntity.java b/diversim/src/main/java/diversim/TestingEntity.java index 177a07e..877f91a 100644 --- a/diversim/src/main/java/diversim/TestingEntity.java +++ b/diversim/src/main/java/diversim/TestingEntity.java @@ -23,23 +23,18 @@ public class TestingEntity extends Entity { MatchingStrategy matcher; SpeciationStrategy mutator; + ArrayList services; public TestingEntity(int id, List servs, MatchingStrategy ms) { - super(id); - for (Service s : servs) { - BipartiteGraph.addUnique(services, s); - } - this.matcher = ms; - } - - public boolean matches(Entity target){ - return this.matcher.matches(this, target); + super(id); + this.matcher = ms; + this.services = new ArrayList(servs); } public void addSpeciationStrategy(SpeciationStrategy ss){ this.mutator = ss; } - + /* * (non-Javadoc) @@ -58,7 +53,7 @@ public String toString() { } public void reproduce(List all_services){ - MersenneTwisterFast rnd = new MersenneTwisterFast(); + MersenneTwisterFast rnd = new MersenneTwisterFast(System.nanoTime()); if (rnd.nextBoolean()){ System.out.println(this.getClass().getSimpleName() + "speciating using Reduction strategy"); this.addSpeciationStrategy(new ReductionSpeciation()); @@ -87,20 +82,31 @@ public static void main(String[] args){ diffServices.add(b); diffServices.add(a); - TestingEntity platform = new TestingEntity(10, allServices, anyMatcher); - TestingEntity app = new TestingEntity(20, someServices, allMatcher); + AppReproductionStrategy ars = new AppClonalReproduction(); + 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); + 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)); - TestingEntity another_app = new TestingEntity(30, someServices, allMatcher); - TestingEntity yet_a_app = new TestingEntity(40, diffServices, allMatcher); - another_app.reproduce(allServices); - yet_a_app.reproduce(allServices); + List child_apps = app.reproduce(); + List child_platforms = platform.reproduce(); - System.out.println(another_app); - System.out.println(yet_a_app); + for (App child: child_apps){ + System.out.println(child); + } + for (Platform child: child_platforms){ + System.out.println(child); + } } From d7cd250129158d1765c6f20d45ad0daf6ef97f2d Mon Sep 17 00:00:00 2001 From: Vivek Nallur Date: Wed, 21 Aug 2013 11:59:16 +0100 Subject: [PATCH 07/11] Tested speciation-based reproduction on Apps --- diversim/src/main/java/diversim/App.java | 6 +-- diversim/src/main/java/diversim/Platform.java | 4 +- .../src/main/java/diversim/TestingEntity.java | 38 +++++++++++++++++-- 3 files changed, 40 insertions(+), 8 deletions(-) diff --git a/diversim/src/main/java/diversim/App.java b/diversim/src/main/java/diversim/App.java index 9b4d358..c66478a 100644 --- a/diversim/src/main/java/diversim/App.java +++ b/diversim/src/main/java/diversim/App.java @@ -49,9 +49,9 @@ public void setReproductionStrategy(AppReproductionStrategy rs){ this.reproducer = rs; } - public List reproduce(){ - return this.reproducer.reproduce(this, null); - } + public List reproduce(List possible_mates){ + return this.reproducer.reproduce(this, possible_mates); + } public App(int id, List service_dependencies) { diff --git a/diversim/src/main/java/diversim/Platform.java b/diversim/src/main/java/diversim/Platform.java index 965fc72..3c5d806 100644 --- a/diversim/src/main/java/diversim/Platform.java +++ b/diversim/src/main/java/diversim/Platform.java @@ -32,8 +32,8 @@ public void setReproductionStrategy(PlatformReproductionStrategy rs){ this.reproducer = rs; } - public List reproduce(){ - return this.reproducer.reproduce(this, null); + public List reproduce(List possible_mates){ + return this.reproducer.reproduce(this, possible_mates); } public void setLoadingFactor(int load){ diff --git a/diversim/src/main/java/diversim/TestingEntity.java b/diversim/src/main/java/diversim/TestingEntity.java index 877f91a..ada49a8 100644 --- a/diversim/src/main/java/diversim/TestingEntity.java +++ b/diversim/src/main/java/diversim/TestingEntity.java @@ -69,20 +69,28 @@ 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(a); + diffServices.add(d); + ArrayList moreServices = new ArrayList(); + moreServices.add(e); + moreServices.add(c); AppReproductionStrategy ars = new AppClonalReproduction(); + AppReproductionStrategy asr = new AppSpeciationReproduction(); PlatformReproductionStrategy prs = new PlatformClonalReproduction(); Platform platform = new Platform(10, allServices); @@ -93,13 +101,26 @@ public static void main(String[] args){ 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(); - List child_platforms = platform.reproduce(); + List child_apps = app.reproduce(all_apps); + List child_platforms = platform.reproduce(null); for (App child: child_apps){ System.out.println(child); @@ -108,6 +129,17 @@ public static void main(String[] args){ System.out.println(child); } + List children_app1 = app1.reproduce(all_apps); + List children_app2 = app2.reproduce(all_apps); + + for (App child: children_app1){ + System.out.println(child); + } + + for (App child: children_app2){ + System.out.println(child); + } + } } From e16c070ff8a04de557cae14cc9bc772d3b4178d4 Mon Sep 17 00:00:00 2001 From: Vivek Nallur Date: Wed, 21 Aug 2013 11:59:16 +0100 Subject: [PATCH 08/11] Tested speciation-based reproduction on Apps and Platforms --- diversim/src/main/java/diversim/App.java | 6 +-- diversim/src/main/java/diversim/Platform.java | 4 +- .../src/main/java/diversim/TestingEntity.java | 38 +++++++++++++++++-- 3 files changed, 40 insertions(+), 8 deletions(-) diff --git a/diversim/src/main/java/diversim/App.java b/diversim/src/main/java/diversim/App.java index 9b4d358..c66478a 100644 --- a/diversim/src/main/java/diversim/App.java +++ b/diversim/src/main/java/diversim/App.java @@ -49,9 +49,9 @@ public void setReproductionStrategy(AppReproductionStrategy rs){ this.reproducer = rs; } - public List reproduce(){ - return this.reproducer.reproduce(this, null); - } + public List reproduce(List possible_mates){ + return this.reproducer.reproduce(this, possible_mates); + } public App(int id, List service_dependencies) { diff --git a/diversim/src/main/java/diversim/Platform.java b/diversim/src/main/java/diversim/Platform.java index 965fc72..3c5d806 100644 --- a/diversim/src/main/java/diversim/Platform.java +++ b/diversim/src/main/java/diversim/Platform.java @@ -32,8 +32,8 @@ public void setReproductionStrategy(PlatformReproductionStrategy rs){ this.reproducer = rs; } - public List reproduce(){ - return this.reproducer.reproduce(this, null); + public List reproduce(List possible_mates){ + return this.reproducer.reproduce(this, possible_mates); } public void setLoadingFactor(int load){ diff --git a/diversim/src/main/java/diversim/TestingEntity.java b/diversim/src/main/java/diversim/TestingEntity.java index 877f91a..ada49a8 100644 --- a/diversim/src/main/java/diversim/TestingEntity.java +++ b/diversim/src/main/java/diversim/TestingEntity.java @@ -69,20 +69,28 @@ 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(a); + diffServices.add(d); + ArrayList moreServices = new ArrayList(); + moreServices.add(e); + moreServices.add(c); AppReproductionStrategy ars = new AppClonalReproduction(); + AppReproductionStrategy asr = new AppSpeciationReproduction(); PlatformReproductionStrategy prs = new PlatformClonalReproduction(); Platform platform = new Platform(10, allServices); @@ -93,13 +101,26 @@ public static void main(String[] args){ 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(); - List child_platforms = platform.reproduce(); + List child_apps = app.reproduce(all_apps); + List child_platforms = platform.reproduce(null); for (App child: child_apps){ System.out.println(child); @@ -108,6 +129,17 @@ public static void main(String[] args){ System.out.println(child); } + List children_app1 = app1.reproduce(all_apps); + List children_app2 = app2.reproduce(all_apps); + + for (App child: children_app1){ + System.out.println(child); + } + + for (App child: children_app2){ + System.out.println(child); + } + } } From 2e5055ed4b764b3ecd57b48c5fd1ca5f5d7e6404 Mon Sep 17 00:00:00 2001 From: Hui Song Date: Fri, 6 Sep 2013 12:47:46 +0200 Subject: [PATCH 09/11] try to push --- .../src/main/java/diversim/ReConnect.java | 50 +++++++++++++ .../main/java/diversim/StrategyFactory.java | 71 +++++++++++++++++++ .../extinction/AgingExtinctionStrategy.java | 31 ++++++++ .../extinction/ExtinctionStrategy.java | 11 +++ .../extinction/OrphanExtinctionStrategy.java | 26 +++++++ .../AbstractSpeciationReproduction.java | 30 ++++++++ .../reproduction/AppClonalReproduction.java | 25 +++++++ .../reproduction/AppReproductionStrategy.java | 14 ++++ .../AppSpeciationReproduction.java | 37 ++++++++++ .../reproduction/ExtensionSpeciation.java | 26 +++++++ .../PlatformClonalReproduction.java | 23 ++++++ .../PlatformReproductionStrategy.java | 14 ++++ .../PlatformSpeciationReproduction.java | 28 ++++++++ .../reproduction/ReductionSpeciation.java | 21 ++++++ .../reproduction/SpeciationStrategy.java | 16 +++++ 15 files changed, 423 insertions(+) create mode 100644 diversim/src/main/java/diversim/ReConnect.java create mode 100644 diversim/src/main/java/diversim/StrategyFactory.java create mode 100644 diversim/src/main/java/diversim/strategy/extinction/AgingExtinctionStrategy.java create mode 100644 diversim/src/main/java/diversim/strategy/extinction/ExtinctionStrategy.java create mode 100644 diversim/src/main/java/diversim/strategy/extinction/OrphanExtinctionStrategy.java create mode 100644 diversim/src/main/java/diversim/strategy/reproduction/AbstractSpeciationReproduction.java create mode 100644 diversim/src/main/java/diversim/strategy/reproduction/AppClonalReproduction.java create mode 100644 diversim/src/main/java/diversim/strategy/reproduction/AppReproductionStrategy.java create mode 100644 diversim/src/main/java/diversim/strategy/reproduction/AppSpeciationReproduction.java create mode 100644 diversim/src/main/java/diversim/strategy/reproduction/ExtensionSpeciation.java create mode 100644 diversim/src/main/java/diversim/strategy/reproduction/PlatformClonalReproduction.java create mode 100644 diversim/src/main/java/diversim/strategy/reproduction/PlatformReproductionStrategy.java create mode 100644 diversim/src/main/java/diversim/strategy/reproduction/PlatformSpeciationReproduction.java create mode 100644 diversim/src/main/java/diversim/strategy/reproduction/ReductionSpeciation.java create mode 100644 diversim/src/main/java/diversim/strategy/reproduction/SpeciationStrategy.java diff --git a/diversim/src/main/java/diversim/ReConnect.java b/diversim/src/main/java/diversim/ReConnect.java new file mode 100644 index 0000000..d762ae0 --- /dev/null +++ b/diversim/src/main/java/diversim/ReConnect.java @@ -0,0 +1,50 @@ +package diversim; + +import java.util.ArrayList; +import java.util.List; + +import sim.engine.SimState; +import sim.engine.Steppable; + +public class ReConnect implements Steppable { + MatchingStrategy matcher = new AllMatchingService(); + + @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.schedule + } + } + + System.out.println(String.format("apps: %d",graph.apps.size())); + graph.apps.removeAll(dead); + + + System.out.println(String.format("apps: %d",graph.apps.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/StrategyFactory.java b/diversim/src/main/java/diversim/StrategyFactory.java new file mode 100644 index 0000000..ff28430 --- /dev/null +++ b/diversim/src/main/java/diversim/StrategyFactory.java @@ -0,0 +1,71 @@ +package diversim; + +import java.util.ArrayList; +import java.util.List; + +import diversim.strategy.extinction.AgingExtinctionStrategy; +import diversim.strategy.extinction.ExtinctionStrategy; +import diversim.strategy.extinction.OrphanExtinctionStrategy; +import diversim.strategy.reproduction.AppReproductionStrategy; +import diversim.strategy.reproduction.AppSpeciationReproduction; +import diversim.strategy.reproduction.ExtensionSpeciation; +import diversim.strategy.reproduction.PlatformReproductionStrategy; +import diversim.strategy.reproduction.PlatformSpeciationReproduction; +import diversim.strategy.reproduction.ReductionSpeciation; +import ec.util.MersenneTwisterFast; + +public class StrategyFactory { + + public final static int APP_MAX_LIFE = 300; + + 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 AppReproductionStrategy createAppReproductionStrategy(BipartiteGraph graph){ + + if(appReproductionStrategy != null) + return appReproductionStrategy; + + List allServices = graph.services; + AppSpeciationReproduction asr = new AppSpeciationReproduction(); + + asr.getStrategies().add(new ReductionSpeciation()); + asr.getStrategies().add(new ExtensionSpeciation()); + asr.setAllServices(allServices); + + return asr; + } + + /** + * Now, the platforms always generate a "degenerated child"; + * @param allServices + * @return + */ + public PlatformReproductionStrategy createPlatformReproductionStrategy(List allServices){ + PlatformSpeciationReproduction psr = new PlatformSpeciationReproduction(); + psr.getStrategies().add(new ReductionSpeciation()); + psr.setAllServices(allServices); + + return psr; + } + + public List createAppExtinctionStrategies(Entity entity, BipartiteGraph graph){ + MersenneTwisterFast rnd = new MersenneTwisterFast(System.nanoTime()); + List killers = new ArrayList(); + killers.add(new AgingExtinctionStrategy(entity, graph.schedule.getSteps(), rnd.nextInt(APP_MAX_LIFE))); + killers.add(new OrphanExtinctionStrategy(entity)); + return killers; + + } + + +} 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..34be693 --- /dev/null +++ b/diversim/src/main/java/diversim/strategy/extinction/AgingExtinctionStrategy.java @@ -0,0 +1,31 @@ +package diversim.strategy.extinction; + +import diversim.BipartiteGraph; +import diversim.Entity; + +public class AgingExtinctionStrategy implements ExtinctionStrategy { + + private Entity entity = null; + private int expectedAge = 0; + private long born = 0; + + public AgingExtinctionStrategy(Entity entity, long born, int expectedAge){ + + this.born = born; + this.expectedAge = expectedAge; + this.entity = entity; + + } + + @Override + public boolean die(BipartiteGraph graph) { + long steps = graph.schedule.getSteps(); + if( steps - born >= expectedAge){ + return true; + } + else + return false; + + } + +} diff --git a/diversim/src/main/java/diversim/strategy/extinction/ExtinctionStrategy.java b/diversim/src/main/java/diversim/strategy/extinction/ExtinctionStrategy.java new file mode 100644 index 0000000..9085023 --- /dev/null +++ b/diversim/src/main/java/diversim/strategy/extinction/ExtinctionStrategy.java @@ -0,0 +1,11 @@ +package diversim.strategy.extinction; + +import diversim.BipartiteGraph; + +/** + * The interface for extinction strategies + * @author Vivek Nallur + */ +public interface ExtinctionStrategy { + public boolean die(BipartiteGraph graph); +} diff --git a/diversim/src/main/java/diversim/strategy/extinction/OrphanExtinctionStrategy.java b/diversim/src/main/java/diversim/strategy/extinction/OrphanExtinctionStrategy.java new file mode 100644 index 0000000..12914e5 --- /dev/null +++ b/diversim/src/main/java/diversim/strategy/extinction/OrphanExtinctionStrategy.java @@ -0,0 +1,26 @@ +package diversim.strategy.extinction; + +import diversim.BipartiteGraph; +import diversim.Entity; + +public class OrphanExtinctionStrategy implements ExtinctionStrategy { + + private Entity entity = null; + + + public OrphanExtinctionStrategy(Entity entity){ + + this.entity = entity; + + } + + @Override + public boolean die(BipartiteGraph graph) { + if(entity.services.size() == 0) + return true; + if(entity.degree == 0) + return true; + return false; + } + +} diff --git a/diversim/src/main/java/diversim/strategy/reproduction/AbstractSpeciationReproduction.java b/diversim/src/main/java/diversim/strategy/reproduction/AbstractSpeciationReproduction.java new file mode 100644 index 0000000..f98e07c --- /dev/null +++ b/diversim/src/main/java/diversim/strategy/reproduction/AbstractSpeciationReproduction.java @@ -0,0 +1,30 @@ +package diversim.strategy.reproduction; + +import java.util.ArrayList; +import java.util.List; + +import diversim.Service; + +public class AbstractSpeciationReproduction { + + public AbstractSpeciationReproduction() { + super(); + } + + ArrayList strategies = new ArrayList(); + + List allServices = null; + + public List getAllServices(){ + return allServices; + } + + public void setAllServices(List allServices){ + this.allServices = new ArrayList(allServices); + } + + public List getStrategies(){ + return strategies; + } + +} \ No newline at end of file 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..607de97 --- /dev/null +++ b/diversim/src/main/java/diversim/strategy/reproduction/AppClonalReproduction.java @@ -0,0 +1,25 @@ +package diversim.strategy.reproduction; + +import java.util.List; +import java.util.ArrayList; + +import diversim.App; +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{ + public List reproduce(App parent, List other_apps){ + MersenneTwisterFast rnd = new MersenneTwisterFast(System.nanoTime()); + App child = new App(rnd.nextInt(), parent.getDependencies()); + 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..210041e --- /dev/null +++ b/diversim/src/main/java/diversim/strategy/reproduction/AppReproductionStrategy.java @@ -0,0 +1,14 @@ +package diversim.strategy.reproduction; + +import java.util.List; + +import diversim.App; +import ec.util.MersenneTwisterFast; + +/** + * This is the interface that all App reproduction strategies must implement + * @author Vivek Nallur + */ +public interface AppReproductionStrategy{ + public List reproduce(App parent, List other_apps); +} diff --git a/diversim/src/main/java/diversim/strategy/reproduction/AppSpeciationReproduction.java b/diversim/src/main/java/diversim/strategy/reproduction/AppSpeciationReproduction.java new file mode 100644 index 0000000..e6a496c --- /dev/null +++ b/diversim/src/main/java/diversim/strategy/reproduction/AppSpeciationReproduction.java @@ -0,0 +1,37 @@ +package diversim.strategy.reproduction; + +import java.util.ArrayList; +import java.util.List; + +import diversim.App; +import ec.util.MersenneTwisterFast; + + +/** + * Randomly choose one of the registered speciation strategies + * to change its dna + * + * @author hui song + * + */ +public class AppSpeciationReproduction extends AbstractSpeciationReproduction implements AppReproductionStrategy { + + + + + @Override + public List reproduce(App parent, List other_apps) { + MersenneTwisterFast rnd = new MersenneTwisterFast(System.nanoTime()); + + SpeciationStrategy strategy = getStrategies().get(rnd.nextInt(getStrategies().size())); + + App child = new App(rnd.nextInt(), + strategy.speciate(parent.getDependencies(),getAllServices()) + ); + ArrayList children = new ArrayList(); + children.add(child); + return children; + + } + +} diff --git a/diversim/src/main/java/diversim/strategy/reproduction/ExtensionSpeciation.java b/diversim/src/main/java/diversim/strategy/reproduction/ExtensionSpeciation.java new file mode 100644 index 0000000..e7313b0 --- /dev/null +++ b/diversim/src/main/java/diversim/strategy/reproduction/ExtensionSpeciation.java @@ -0,0 +1,26 @@ +package diversim.strategy.reproduction; + +import java.util.List; +import java.util.ArrayList; +import java.util.Set; +import java.util.HashSet; + +import diversim.Service; +import ec.util.MersenneTwisterFast; +import sim.util.Bag; +/** + * This class implements the Extension speciation strategy + * @author Vivek Nallur + */ + +public class ExtensionSpeciation implements SpeciationStrategy{ + public List speciate(List current_dna, List all_services){ + 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/PlatformClonalReproduction.java b/diversim/src/main/java/diversim/strategy/reproduction/PlatformClonalReproduction.java new file mode 100644 index 0000000..ef00a99 --- /dev/null +++ b/diversim/src/main/java/diversim/strategy/reproduction/PlatformClonalReproduction.java @@ -0,0 +1,23 @@ +package diversim.strategy.reproduction; + +import java.util.List; +import java.util.ArrayList; + +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, List other_platforms){ + 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..4ae150c --- /dev/null +++ b/diversim/src/main/java/diversim/strategy/reproduction/PlatformReproductionStrategy.java @@ -0,0 +1,14 @@ +package diversim.strategy.reproduction; + +import java.util.List; + +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, List other_platforms); +} diff --git a/diversim/src/main/java/diversim/strategy/reproduction/PlatformSpeciationReproduction.java b/diversim/src/main/java/diversim/strategy/reproduction/PlatformSpeciationReproduction.java new file mode 100644 index 0000000..a963374 --- /dev/null +++ b/diversim/src/main/java/diversim/strategy/reproduction/PlatformSpeciationReproduction.java @@ -0,0 +1,28 @@ +package diversim.strategy.reproduction; + +import java.util.ArrayList; +import java.util.List; + +import diversim.Platform; +import diversim.Service; +import ec.util.MersenneTwisterFast; + +public class PlatformSpeciationReproduction + extends AbstractSpeciationReproduction + implements PlatformReproductionStrategy { + + + + @Override + public List reproduce(Platform parent, + List other_platforms) { + // TODO Auto-generated method stub + MersenneTwisterFast rnd = new MersenneTwisterFast(System.nanoTime()); + SpeciationStrategy strategy = getStrategies().get(rnd.nextInt(getStrategies().size())); + List services = strategy.speciate(parent.services, getAllServices()); + List children = new ArrayList(); + children.add(new Platform(rnd.nextInt(), services, parent.getLoadingFactor())); + return children; + } + +} diff --git a/diversim/src/main/java/diversim/strategy/reproduction/ReductionSpeciation.java b/diversim/src/main/java/diversim/strategy/reproduction/ReductionSpeciation.java new file mode 100644 index 0000000..d8c1d35 --- /dev/null +++ b/diversim/src/main/java/diversim/strategy/reproduction/ReductionSpeciation.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 ReductionSpeciation implements SpeciationStrategy{ + 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/SpeciationStrategy.java b/diversim/src/main/java/diversim/strategy/reproduction/SpeciationStrategy.java new file mode 100644 index 0000000..5894fc1 --- /dev/null +++ b/diversim/src/main/java/diversim/strategy/reproduction/SpeciationStrategy.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 SpeciationStrategy{ + public List speciate(List current_dna, List all_services); +} + From 4043e2ced30862b5b270846747e85db76081f73a Mon Sep 17 00:00:00 2001 From: Hui Song Date: Wed, 11 Sep 2013 16:02:08 +0200 Subject: [PATCH 10/11] A first try with an experiment --- diversim/src/main/java/diversim/App.java | 80 ++++- .../java/diversim/AppClonalReproduction.java | 24 -- .../diversim/AppReproductionStrategy.java | 13 - .../main/java/diversim/BipartiteGraph.java | 65 +++- .../java/diversim/BipartiteGraphWithUI.java | 67 +++- diversim/src/main/java/diversim/Entity.java | 9 +- .../java/diversim/ExtensionSpeciation.java | 25 -- .../java/diversim/ExtinctionStrategy.java | 7 - diversim/src/main/java/diversim/Fate.java | 3 + diversim/src/main/java/diversim/Platform.java | 61 +++- .../diversim/PlatformClonalReproduction.java | 22 -- .../PlatformReproductionStrategy.java | 13 - .../src/main/java/diversim/ReConnect.java | 29 +- .../java/diversim/ReductionSpeciation.java | 20 -- .../java/diversim/SpeciationStrategy.java | 14 - .../main/java/diversim/StrategyFactory.java | 112 +++++-- .../src/main/java/diversim/TestingEntity.java | 302 +++++++++--------- .../extinction/AgingExtinctionStrategy.java | 23 +- .../extinction/AppExtinctionStrategy.java | 9 + .../AppOrphanExtinctionStrategy.java | 19 ++ .../extinction/ExtinctionStrategy.java | 11 - .../extinction/OrphanExtinctionStrategy.java | 26 -- .../PlatformExtinctionStrategy.java | 10 + .../matching}/AllMatchingService.java | 5 +- .../matching}/AnyMatchingService.java | 5 +- .../matching}/MatchingStrategy.java | 4 +- .../AbstractSpeciationReproduction.java | 30 -- .../reproduction/AppClonalReproduction.java | 8 +- .../reproduction/AppReproductionStrategy.java | 5 +- .../AppReproductionWithPossibility.java | 29 ++ ...va => AppSpeciationReproductionByDNA.java} | 22 +- .../reproduction/DNAExtensionSpeciation.java | 29 ++ ...ation.java => DNAReductionSpeciation.java} | 10 +- ...iationStrategy.java => DNASpeciation.java} | 2 +- .../reproduction/ExtensionSpeciation.java | 26 -- .../PlatformClonalReproduction.java | 3 +- .../PlatformReproductionStrategy.java | 3 +- .../PlatformReproductionWithPossibility.java | 30 ++ .../PlatformSpeciationReproduction.java | 28 -- .../PlatformSpeciationReproductionByDNA.java | 33 ++ .../reproduction/ReproductionStrategy.java | 11 + gitout.txt | 0 42 files changed, 745 insertions(+), 502 deletions(-) delete mode 100644 diversim/src/main/java/diversim/AppClonalReproduction.java delete mode 100644 diversim/src/main/java/diversim/AppReproductionStrategy.java delete mode 100644 diversim/src/main/java/diversim/ExtensionSpeciation.java delete mode 100644 diversim/src/main/java/diversim/ExtinctionStrategy.java delete mode 100644 diversim/src/main/java/diversim/PlatformClonalReproduction.java delete mode 100644 diversim/src/main/java/diversim/PlatformReproductionStrategy.java delete mode 100644 diversim/src/main/java/diversim/ReductionSpeciation.java delete mode 100644 diversim/src/main/java/diversim/SpeciationStrategy.java create mode 100644 diversim/src/main/java/diversim/strategy/extinction/AppExtinctionStrategy.java create mode 100644 diversim/src/main/java/diversim/strategy/extinction/AppOrphanExtinctionStrategy.java delete mode 100644 diversim/src/main/java/diversim/strategy/extinction/ExtinctionStrategy.java delete mode 100644 diversim/src/main/java/diversim/strategy/extinction/OrphanExtinctionStrategy.java create mode 100644 diversim/src/main/java/diversim/strategy/extinction/PlatformExtinctionStrategy.java rename diversim/src/main/java/diversim/{ => strategy/matching}/AllMatchingService.java (88%) rename diversim/src/main/java/diversim/{ => strategy/matching}/AnyMatchingService.java (93%) rename diversim/src/main/java/diversim/{ => strategy/matching}/MatchingStrategy.java (91%) delete mode 100644 diversim/src/main/java/diversim/strategy/reproduction/AbstractSpeciationReproduction.java create mode 100644 diversim/src/main/java/diversim/strategy/reproduction/AppReproductionWithPossibility.java rename diversim/src/main/java/diversim/strategy/reproduction/{AppSpeciationReproduction.java => AppSpeciationReproductionByDNA.java} (54%) create mode 100644 diversim/src/main/java/diversim/strategy/reproduction/DNAExtensionSpeciation.java rename diversim/src/main/java/diversim/strategy/reproduction/{ReductionSpeciation.java => DNAReductionSpeciation.java} (52%) rename diversim/src/main/java/diversim/strategy/reproduction/{SpeciationStrategy.java => DNASpeciation.java} (88%) delete mode 100644 diversim/src/main/java/diversim/strategy/reproduction/ExtensionSpeciation.java create mode 100644 diversim/src/main/java/diversim/strategy/reproduction/PlatformReproductionWithPossibility.java delete mode 100644 diversim/src/main/java/diversim/strategy/reproduction/PlatformSpeciationReproduction.java create mode 100644 diversim/src/main/java/diversim/strategy/reproduction/PlatformSpeciationReproductionByDNA.java create mode 100644 diversim/src/main/java/diversim/strategy/reproduction/ReproductionStrategy.java create mode 100644 gitout.txt diff --git a/diversim/src/main/java/diversim/App.java b/diversim/src/main/java/diversim/App.java index c66478a..daed46a 100644 --- a/diversim/src/main/java/diversim/App.java +++ b/diversim/src/main/java/diversim/App.java @@ -6,24 +6,41 @@ 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 rely on specific services to function, and if an App cannot find a - * Platform that provides at least these services, then it dies. + * 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 boolean dead = false; + + public List platforms = new ArrayList(); + public double getRedundancy() { return redundancy; } - AppReproductionStrategy reproducer; + List reproducers; + List killers; + public List getDependencies(){ return this.services; @@ -45,18 +62,31 @@ public void removeDependencies(List obs_deps){ this.services.removeAll(new HashSet(obs_deps)); } - public void setReproductionStrategy(AppReproductionStrategy rs){ - this.reproducer = rs; - } - public List reproduce(List possible_mates){ - return this.reproducer.reproduce(this, possible_mates); + + + 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); } @@ -72,8 +102,21 @@ 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()); } @@ -85,4 +128,21 @@ public String toString() { 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/AppClonalReproduction.java b/diversim/src/main/java/diversim/AppClonalReproduction.java deleted file mode 100644 index edd24e7..0000000 --- a/diversim/src/main/java/diversim/AppClonalReproduction.java +++ /dev/null @@ -1,24 +0,0 @@ -package diversim; - -import java.util.List; -import java.util.ArrayList; - -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{ - public List reproduce(App parent, List other_apps){ - MersenneTwisterFast rnd = new MersenneTwisterFast(System.nanoTime()); - App child = new App(rnd.nextInt(), parent.getDependencies()); - ArrayList children = new ArrayList(); - children.add(child); - return children; - } -} diff --git a/diversim/src/main/java/diversim/AppReproductionStrategy.java b/diversim/src/main/java/diversim/AppReproductionStrategy.java deleted file mode 100644 index 5ac6fa8..0000000 --- a/diversim/src/main/java/diversim/AppReproductionStrategy.java +++ /dev/null @@ -1,13 +0,0 @@ -package diversim; - -import java.util.List; - -import ec.util.MersenneTwisterFast; - -/** - * This is the interface that all App reproduction strategies must implement - * @author Vivek Nallur - */ -public interface AppReproductionStrategy{ - public List reproduce(App parent, List other_apps); -} diff --git a/diversim/src/main/java/diversim/BipartiteGraph.java b/diversim/src/main/java/diversim/BipartiteGraph.java index c5b7a4f..9f46c62 100644 --- a/diversim/src/main/java/diversim/BipartiteGraph.java +++ b/diversim/src/main/java/diversim/BipartiteGraph.java @@ -44,6 +44,7 @@ * * @author Marco Biazzini * @author Vivek Nallur + * @author Hui Song * */ public class BipartiteGraph extends SimState { @@ -61,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. @@ -73,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. */ @@ -121,6 +128,9 @@ public class BipartiteGraph extends SimState { private int aCounter; public boolean changed; +public static BipartiteGraph INSTANCE = null; + + /** * Getters and setters. @@ -253,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; } @@ -298,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 @@ -327,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()); + } @@ -392,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. @@ -453,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 @@ -539,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 ea3ee61..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,12 +111,22 @@ 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( @@ -112,12 +134,14 @@ 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.getLoadingFactor()), 0, 0); - double dist = sysSpace.getWidth() / (((BipartiteGraph)state).numPlatforms + 1); + //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 d8fd468..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; @@ -30,12 +31,12 @@ abstract public class Entity implements Steppable { /** * All services hosted by the entity. */ - ArrayList services; + public ArrayList services; /** * The number of link touching the entity in the bipartite graph. */ - int degree; + public int degree; /** * The matching strategy employed by this entity @@ -52,9 +53,11 @@ abstract public class Entity implements Steppable { public boolean matches(Entity target){ return this.matcher.matches(this, target); } + public void setMatchingStrategy(MatchingStrategy ms){ this.matcher = ms; - } + } + public int getDegree() { return degree; } diff --git a/diversim/src/main/java/diversim/ExtensionSpeciation.java b/diversim/src/main/java/diversim/ExtensionSpeciation.java deleted file mode 100644 index bbafde7..0000000 --- a/diversim/src/main/java/diversim/ExtensionSpeciation.java +++ /dev/null @@ -1,25 +0,0 @@ -package diversim; - -import java.util.List; -import java.util.ArrayList; -import java.util.Set; -import java.util.HashSet; - -import ec.util.MersenneTwisterFast; -import sim.util.Bag; -/** - * This class implements the Extension speciation strategy - * @author Vivek Nallur - */ - -public class ExtensionSpeciation implements SpeciationStrategy{ - public List speciate(List current_dna, List all_services){ - 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/ExtinctionStrategy.java b/diversim/src/main/java/diversim/ExtinctionStrategy.java deleted file mode 100644 index 490db47..0000000 --- a/diversim/src/main/java/diversim/ExtinctionStrategy.java +++ /dev/null @@ -1,7 +0,0 @@ -/** - * The interface for extinction strategies - * @author Vivek Nallur - */ -public interface ExtinctionStrategy { - public void die(); -} 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 3c5d806..3ed52ef 100644 --- a/diversim/src/main/java/diversim/Platform.java +++ b/diversim/src/main/java/diversim/Platform.java @@ -5,6 +5,10 @@ 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.*; @@ -18,6 +22,10 @@ * * @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 { @@ -25,15 +33,21 @@ public class Platform extends Entity { // how many apps can one service on this platform support. private int APP_PER_SERVICE = 1; - PlatformReproductionStrategy reproducer; + public boolean dead = false; + + public List app = new ArrayList(); + + List reproducers; + List killers; // ArrayList supportedServices; - public void setReproductionStrategy(PlatformReproductionStrategy rs){ - this.reproducer = rs; - } - public List reproduce(List possible_mates){ - return this.reproducer.reproduce(this, possible_mates); + 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){ @@ -78,9 +92,22 @@ public Platform(int id, List servs, int loading_factor) { */ @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 @@ -89,4 +116,24 @@ public String 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/PlatformClonalReproduction.java b/diversim/src/main/java/diversim/PlatformClonalReproduction.java deleted file mode 100644 index d77d1bc..0000000 --- a/diversim/src/main/java/diversim/PlatformClonalReproduction.java +++ /dev/null @@ -1,22 +0,0 @@ -package diversim; - -import java.util.List; -import java.util.ArrayList; - -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, List other_platforms){ - 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/PlatformReproductionStrategy.java b/diversim/src/main/java/diversim/PlatformReproductionStrategy.java deleted file mode 100644 index 44a543f..0000000 --- a/diversim/src/main/java/diversim/PlatformReproductionStrategy.java +++ /dev/null @@ -1,13 +0,0 @@ -package diversim; - -import java.util.List; - -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, List other_platforms); -} diff --git a/diversim/src/main/java/diversim/ReConnect.java b/diversim/src/main/java/diversim/ReConnect.java index d762ae0..6fabe1a 100644 --- a/diversim/src/main/java/diversim/ReConnect.java +++ b/diversim/src/main/java/diversim/ReConnect.java @@ -3,11 +3,22 @@ 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 = new AllMatchingService(); + MatchingStrategy matcher = StrategyFactory.fINSTANCE.createMatchingStrategy(); @Override public void step(SimState state) { @@ -18,15 +29,25 @@ public void step(SimState state) { if(a.dead){ dead.add(a); graph.bipartiteNetwork.removeNode(a); - //graph.schedule + } } - System.out.println(String.format("apps: %d",graph.apps.size())); 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())); + - System.out.println(String.format("apps: %d",graph.apps.size())); for(App app:graph.apps) diff --git a/diversim/src/main/java/diversim/ReductionSpeciation.java b/diversim/src/main/java/diversim/ReductionSpeciation.java deleted file mode 100644 index a52362b..0000000 --- a/diversim/src/main/java/diversim/ReductionSpeciation.java +++ /dev/null @@ -1,20 +0,0 @@ -package diversim; - -import java.util.List; -import java.util.ArrayList; - -import ec.util.MersenneTwisterFast; -import sim.util.Bag; -/** - * This class implements the Reduction speciation strategy - * @author Vivek Nallur - */ - -public class ReductionSpeciation implements SpeciationStrategy{ - 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/SpeciationStrategy.java b/diversim/src/main/java/diversim/SpeciationStrategy.java deleted file mode 100644 index ac6ef72..0000000 --- a/diversim/src/main/java/diversim/SpeciationStrategy.java +++ /dev/null @@ -1,14 +0,0 @@ -package diversim; - -import java.util.List; - -/** - * - * @author Vivek Nallur - * - * This is the base interface for all speciation strategies - */ -public interface SpeciationStrategy{ - public List speciate(List current_dna, List all_services); -} - diff --git a/diversim/src/main/java/diversim/StrategyFactory.java b/diversim/src/main/java/diversim/StrategyFactory.java index ff28430..098836b 100644 --- a/diversim/src/main/java/diversim/StrategyFactory.java +++ b/diversim/src/main/java/diversim/StrategyFactory.java @@ -4,19 +4,40 @@ import java.util.List; import diversim.strategy.extinction.AgingExtinctionStrategy; -import diversim.strategy.extinction.ExtinctionStrategy; -import diversim.strategy.extinction.OrphanExtinctionStrategy; +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.AppSpeciationReproduction; -import diversim.strategy.reproduction.ExtensionSpeciation; +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.PlatformSpeciationReproduction; -import diversim.strategy.reproduction.ReductionSpeciation; +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 = 300; + public final static int APP_MAX_LIFE = 100; + public final static int PLATFORM_MAX_LIFE = 23; public static StrategyFactory fINSTANCE = new StrategyFactory(); @@ -24,48 +45,83 @@ protected StrategyFactory(){ } - private static AppReproductionStrategy appReproductionStrategy = null; + //private static AppReproductionStrategy appReproductionStrategy = null; /** * Currently, reproduction strategy is general, so only one instance is enough * @param graph * @return */ - public AppReproductionStrategy createAppReproductionStrategy(BipartiteGraph graph){ + public List createAppReproductionStrategy(App app, BipartiteGraph graph){ - if(appReproductionStrategy != null) - return appReproductionStrategy; + if(APP_REPRODUCTION_STRATEGIES != null) + return APP_REPRODUCTION_STRATEGIES; - List allServices = graph.services; - AppSpeciationReproduction asr = new AppSpeciationReproduction(); - - asr.getStrategies().add(new ReductionSpeciation()); - asr.getStrategies().add(new ExtensionSpeciation()); - asr.setAllServices(allServices); - - return asr; + 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 PlatformReproductionStrategy createPlatformReproductionStrategy(List allServices){ - PlatformSpeciationReproduction psr = new PlatformSpeciationReproduction(); - psr.getStrategies().add(new ReductionSpeciation()); - psr.setAllServices(allServices); + public List createPlatformReproductionStrategy(Platform platform, BipartiteGraph state){ + if(PLATFORM_REPRODUCTION_STRATEGIES != null) + return PLATFORM_REPRODUCTION_STRATEGIES; + + List result = new ArrayList(); + - return psr; + + 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(Entity entity, BipartiteGraph graph){ + public List createAppExtinctionStrategies(App app, BipartiteGraph graph){ MersenneTwisterFast rnd = new MersenneTwisterFast(System.nanoTime()); - List killers = new ArrayList(); - killers.add(new AgingExtinctionStrategy(entity, graph.schedule.getSteps(), rnd.nextInt(APP_MAX_LIFE))); - killers.add(new OrphanExtinctionStrategy(entity)); + 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 index ada49a8..2d6bc4e 100644 --- a/diversim/src/main/java/diversim/TestingEntity.java +++ b/diversim/src/main/java/diversim/TestingEntity.java @@ -1,145 +1,157 @@ -package diversim; - -import java.util.ArrayList; -import java.util.List; - -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; - SpeciationStrategy mutator; - ArrayList services; - - public TestingEntity(int id, List servs, MatchingStrategy ms) { - super(id); - this.matcher = ms; - this.services = new ArrayList(servs); - } - - public void addSpeciationStrategy(SpeciationStrategy 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 ReductionSpeciation()); - - }else{ - System.out.println(this.getClass().getSimpleName() + "speciating using Extension strategy"); - this.addSpeciationStrategy(new ExtensionSpeciation()); - } - 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(); - AppReproductionStrategy asr = new AppSpeciationReproduction(); - 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); - - for (App child: children_app1){ - System.out.println(child); - } - - for (App child: children_app2){ - System.out.println(child); - } - - } - -} +//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 index 34be693..fe68012 100644 --- a/diversim/src/main/java/diversim/strategy/extinction/AgingExtinctionStrategy.java +++ b/diversim/src/main/java/diversim/strategy/extinction/AgingExtinctionStrategy.java @@ -1,23 +1,25 @@ package diversim.strategy.extinction; +import diversim.App; import diversim.BipartiteGraph; import diversim.Entity; +import diversim.Platform; -public class AgingExtinctionStrategy implements ExtinctionStrategy { +public class AgingExtinctionStrategy implements AppExtinctionStrategy, PlatformExtinctionStrategy { private Entity entity = null; private int expectedAge = 0; private long born = 0; - public AgingExtinctionStrategy(Entity entity, long born, int expectedAge){ + public AgingExtinctionStrategy(Entity entity, BipartiteGraph graph, int expectedAge){ - this.born = born; + this.born = graph.schedule.getSteps(); this.expectedAge = expectedAge; this.entity = entity; } - @Override + public boolean die(BipartiteGraph graph) { long steps = graph.schedule.getSteps(); if( steps - born >= expectedAge){ @@ -28,4 +30,17 @@ public boolean die(BipartiteGraph graph) { } + + @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/ExtinctionStrategy.java b/diversim/src/main/java/diversim/strategy/extinction/ExtinctionStrategy.java deleted file mode 100644 index 9085023..0000000 --- a/diversim/src/main/java/diversim/strategy/extinction/ExtinctionStrategy.java +++ /dev/null @@ -1,11 +0,0 @@ -package diversim.strategy.extinction; - -import diversim.BipartiteGraph; - -/** - * The interface for extinction strategies - * @author Vivek Nallur - */ -public interface ExtinctionStrategy { - public boolean die(BipartiteGraph graph); -} diff --git a/diversim/src/main/java/diversim/strategy/extinction/OrphanExtinctionStrategy.java b/diversim/src/main/java/diversim/strategy/extinction/OrphanExtinctionStrategy.java deleted file mode 100644 index 12914e5..0000000 --- a/diversim/src/main/java/diversim/strategy/extinction/OrphanExtinctionStrategy.java +++ /dev/null @@ -1,26 +0,0 @@ -package diversim.strategy.extinction; - -import diversim.BipartiteGraph; -import diversim.Entity; - -public class OrphanExtinctionStrategy implements ExtinctionStrategy { - - private Entity entity = null; - - - public OrphanExtinctionStrategy(Entity entity){ - - this.entity = entity; - - } - - @Override - public boolean die(BipartiteGraph graph) { - if(entity.services.size() == 0) - return true; - if(entity.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/AllMatchingService.java b/diversim/src/main/java/diversim/strategy/matching/AllMatchingService.java similarity index 88% rename from diversim/src/main/java/diversim/AllMatchingService.java rename to diversim/src/main/java/diversim/strategy/matching/AllMatchingService.java index 582827a..d34c3b0 100644 --- a/diversim/src/main/java/diversim/AllMatchingService.java +++ b/diversim/src/main/java/diversim/strategy/matching/AllMatchingService.java @@ -1,7 +1,10 @@ -package diversim; +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 diff --git a/diversim/src/main/java/diversim/AnyMatchingService.java b/diversim/src/main/java/diversim/strategy/matching/AnyMatchingService.java similarity index 93% rename from diversim/src/main/java/diversim/AnyMatchingService.java rename to diversim/src/main/java/diversim/strategy/matching/AnyMatchingService.java index 4e13774..d5427df 100644 --- a/diversim/src/main/java/diversim/AnyMatchingService.java +++ b/diversim/src/main/java/diversim/strategy/matching/AnyMatchingService.java @@ -1,7 +1,10 @@ -package diversim; +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, diff --git a/diversim/src/main/java/diversim/MatchingStrategy.java b/diversim/src/main/java/diversim/strategy/matching/MatchingStrategy.java similarity index 91% rename from diversim/src/main/java/diversim/MatchingStrategy.java rename to diversim/src/main/java/diversim/strategy/matching/MatchingStrategy.java index 338e721..3301803 100644 --- a/diversim/src/main/java/diversim/MatchingStrategy.java +++ b/diversim/src/main/java/diversim/strategy/matching/MatchingStrategy.java @@ -1,4 +1,6 @@ -package diversim; +package diversim.strategy.matching; + +import diversim.Entity; /** * This interface defines the basic method for determining whether an entity diff --git a/diversim/src/main/java/diversim/strategy/reproduction/AbstractSpeciationReproduction.java b/diversim/src/main/java/diversim/strategy/reproduction/AbstractSpeciationReproduction.java deleted file mode 100644 index f98e07c..0000000 --- a/diversim/src/main/java/diversim/strategy/reproduction/AbstractSpeciationReproduction.java +++ /dev/null @@ -1,30 +0,0 @@ -package diversim.strategy.reproduction; - -import java.util.ArrayList; -import java.util.List; - -import diversim.Service; - -public class AbstractSpeciationReproduction { - - public AbstractSpeciationReproduction() { - super(); - } - - ArrayList strategies = new ArrayList(); - - List allServices = null; - - public List getAllServices(){ - return allServices; - } - - public void setAllServices(List allServices){ - this.allServices = new ArrayList(allServices); - } - - public List getStrategies(){ - return strategies; - } - -} \ No newline at end of file diff --git a/diversim/src/main/java/diversim/strategy/reproduction/AppClonalReproduction.java b/diversim/src/main/java/diversim/strategy/reproduction/AppClonalReproduction.java index 607de97..6c1c720 100644 --- a/diversim/src/main/java/diversim/strategy/reproduction/AppClonalReproduction.java +++ b/diversim/src/main/java/diversim/strategy/reproduction/AppClonalReproduction.java @@ -4,6 +4,7 @@ import java.util.ArrayList; import diversim.App; +import diversim.BipartiteGraph; import ec.util.MersenneTwisterFast; /** @@ -15,11 +16,14 @@ * @author Vivek Nallur */ public class AppClonalReproduction implements AppReproductionStrategy{ - public List reproduce(App parent, List other_apps){ + + @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 index 210041e..8887100 100644 --- a/diversim/src/main/java/diversim/strategy/reproduction/AppReproductionStrategy.java +++ b/diversim/src/main/java/diversim/strategy/reproduction/AppReproductionStrategy.java @@ -3,12 +3,13 @@ import java.util.List; import diversim.App; -import ec.util.MersenneTwisterFast; +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, List other_apps); + 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/AppSpeciationReproduction.java b/diversim/src/main/java/diversim/strategy/reproduction/AppSpeciationReproductionByDNA.java similarity index 54% rename from diversim/src/main/java/diversim/strategy/reproduction/AppSpeciationReproduction.java rename to diversim/src/main/java/diversim/strategy/reproduction/AppSpeciationReproductionByDNA.java index e6a496c..ae5ec56 100644 --- a/diversim/src/main/java/diversim/strategy/reproduction/AppSpeciationReproduction.java +++ b/diversim/src/main/java/diversim/strategy/reproduction/AppSpeciationReproductionByDNA.java @@ -4,6 +4,7 @@ import java.util.List; import diversim.App; +import diversim.BipartiteGraph; import ec.util.MersenneTwisterFast; @@ -14,24 +15,29 @@ * @author hui song * */ -public class AppSpeciationReproduction extends AbstractSpeciationReproduction implements AppReproductionStrategy { - + +public class AppSpeciationReproductionByDNA implements AppReproductionStrategy { + DNASpeciation speciator = null; + public AppSpeciationReproductionByDNA(DNASpeciation speciator){ + this.speciator = speciator; + } - @Override - public List reproduce(App parent, List other_apps) { + + public List reproduce(App parent, BipartiteGraph state) { MersenneTwisterFast rnd = new MersenneTwisterFast(System.nanoTime()); - SpeciationStrategy strategy = getStrategies().get(rnd.nextInt(getStrategies().size())); - App child = new App(rnd.nextInt(), - strategy.speciate(parent.getDependencies(),getAllServices()) + 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/ReductionSpeciation.java b/diversim/src/main/java/diversim/strategy/reproduction/DNAReductionSpeciation.java similarity index 52% rename from diversim/src/main/java/diversim/strategy/reproduction/ReductionSpeciation.java rename to diversim/src/main/java/diversim/strategy/reproduction/DNAReductionSpeciation.java index d8c1d35..e25390e 100644 --- a/diversim/src/main/java/diversim/strategy/reproduction/ReductionSpeciation.java +++ b/diversim/src/main/java/diversim/strategy/reproduction/DNAReductionSpeciation.java @@ -11,11 +11,11 @@ * @author Vivek Nallur */ -public class ReductionSpeciation implements SpeciationStrategy{ +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); + 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/SpeciationStrategy.java b/diversim/src/main/java/diversim/strategy/reproduction/DNASpeciation.java similarity index 88% rename from diversim/src/main/java/diversim/strategy/reproduction/SpeciationStrategy.java rename to diversim/src/main/java/diversim/strategy/reproduction/DNASpeciation.java index 5894fc1..b429fc5 100644 --- a/diversim/src/main/java/diversim/strategy/reproduction/SpeciationStrategy.java +++ b/diversim/src/main/java/diversim/strategy/reproduction/DNASpeciation.java @@ -10,7 +10,7 @@ * * This is the base interface for all speciation strategies */ -public interface SpeciationStrategy{ +public interface DNASpeciation{ public List speciate(List current_dna, List all_services); } diff --git a/diversim/src/main/java/diversim/strategy/reproduction/ExtensionSpeciation.java b/diversim/src/main/java/diversim/strategy/reproduction/ExtensionSpeciation.java deleted file mode 100644 index e7313b0..0000000 --- a/diversim/src/main/java/diversim/strategy/reproduction/ExtensionSpeciation.java +++ /dev/null @@ -1,26 +0,0 @@ -package diversim.strategy.reproduction; - -import java.util.List; -import java.util.ArrayList; -import java.util.Set; -import java.util.HashSet; - -import diversim.Service; -import ec.util.MersenneTwisterFast; -import sim.util.Bag; -/** - * This class implements the Extension speciation strategy - * @author Vivek Nallur - */ - -public class ExtensionSpeciation implements SpeciationStrategy{ - public List speciate(List current_dna, List all_services){ - 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/PlatformClonalReproduction.java b/diversim/src/main/java/diversim/strategy/reproduction/PlatformClonalReproduction.java index ef00a99..5d82fe8 100644 --- a/diversim/src/main/java/diversim/strategy/reproduction/PlatformClonalReproduction.java +++ b/diversim/src/main/java/diversim/strategy/reproduction/PlatformClonalReproduction.java @@ -3,6 +3,7 @@ import java.util.List; import java.util.ArrayList; +import diversim.BipartiteGraph; import diversim.Platform; import ec.util.MersenneTwisterFast; @@ -12,7 +13,7 @@ * @author Vivek Nallur */ public class PlatformClonalReproduction implements PlatformReproductionStrategy{ - public List reproduce(Platform parent, List other_platforms){ + 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()); diff --git a/diversim/src/main/java/diversim/strategy/reproduction/PlatformReproductionStrategy.java b/diversim/src/main/java/diversim/strategy/reproduction/PlatformReproductionStrategy.java index 4ae150c..c898383 100644 --- a/diversim/src/main/java/diversim/strategy/reproduction/PlatformReproductionStrategy.java +++ b/diversim/src/main/java/diversim/strategy/reproduction/PlatformReproductionStrategy.java @@ -2,6 +2,7 @@ import java.util.List; +import diversim.BipartiteGraph; import diversim.Platform; import ec.util.MersenneTwisterFast; @@ -10,5 +11,5 @@ * @author Vivek Nallur */ public interface PlatformReproductionStrategy{ - public List reproduce(Platform parent, List other_platforms); + 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/PlatformSpeciationReproduction.java b/diversim/src/main/java/diversim/strategy/reproduction/PlatformSpeciationReproduction.java deleted file mode 100644 index a963374..0000000 --- a/diversim/src/main/java/diversim/strategy/reproduction/PlatformSpeciationReproduction.java +++ /dev/null @@ -1,28 +0,0 @@ -package diversim.strategy.reproduction; - -import java.util.ArrayList; -import java.util.List; - -import diversim.Platform; -import diversim.Service; -import ec.util.MersenneTwisterFast; - -public class PlatformSpeciationReproduction - extends AbstractSpeciationReproduction - implements PlatformReproductionStrategy { - - - - @Override - public List reproduce(Platform parent, - List other_platforms) { - // TODO Auto-generated method stub - MersenneTwisterFast rnd = new MersenneTwisterFast(System.nanoTime()); - SpeciationStrategy strategy = getStrategies().get(rnd.nextInt(getStrategies().size())); - List services = strategy.speciate(parent.services, getAllServices()); - List children = new ArrayList(); - children.add(new Platform(rnd.nextInt(), services, parent.getLoadingFactor())); - return children; - } - -} 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 From 6eb9de83318b84075a5fd9c9d3c3fecd02aa2e6e Mon Sep 17 00:00:00 2001 From: Hui Song Date: Wed, 11 Sep 2013 16:02:29 +0200 Subject: [PATCH 11/11] forget something?