Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
9 changes: 6 additions & 3 deletions diversim/diversim.sh
Original file line number Diff line number Diff line change
@@ -1,11 +1,14 @@
#!/bin/bash

binDir=$HOME/workspace/diversim/bin # where are the binaries
libDir=$HOME/Diversify/mason # where are the libs
# where are the binaries
binDir=$HOME/ownCloud/neutral_model/diversify/diversim/src/main/java

# where are the libs
libDir=$HOME/ownCloud/tcd/mason

libs=$libDir/jmf.jar:$libDir/itext-1.2.jar:$libDir/portfolio.jar:$libDir/jcommon-1.0.16.jar:$libDir/jfreechart-1.0.13.jar:$libDir/jar/mason.17.jar

diversim_cmdLine="java -Xmx256M -cp $binDir:$libs diversim.BipartiteGraph" # WithUI
diversim_cmdLine="java -Xmx256M -cp $binDir:$libs diversim.BipartiteGraphWithUI" # WithUI
diversim_params="-for 2000" # 2>/dev/null -seed 1 -time 1000 -for 200000

if [ $# -lt 1 ] ; then
Expand Down
178 changes: 136 additions & 42 deletions diversim/src/main/java/diversim/App.java
Original file line number Diff line number Diff line change
Expand Up @@ -2,53 +2,147 @@


import java.util.List;
import java.util.ArrayList;
import java.util.HashSet;
import java.util.Set; // Interface

import diversim.strategy.extinction.AppExtinctionStrategy;
import diversim.strategy.reproduction.AppReproductionStrategy;
import ec.util.MersenneTwisterFast;
import sim.engine.SimState;

/**
* Apps must be injected in the simulation via the createApp() method in the BipartiteGraph class.
*
* @author Marco Biazzini
* Apps rely on specific services to function
* @author Vivek Nallur
*
* In each step, an App has the authority may choose to reproduce itself, either via clone or
* speciation, decided by the {@link #reproducers}, which are all instances of
* {@ AppReproductionStrategy}.
*
* An App dies if a instance of {@ AppExtinctionStrategy} in {@link #killers} suggests so. A
* {@link #dead} app will not reproduce, and will also be removed from the bipartite graph.
*
* @author Hui Song
*/
public class App extends Entity {

double redundancy = 0;


public double getRedundancy() {
return redundancy;
}


public App(int id, List<Service> servs) {
super(id);
for (Service s : servs) {
BipartiteGraph.addUnique(services, s);
}
}


/*
* (non-Javadoc)
* @see diversim.Entity#step(sim.engine.SimState)
*/
@Override
public void step(SimState state) {
BipartiteGraph graph = (BipartiteGraph) state;

// TODO something

redundancy = ((double)degree) / graph.numPlatforms;
System.out.println("Step " + state.schedule.getSteps() + " : " + toString());
}


@Override
public String toString() {
String res = super.toString();
res += " ; redundancy = " + redundancy;
return res;
}



double redundancy = 0;

public boolean dead = false;

public List<Platform> platforms = new ArrayList<Platform>();


public double getRedundancy() {
return redundancy;
}

List<AppReproductionStrategy> reproducers;
List<AppExtinctionStrategy> killers;


public List<Service> getDependencies(){
return this.services;
}

public void addDependencies(List<Service> 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<Service> obs_deps){
// The removeAll method should ideally execute in O(n) time
// however at least as of java 1.7, it does not. Inserting
// the obsolete_dependencies into a HashSet ensures that array
// compaction happens only once, and thus is reasonably optimal

this.services.removeAll(new HashSet(obs_deps));
}




public List<App> reproduce(BipartiteGraph state){
List<App> result = new ArrayList<App>();
for(AppReproductionStrategy reproducer : reproducers){
result.addAll(reproducer.reproduce(this, state));
}
return result;
}


public App(int id, List<Service> service_dependencies) {
super(id);
this.services = new ArrayList<Service> (service_dependencies);

// TODO: Now only use AppSpeciationReproduction, may need to include others
//Not initialized from 0, so that new Apps will always survice to their first
// steps.
degree = -1;
}

public void initStrategies(BipartiteGraph graph){
this.reproducers = StrategyFactory.fINSTANCE.createAppReproductionStrategy(this, graph);
this.killers = StrategyFactory.fINSTANCE.createAppExtinctionStrategies(this, graph);
}


/*
* (non-Javadoc)
* @see diversim.Entity#step(sim.engine.SimState)
*
* This is where we want the command pattern to be invoked from the bipartite
* graph.
*/
@Override
public void step(SimState state) {
BipartiteGraph graph = (BipartiteGraph) state;

// TODO something

if(dieOrNot(graph)){
return;
}

MersenneTwisterFast rnd = new MersenneTwisterFast(System.nanoTime());


List<App> newApps = reproduce(graph);
for(App app : newApps){
graph.addApp(app);
}

redundancy = ((double)degree) / graph.numPlatforms;
if(redundancy < 0) redundancy = 0;
System.out.println("Step " + state.schedule.getSteps() + " : " + toString());
}


@Override
public String toString() {
String res = super.toString();
res += " ; redundancy = " + redundancy;
return res;
}

/**
* Many killers, any of them could kill an App
*
* TODO: should introduce a "Die Strategy"
*/
public boolean dieOrNot(BipartiteGraph graph){
if(dead)
return true;
for(AppExtinctionStrategy killer : killers){
if( killer.die(this, graph)){
this.dead = true;
return true;
}
}
return false;
}

}
84 changes: 79 additions & 5 deletions diversim/src/main/java/diversim/BipartiteGraph.java
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,27 @@
* consistent updating (see comments about the start() method).
* More info in the comments of the methods.
*
* In a nutshell:
* We have a two communities of species: platforms and apps
* Each specie of platform is description of the set of services it supports.
* Each individual platform-instance is an individual of a particular specie.
* There can be many species, but a specie with no individual
* platform-instances is extinct.
*
* Each specie of app is a description of the set of services it needs. Each
* app-instance is an individual of a particular specie. If an app-instance
* cannot find a platform-instance that supports at least its required set of
* services, it dies.
*
* Each platform-instance supports one app-instance.
*
* For visualization purposes:
* We need to show specie-level interactions, perhaps(?) instead of individual
* level interactions.
*
* @author Marco Biazzini
* @author Vivek Nallur
* @author Hui Song
*
*/
public class BipartiteGraph extends SimState {
Expand All @@ -42,7 +62,7 @@ public class BipartiteGraph extends SimState {
/**
* Initial total number of services
*/
int initServices = 30;
public static int initServices = 30;

/**
* Max number of links a platform bears without triggering some diversification rule.
Expand All @@ -54,6 +74,12 @@ public class BipartiteGraph extends SimState {
*/
int platformMinSize = 3;

/**
* x out of 100 possibility to allow an App to reproduce itself.
*/
int percentAppReproduce = 1; // 1%
int percentPlatformReproduce = 1; // 1%

/**
* Current number of platforms.
*/
Expand Down Expand Up @@ -102,6 +128,9 @@ public class BipartiteGraph extends SimState {
private int aCounter;
public boolean changed;

public static BipartiteGraph INSTANCE = null;



/**
* Getters and setters.
Expand Down Expand Up @@ -234,24 +263,28 @@ private void init() {
public BipartiteGraph(long seed) {
super(seed);
init();
INSTANCE = this;
}


public BipartiteGraph(MersenneTwisterFast random) {
super(random);
init();
INSTANCE = this;
}


public BipartiteGraph(MersenneTwisterFast random, Schedule schedule) {
super(random, schedule);
init();
INSTANCE = this;
}


public BipartiteGraph(long seed, Schedule schedule) {
super(seed, schedule);
init();
INSTANCE = this;
}


Expand Down Expand Up @@ -279,21 +312,25 @@ public void start() {
services.add(new Service(++sCounter));
numServices++;
}



// create platforms
for (int i = 0; i < initPlatforms; i++) { // all services to each platform
createPlatform(services);
Platform pltf = createPlatform(services);
pltf.initStategies(this);
}

// create apps
for (int i = 0; i < initApps; i++) {
createApp(selectServices());
App app = createApp(selectServices());
app.initStrategies(this);
}


// create the fate agent
fate = new Fate(random);
schedule.scheduleRepeating(schedule.getTime() + 1.2, fate, 1.0);
//fate = new Fate(random);
//schedule.scheduleRepeating(schedule.getTime() + 1.2, fate, 1.0);

// define initial network:
// link every platform to all apps that use at least one of its services
Expand All @@ -308,12 +345,15 @@ public void start() {
// then the network printout, then fate.
Steppable print = new Steppable() {
public void step(SimState state) {
//System.out.println("Step " + state.schedule.getSteps() + " : " + "what happened?");
if (changed)
printoutNetwork();
changed = false;
}
};
schedule.scheduleRepeating(schedule.getTime() + 1.1, print, 1.0);
schedule.scheduleRepeating(schedule.getTime()+1, new ReConnect());

}


Expand Down Expand Up @@ -373,6 +413,21 @@ public App createApp(List<Service> 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.
Expand Down Expand Up @@ -434,6 +489,13 @@ public void createLinks(Entity e, ArrayList<? extends Entity> entities) {

}

public void setLink(App app, Platform pltf){
bipartiteNetwork.addEdge(app, pltf, 1);
app.degree ++;
pltf.degree ++;
changed = true;
}


/**
* Textual printout of the network
Expand Down Expand Up @@ -520,5 +582,17 @@ static public void printAny(Object data, String trailer, PrintStream out) {
out.flush();
}

public Platform addPlatform(Platform platform) {
bipartiteNetwork.addNode(platform);
platforms.add(platform);
numPlatforms++;
changed = true;
schedule.scheduleRepeating(platform);
return platform;

}




}
Loading