239 lines
6.3 KiB
Java
239 lines
6.3 KiB
Java
import java.io.BufferedReader;
|
|
import java.io.IOException;
|
|
import java.io.InputStreamReader;
|
|
import java.util.ArrayList;
|
|
import java.util.List;
|
|
import java.util.Random;
|
|
import java.util.logging.LogManager;
|
|
|
|
import analysis.NetworkDumper;
|
|
|
|
import node.Node;
|
|
|
|
public class RandomGenerator {
|
|
public static int initNodeCount = 250;
|
|
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();
|
|
|
|
/**
|
|
* @param args
|
|
* @throws IOException
|
|
*/
|
|
public static void main(String[] args) throws IOException {
|
|
double rate;
|
|
int add = 0;
|
|
int remove = 0;
|
|
|
|
System.setProperty("java.util.logging.config.file",
|
|
"logging.properties");
|
|
|
|
add(initNodeCount);
|
|
|
|
try {
|
|
LogManager.getLogManager().readConfiguration();
|
|
} catch (Exception e) {
|
|
e.printStackTrace();
|
|
}
|
|
|
|
BufferedReader in = new BufferedReader(new InputStreamReader(System.in));
|
|
String s;
|
|
while ((s = in.readLine()) != null && s.length() != 0) {
|
|
String[] splitted = s.split(" ");
|
|
|
|
String cmd = splitted[0];
|
|
int count = 0;
|
|
if (splitted.length > 1) {
|
|
count = Integer.valueOf(splitted[1]);
|
|
}
|
|
|
|
switch (cmd) {
|
|
|
|
case "dump":
|
|
nodes.get(0).gatherInformationOfNetwork();
|
|
try {
|
|
Thread.sleep(5000);
|
|
} catch (InterruptedException e) {
|
|
e.printStackTrace();
|
|
}
|
|
|
|
NetworkDumper d = new NetworkDumper(nodes.get(0));
|
|
d.write(d.networkToDot(nodes.get(0).getNetwork()));
|
|
break;
|
|
case "add":
|
|
add(count);
|
|
System.out.println("Nodecount: " + nodes.size());
|
|
break;
|
|
case "remove":
|
|
if (count < nodes.size()) {
|
|
remove(count);
|
|
} else {
|
|
System.out.println("Can't remove that many nodes.");
|
|
}
|
|
System.out.println("Nodecount: " + nodes.size());
|
|
break;
|
|
case "populate":
|
|
if (splitted.length == 4) {
|
|
for (int i = 0; i < count; i++) {
|
|
System.out.println("Round: " + (i + 1));
|
|
System.out.println("Nodecount: " + nodes.size());
|
|
int oldsize = nodes.size();
|
|
add((int) Math.round(oldsize
|
|
* Double.valueOf(splitted[2])));
|
|
remove((int) Math.round(oldsize
|
|
* Double.valueOf(splitted[3])));
|
|
}
|
|
} else {
|
|
System.out.println("command has to have 3 parameters.");
|
|
}
|
|
System.out.println("Nodecount: " + nodes.size());
|
|
break;
|
|
case "simulate":
|
|
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++) {
|
|
System.out.println("Round: " + (i + 1));
|
|
int oldsize = nodes.size();
|
|
add((int) Math.round(oldsize * initBirthRate));
|
|
remove((int) Math.round(oldsize * initDeathRate));
|
|
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;
|
|
case "prefsim":
|
|
if (splitted.length > 2) {
|
|
rate = Double.valueOf(splitted[2]);
|
|
int neighborcount = Integer.valueOf(splitted[3]);
|
|
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) {
|
|
removeWithNeighborCount(1, neighborcount);
|
|
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;
|
|
default:
|
|
System.out.println("Unknown command.");
|
|
break;
|
|
}
|
|
}
|
|
}
|
|
|
|
public static void add(int count) throws IOException {
|
|
for (int i = 0; i < count; i++) {
|
|
if (nodes.size() == 0) {
|
|
Node newNode = new Node();
|
|
nodes.add(newNode);
|
|
neighborCount.add(1);
|
|
} 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(1);
|
|
|
|
}
|
|
}
|
|
}
|
|
|
|
public static void remove(int count) {
|
|
for (int i = 0; i < count; i++) {
|
|
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);
|
|
|
|
}
|
|
}
|
|
}
|
|
|
|
}
|