added some new features, for example to remove nodes with a min neighborcount

This commit is contained in:
Denis 2012-12-09 14:01:29 +01:00
parent 911c2f2313
commit 59916d04e7

View File

@ -11,12 +11,13 @@ import analysis.NetworkDumper;
import node.Node;
public class RandomGenerator {
public static int initNodeCount = 99;
public static int initNodeCount = 100;
public static double initBirthRate = 0.50;
public static double initDeathRate = 0.20;
public static int initRounds = 13;
public static List<Node> nodes = new ArrayList<Node>();
public static List<Integer> neighborCount = new ArrayList<Integer>();
public static Random gen = new Random();
/**
@ -27,8 +28,7 @@ public class RandomGenerator {
System.setProperty("java.util.logging.config.file",
"logging.properties");
nodes.add(new Node());
add(initNodeCount);
try {
@ -88,12 +88,40 @@ public class RandomGenerator {
}
System.out.println("Nodecount: " + nodes.size());
break;
case "status":
if(count < nodes.size()){
System.out.println(nodes.get(count));
} else {
System.out.println("Node doesn't exist. Max index at the moment is " + (nodes.size() - 1));
case "simulate":
double rate;
int add = 0;
int remove = 0;
if (splitted.length > 1) {
rate = Double.valueOf(splitted[2]);
for (int i = 0; i < count; i++) {
System.out.println("Round: " + i);
System.out.println("NodeCount: " + nodes.size());
if (gen.nextDouble() < rate) {
add(1);
add++;
}
if (gen.nextDouble() < rate) {
remove(1);
remove++;
}
if (i % (count / 4) == 0) {
nodes.get(0).gatherInformationOfNetwork();
try {
System.out.println(i);
Thread.sleep(500);
} catch (InterruptedException e) {
e.printStackTrace();
}
NetworkDumper nd = new NetworkDumper(nodes.get(0));
nd.write(nd.networkToDot(nodes.get(0).getNetwork()));
}
}
}
System.out.println("Nodecount: " + nodes.size());
System.out.println("We added " + add + " Nodes");
System.out.println("We removed " + remove + " Nodes");
break;
case "go":
for (int i = 0; i < initRounds; i++) {
@ -104,6 +132,15 @@ public class RandomGenerator {
System.out.println("Nodecount: " + nodes.size());
}
break;
case "prioritize":
int neighbors = Integer.valueOf(splitted[2]);
if (count < nodes.size()) {
removeWithNeighborCount(count, neighbors);
} else {
System.out.println("Can't remove that many nodes.");
}
System.out.println("Nodecount: " + nodes.size());
break;
default:
System.out.println("Unknown command.");
break;
@ -113,17 +150,53 @@ public class RandomGenerator {
public static void add(int count) throws IOException {
for (int i = 0; i < count; i++) {
Node newNode = nodes.get(gen.nextInt(nodes.size())).spawn();
nodes.add(newNode);
if (nodes.size() == 0) {
Node newNode = new Node();
nodes.add(newNode);
neighborCount.add(0);
} else {
int index = gen.nextInt(nodes.size());
Node newNode = nodes.get(index).spawn();
neighborCount.set(index,
neighborCount.get(index).intValue() + 1);
nodes.add(newNode);
neighborCount.add(0);
}
}
}
public static void remove(int count) {
for (int i = 0; i < count; i++) {
int index = gen.nextInt(nodes.size());
if (index < nodes.size()) {
int index = 0;
if (0 < nodes.size()) {
index = gen.nextInt(nodes.size());
nodes.get(index).leave();
nodes.remove(index);
neighborCount.remove(index);
}
}
}
public static void removeWithNeighborCount(int count, int neighbors) {
for (int i = 0; i < count; i++) {
int index = 0;
boolean done = false;
if (0 < nodes.size()) {
do {
index = gen.nextInt(nodes.size());
System.out.println("Node " + index + " has " + neighborCount.get(index) + " neighbors.");
if (neighborCount.get(index) >= neighbors) {
System.out.println("Node " + index + " had "
+ neighborCount.get(index) + " neighbors >= "
+ neighbors);
nodes.get(index).leave();
nodes.remove(index);
neighborCount.remove(index);
done = true;
}
} while (!done);
}
}
}