Hi all, for the last few days, I succeeded in constructing a code that
represents a network and infection transmisson through it. This is the
first time ever that i wrote a code. So i am wondering how i validate
my code although it may sound a philosophical question. And also, I
want someone who are good at java programming to review my code. I
would really appreciate it if anyone would review my code ??
it has 4 classes.
/**
* DoubletsOrTriplets.java
*/
import java.util.ArrayList;
public class DoubletsOrTriplets {
String string;
int value;
/**
Constructors
*/
public DoubletsOrTriplets () {
string = "";
value = 0;
}
public DoubletsOrTriplets (String otherString) {
string = otherString;
value = 0;
}
public DoubletsOrTriplets (String otherString, int x) {
string = otherString;
value = x;
}
/**
setters
*/
public void setValue (String otherString, int x) {
string = otherString;
value = x;
}
public void printSelf () {
System.out.printf ("%s = %d\n", string, value);
}
----------------------------------------------------------------------------------------------
/**
* GayEdge.java
*
*/
import java.awt.Color;
import uchicago.src.sim.gui.DrawableEdge;
import uchicago.src.sim.gui.SimGraphics;
import uchicago.src.sim.network.DefaultEdge;
import uchicago.src.sim.network.Node;
/**
* The edge between GayNodes.
*/
public class GayEdge extends DefaultEdge implements DrawableEdge {
private Color color;
public GayEdge() {}
public GayEdge(Node from, Node to, Color color) {
super(from, to, "");
this.color = color;
}
public void setColor(Color c) {
color = c;
}
public void draw(SimGraphics g, int fromX, int toX, int fromY, int
toY) {
g.drawDirectedLink(color, fromX, toX, fromY, toY);
}
}
------------------------------------------------------------------------------------------------------------------
/**
* GayNode.java
*/
import java.awt.Color;
import java.util.ArrayList;
import uchicago.src.sim.gui.RectNetworkItem;
import uchicago.src.sim.network.DefaultDrawableNode;
import uchicago.src.sim.network.DefaultNode;
import uchicago.src.sim.network.Edge;
import uchicago.src.sim.util.Random;
public class GayNode extends DefaultDrawableNode {
String infectStatus = "s";
Color color;
public GayNode (int x, int y) {
RectNetworkItem rect = new RectNetworkItem(x, y);
setDrawable(rect);
infectStatus = "s";
}
public void setInfectStatus (String string) {
infectStatus = string;
}
public String getInfectStatus () {
return infectStatus;
}
/**
* Makes an edge to the specified node and from the specifed node
to
* thisGayNode if both nodes do not already have edges to each
* other. The edges is displayed in the specified color.
*/
public void makeEdgeToFrom(DefaultNode node, Color color) {
if (! hasEdgeTo(node)) {
Edge edge = new GayEdge(this, node, color);
addOutEdge(edge);
node.addInEdge(edge);
Edge otherEdge = new GayEdge(node, this, color);
node.addOutEdge(otherEdge);
addInEdge(otherEdge);
}
}
/**
* Creates a new edge between this GayNode and a node chosen
* at random from the specified list. This edge is created via
* <tt>makeEdgeFromTo</tt> and so those conditions must be met as
* well.
*/
public void meetRandom(ArrayList list) {
int index = Random.uniform.nextIntFromTo(0, list.size() - 1);
GayNode node = (GayNode)list.get(index);
while (this.equals(node)) {
index = Random.uniform.nextIntFromTo(0, list.size() - 1);
node = (GayNode)list.get(index);
}
makeEdgeToFrom(node, Color.white);
}
public void removePartners() {
GayNode jNode = (GayNode)getRandomNodeOut();
// will be null if no outEdges.
if (jNode != null) {
removeEdgesTo(jNode);
jNode.removeEdgesFrom(this);
removeEdgesFrom(jNode);
jNode.removeEdgesTo(this);
}
}
}
-------------------------------------------------------------------------------------
/**
* GayModel.java
*/
import uchicago.src.reflector.ListPropertyDescriptor;
import uchicago.src.sim.analysis.Histogram;
import uchicago.src.sim.analysis.NetSequenceGraph;
import uchicago.src.sim.analysis.OpenSequenceGraph;
import uchicago.src.sim.analysis.Plot;
import uchicago.src.sim.analysis.PlotModel;
import uchicago.src.sim.engine.BasicAction;
import uchicago.src.sim.engine.Controller;
import uchicago.src.sim.engine.Schedule;
import uchicago.src.sim.engine.SimModelImpl;
import uchicago.src.sim.gui.*;
import uchicago.src.sim.util.Random;
import uchicago.src.sim.analysis.OpenStats;
import uchicago.src.sim.analysis.Sequence;
import java.awt.*;
//import java.util.ArrayList;
//import java.util.Vector;
import java.awt.Color;
import java.util.*;
//import java.lang.Object;
public class GayModel extends SimModelImpl {
// model variables
private int numNodes = 50;
private int initInfNodes = 5;
private int worldXSize = 400;
private int worldYSize = 400;
private int updateEveryN = 5;
private int initialSteps = 1;
private int maxDegree = 10;
private int ss = 0, se1 = 0, se2 = 0, sa1 = 0, sa2 =
0, sa3 = 0, sl = 0;
private int e1e1 = 0, e1e2 = 0, e1a1 = 0,e1a2 = 0,
e1a3 = 0, e1l = 0;
private int e2e2 = 0, e2a1 = 0, e2a2 = 0, e2a3 = 0,
e2l = 0;
private int a1a1 = 0, a1a2 = 0, a1a3 = 0, a1l = 0;
private int a2a2 = 0, a2a3 = 0, a2l = 0;
private int a3a3 = 0, a3l = 0;
private int ll = 0;
private int tickCount = 0;
private double removeProb = 0.01;
private double partnershipProb = 0.01;
private double probFromE1ToE2 = 0.05, probFromA1ToA2 =
0.015, probFromLToS = 0.005;
private double transProbAtE = 0.08, transProbAtA =
0.002, transProbAtL = 0.08;
private double meanDegree = 0;
// arraylists
private ArrayList agentList = new ArrayList();
private ArrayList doubletList = new ArrayList();
// implementation variables
private String layoutType = "KK";
private DisplaySurface surface;
private Schedule schedule;
private AbstractGraphLayout graphLayout;
private Histogram degreeDist;
private boolean showHist = true;
private NetSequenceGraph graph, otherGraph;
private boolean showPlot = false;
private BasicAction initialAction;
private OpenSequenceGraph seqGraph;
private boolean showPrev = true;
public GayModel() {
Vector vect = new Vector();
vect.add("Fruch");
vect.add("KK");
vect.add("CircleLayout");
ListPropertyDescriptor pd = new ListPropertyDescriptor("LayoutType",
vect);
// the name of the property: LayoutType
// vect - the objects to be displayed in the combo box and passed
// to the appropriate set method.
descriptors.put("LayoutType", pd);
}
// get and set accessor methods
public String getLayoutType() {
return layoutType;
}
public void setLayoutType(String type) {
layoutType = type;
}
public boolean getDegreeHist() {
return showHist;
}
public void setDegreeHist(boolean val) {
showHist = val;
}
public boolean getPrevGraph() {
return showPrev;
}
public void setPrevGraph(boolean val) {
showPrev = val;
}
public boolean getPlot() {
return showPlot;
}
public void setPlot(boolean val) {
showPlot = val;
}
public double getRemoveProb() {
return removeProb;
}
public void setRemoveProb(double prob) {
removeProb = prob;
}
public double getPartnershipProb() {
return partnershipProb;
}
public void setPartnershipProb(double prob) {
partnershipProb = prob;
}
public void setNumNodes(int n) {
numNodes = n;
}
public int getNumNodes() {
return numNodes;
}
public int getInitInfNodes() {
return initInfNodes;
}
public void setInitInfNodes(int num) {
initInfNodes = num;
}
public double getProbFromE1ToE2() {
return probFromE1ToE2;
}
public void setProbFromE1ToE2(double prob) {
probFromE1ToE2 = prob;
}
public double getProbFromA1ToA2() {
return probFromA1ToA2;
}
public void setProbFromA1ToA2(double prob) {
probFromA1ToA2 = prob;
}
public double getProbFromLToS() {
return probFromLToS;
}
public void setProbFromLToS(double prob) {
probFromLToS = prob;
}
public double getTransProbAtE() {
return transProbAtE;
}
public void setTransProbAtE(double tp) {
transProbAtE = tp;
}
public double getTransProbAtA() {
return transProbAtA;
}
public void setTransProbAtA(double tp) {
transProbAtA = tp;
}
public double getTransProbAtL() {
return transProbAtL;
}
public void setTransProbAtL(double tp) {
transProbAtL = tp;
}
public int getMaxDegree() {
return maxDegree;
}
public void setMaxDegree(int degree) {
maxDegree = degree;
}
public void setUpdateEveryN(int updates) {
updateEveryN = updates;
}
public int getUpdateEveryN() {
return updateEveryN;
}
public int getStartRemoveAfter() {
return initialSteps;
}
public void setStartRemoveAfter(int steps) {
initialSteps = steps;
}
// arraylist methods
// print the component of the doublet (or triplet) list
public void printList (ArrayList list) {
for (int i = 0; i < list.size(); i++) {
DoubletsOrTriplets doublet = new DoubletsOrTriplets();
doublet = (DoubletsOrTriplets)list.get(i);
doublet.printSelf();
}
}
// builds the model
public void buildModel() {
for (int i = 0; i < (numNodes - initInfNodes); i++) {
int x = Random.uniform.nextIntFromTo(0, worldXSize - 1);
int y = Random.uniform.nextIntFromTo(0, worldYSize - 1);
GayNode node = new GayNode(x, y);
node.setColor (Color.gray);
agentList.add(node);
}
for (int i = 0; i < initInfNodes; i++) {
int x = Random.uniform.nextIntFromTo(0, worldXSize - 1);
int y = Random.uniform.nextIntFromTo(0, worldYSize - 1);
GayNode node = new GayNode(x, y);
node.setColor (Color.green);
node.setInfectStatus ("a1");
agentList.add(node);
}
if (showHist) makeHistogram();
if (showPlot) makePlot();
if (showPrev) displayPrevalence();
}
private void randomMeeting() {
for (int i = 0; i < numNodes; i++) {
GayNode iNode = (GayNode) agentList.get(i);
double aDouble = Random.uniform.nextDoubleFromTo(0, 1);
if (partnershipProb > aDouble) {
iNode.meetRandom(agentList);
} }
}
/**
it's very possible if the number of edges increases then
infectProbability will increase over 1.
*/
private void infectPartners () {
for (int i = 0; i < numNodes; i++) {
int ePartners = 0, aPartners = 0, lPartners = 0;
GayNode iNode = (GayNode) agentList.get(i);
String aString = iNode.getInfectStatus();
int degree = iNode.getInDegree();
if (aString.equals("s") && degree != 0) {
ArrayList list = iNode.getInNodes();
for (int j = 0; j < list.size(); j++) {
GayNode jNode = (GayNode)list.get(j);
String otherString = jNode.getInfectStatus();
if (otherString.equals("e1") || otherString.equals("e2")) {
ePartners += 1;
}
else if (otherString.equals("a1") || otherString.equals("a2") ||
otherString.equals("a3")) {
aPartners += 1;
}
else if (otherString.equals("l")) {
lPartners += 1;
}
}
//System.out.printf ("ePartners=%d, aPartners = %d, lPartners =
%d\n", ePartners, aPartners, lPartners);
double infectionPartners = ePartners * transProbAtE + aPartners *
transProbAtA + lPartners * transProbAtL;
double safeProb = Random.uniform.nextDoubleFromTo(0, 1);
//System.out.printf ("infectionPartners = %.4f, safeProb = %.4f\n",
infectionPartners, safeProb);
if (infectionPartners > safeProb) {
iNode.setColor (Color.magenta);
iNode.setInfectStatus ("e1");
agentList.add (iNode);
} } }
}
private void randomProgress () {
for (int i = 0; i < numNodes; i++) {
GayNode iNode = (GayNode) agentList.get(i);
String string = iNode.getInfectStatus();
double aDouble = Random.uniform.nextDoubleFromTo(0, 1);
if (string.equals("e1") && probFromE1ToE2 > aDouble ) {
iNode.setColor(Color.blue);
iNode.setInfectStatus("e2");
}
else if (string.equals("e2") && probFromE1ToE2 > aDouble ) {
iNode.setColor(Color.green);
iNode.setInfectStatus("a1");
}
else if (string.equals("a1") && probFromA1ToA2 > aDouble ) {
iNode.setColor(Color.yellow);
iNode.setInfectStatus("a2");
}
else if (string.equals("a2") && probFromA1ToA2 > aDouble ) {
iNode.setColor(Color.orange);
iNode.setInfectStatus("a3");
}
else if (string.equals("a3") && probFromA1ToA2 > aDouble ) {
iNode.setColor(Color.red);
iNode.setInfectStatus("l");
}
else if (string.equals("l") && probFromLToS > aDouble ) {
iNode.setColor(Color.gray);
iNode.setInfectStatus("s");
}
agentList.add (iNode);
}
}
private void removeRandomPartners() {
for (int i = 0; i < numNodes; i++) {
GayNode node = (GayNode) agentList.get(i);
int degree = node.getOutDegree(); // gets outdegree of node
double aDouble = Random.uniform.nextDoubleFromTo(0, 1);
if (removeProb > aDouble && degree > 0) {
node.removePartners();
}
}
}
public double getMeanDegree() {
double degree = 0;
for (int i = 0; i < numNodes; i++) {
GayNode node = (GayNode) agentList.get(i);
degree += node.getOutDegree(); // gets outdegree of a node
}
meanDegree = degree/(double)numNodes;
return meanDegree;
}
public ArrayList valueDoubletList (ArrayList list) {
tickCount += 1; // This tick count doesn't reset to zero when I reset
the model.
System.out.printf("Tick count = %d\n", tickCount);
list.clear();
int ss = 0, se1 = 0, se2 = 0, sa1 = 0, sa2 = 0, sa3 = 0, sl = 0;
int e1e1 = 0, e1e2 = 0, e1a1 = 0,e1a2 = 0, e1a3 = 0, e1l = 0;
int e2e2 = 0, e2a1 = 0, e2a2 = 0, e2a3 = 0, e2l = 0;
int a1a1 = 0, a1a2 = 0, a1a3 = 0, a1l = 0;
int a2a2 = 0, a2a3 = 0, a2l = 0;
int a3a3 = 0, a3l = 0;
int ll = 0;
for (int i = 0; i < numNodes; i++) {
GayNode node = (GayNode) agentList.get(i);
String string = node.getInfectStatus();
ArrayList otherList = node.getOutEdges();
for ( int j = 0; j < otherList.size(); j++) {
GayEdge edge =(GayEdge)otherList.get(j);
GayNode otherNode = (GayNode)edge.getTo();
String otherString = otherNode.getInfectStatus();
if (string.equals("s")) {
if (otherString.equals("s")) { ss += 1; }
if (otherString.equals("e1")) { se1 += 1; }
if (otherString.equals("e2")) { se2 += 1; }
if (otherString.equals("a1")) { sa1 += 1; }
if (otherString.equals("a2")) { sa2 += 1; }
if (otherString.equals("a3")) { sa3 += 1; }
if (otherString.equals("l")) { sl += 1; }
}
else if (string.equals("e1")) {
if (otherString.equals("s")) { se1 += 0; }
if (otherString.equals("e1")) { e1e1 += 1; }
if (otherString.equals("e2")) { e1e2 += 1; }
if (otherString.equals("a1")) { e1a1 += 1; }
if (otherString.equals("a2")) { e1a2 += 1; }
if (otherString.equals("a3")) { e1a3 += 1; }
if (otherString.equals("l")) { e1l += 1; }
}
else if (string.equals("e2")) {
if (otherString.equals("s")) { se2 += 0; }
if (otherString.equals("e1")) { e1e2 += 0; }
if (otherString.equals("e2")) { e2e2 += 1; }
if (otherString.equals("a1")) { e2a1 += 1; }
if (otherString.equals("a2")) { e2a2 += 1; }
if (otherString.equals("a3")) { e2a3 += 1; }
if (otherString.equals("l")) { e2l += 1; }
}
else if (string.equals("a1")) {
if (otherString.equals("s")) { sa1 += 0; }
if (otherString.equals("e1")) { e1a1 += 0; }
if (otherString.equals("e2")) { e2a1 += 0; }
if (otherString.equals("a1")) { a1a1 += 1; }
if (otherString.equals("a2")) { a1a2 += 1; }
if (otherString.equals("a3")) { a1a3 += 1; }
if (otherString.equals("l")) { a1l += 1; }
}
else if (string.equals("a2")) {
if (otherString.equals("s")) { sa2 += 0; }
if (otherString.equals("e1")) { e1a2 += 0; }
if (otherString.equals("e2")) { e2a2 += 0; }
if (otherString.equals("a1")) { a1a2 += 0; }
if (otherString.equals("a2")) { a2a2 += 1; }
if (otherString.equals("a3")) { a2a3 += 1; }
if (otherString.equals("l")) { a2l += 1; }
}
else if (string.equals("a3")) {
if (otherString.equals("s")) { sa3 += 0; }
if (otherString.equals("e1")) { e1a3 += 0; }
if (otherString.equals("e2")) { e2a3 += 0; }
if (otherString.equals("a1")) { a1a3 += 0; }
if (otherString.equals("a2")) { a2a3 += 0; }
if (otherString.equals("a3")) { a3a3 += 1; }
if (otherString.equals("l")) { a3l += 1; }
}
else if (string.equals("l")) {
if (otherString.equals("s")) { sl += 0; }
if (otherString.equals("e1")) { e1l += 0; }
if (otherString.equals("e2")) { e2l += 0; }
if (otherString.equals("a1")) { a1l += 0; }
if (otherString.equals("a2")) { a2l += 0; }
if (otherString.equals("a3")) { a3l += 0; }
if (otherString.equals("l")) { ll += 1; }
}
}
}
DoubletsOrTriplets doublet1 = new DoubletsOrTriplets ("ss",
ss);
DoubletsOrTriplets doublet2 = new DoubletsOrTriplets ("se1", se1);
DoubletsOrTriplets doublet3 = new DoubletsOrTriplets ("se2", se2);
DoubletsOrTriplets doublet4 = new DoubletsOrTriplets ("sa1", sa1);
DoubletsOrTriplets doublet5 = new DoubletsOrTriplets ("sa2", sa2);
DoubletsOrTriplets doublet6 = new DoubletsOrTriplets ("sa3", sa3);
DoubletsOrTriplets doublet7 = new DoubletsOrTriplets ("sl", sl);
DoubletsOrTriplets doublet8 = new DoubletsOrTriplets ("e1e1", e1e1);
DoubletsOrTriplets doublet9 = new DoubletsOrTriplets ("e1e2", e1e2);
DoubletsOrTriplets doublet10 = new DoubletsOrTriplets ("e1a1", e1a1);
DoubletsOrTriplets doublet11 = new DoubletsOrTriplets ("e1a2", e1a2);
DoubletsOrTriplets doublet12 = new DoubletsOrTriplets ("e1a3", e1a3);
DoubletsOrTriplets doublet13 = new DoubletsOrTriplets ("e1l", e1l);
DoubletsOrTriplets doublet14 = new DoubletsOrTriplets ("e2e2", e2e2);
DoubletsOrTriplets doublet15 = new DoubletsOrTriplets ("e2a1", e2a1);
DoubletsOrTriplets doublet16 = new DoubletsOrTriplets ("e2a2", e2a2);
DoubletsOrTriplets doublet17 = new DoubletsOrTriplets ("e2a3", e2a3);
DoubletsOrTriplets doublet18 = new DoubletsOrTriplets ("e2l", e2l);
DoubletsOrTriplets doublet19 = new DoubletsOrTriplets ("a1a1", a1a1);
DoubletsOrTriplets doublet20 = new DoubletsOrTriplets ("a1a2", a1a2);
DoubletsOrTriplets doublet21 = new DoubletsOrTriplets ("a1a3", a1a3);
DoubletsOrTriplets doublet22 = new DoubletsOrTriplets ("a1l", a1l);
DoubletsOrTriplets doublet23 = new DoubletsOrTriplets ("a2a2", a2a2);
DoubletsOrTriplets doublet24 = new DoubletsOrTriplets ("a2a3", a2a3);
DoubletsOrTriplets doublet25 = new DoubletsOrTriplets ("a2l", a2l);
DoubletsOrTriplets doublet26 = new DoubletsOrTriplets ("a3a3", a3a3);
DoubletsOrTriplets doublet27 = new DoubletsOrTriplets ("a3l", a3l);
DoubletsOrTriplets doublet28 = new DoubletsOrTriplets ("ll", ll);
list.add (0, doublet1);
list.add (1, doublet2);
list.add (2, doublet3);
list.add (3, doublet4);
list.add (4, doublet5);
list.add (5, doublet6);
list.add (6, doublet7);
list.add (7, doublet8);
list.add (8, doublet9);
list.add (9, doublet10);
list.add (10, doublet11);
list.add (11, doublet12);
list.add (12, doublet13);
list.add (13, doublet14);
list.add (14, doublet15);
list.add (15, doublet16);
list.add (16, doublet17);
list.add (17, doublet18);
list.add (18, doublet19);
list.add (19, doublet20);
list.add (20, doublet21);
list.add (21, doublet22);
list.add (22, doublet23);
list.add (23, doublet24);
list.add (24, doublet25);
list.add (25, doublet26);
list.add (26, doublet27);
list.add (27, doublet28);
return list;
}
class Prevalence implements Sequence {
public double getSValue() {
double prevalence = 0;
int numSusceptible = 0;
String string;
for (int i = 0; i < numNodes; i++) {
GayNode iNode = (GayNode) agentList.get(i);
string = iNode.getInfectStatus();
// System.out.printf("status = %s ", string);
if (string.equals("s")) {
numSusceptible += 1;
}
}
prevalence = ((double)(numNodes - numSusceptible)) /
(double)numNodes;
//System.out.printf("\nprevalence =%.4f \n", prevalence);
return prevalence;
}
}
class AvgNumNodes implements Sequence {
public double getSValue() {
getMeanDegree();
return meanDegree;
}
}
public void buildDisplay() {
if (layoutType.equals("KK")) { // java.lang.String is a subclass of
.Object So .equals is available
graphLayout = new KamadaGraphLayout(agentList, worldXSize,
worldYSize, surface, updateEveryN);
} else if (layoutType.equals("Fruch")) {
graphLayout = new FruchGraphLayout(agentList, worldXSize,
worldYSize, surface, updateEveryN);
} else if (layoutType.equals("CircleLayout")) {
graphLayout = new CircularGraphLayout(agentList, worldXSize,
worldYSize);
}
// these four lines hook up the graph layouts to the stop, pause, and
// exit buttons on the toolbar. When stop, pause, or exit is clicked
// the graph layouts will interrupt their layout as soon as possible.
Controller c = (Controller) getController();
c.addStopListener(graphLayout);
c.addPauseListener(graphLayout);
c.addExitListener(graphLayout);
Network2DDisplay display = new Network2DDisplay(graphLayout);
surface.addDisplayableProbeable(display, "Gay Network");
// add the display as a Zoomable. This means we can "zoom" in on
// various parts of the network.
surface.addZoomable(display);
//surface.setBackground(Color.white);
addSimEventListener(surface); //uchicago.src.sim.engine.SimModelImpl
class
}
/*
* Creates a histogram of the degree distribution.
*/
private void makeHistogram() {
degreeDist = new Histogram("Degree Distribution", maxDegree + 1, 0,
maxDegree + 1, this); // shouldn't the range be double,not int?
degreeDist.createHistogramItem("Degree Distribution", agentList,
"getOutDegree"); // Is "getOutDegree" a data bin src?
// agentList has nodes as components. The node.getOutDegree is the
getBinValue. ??
}
/*
* Creates a Plot of the Clustering Coefficient, the avg. density,
* and the component count.
*/
private void makePlot() {
graph = new NetSequenceGraph("Network Stats", this,
"./net.txt", PlotModel.CSV, agentList);
graph.setAxisTitles("Time", "Statistic Value");
graph.setXRange(0, 50);
graph.setYRange(0, numNodes);
graph.plotDensity("Density", Color.blue, NetSequenceGraph.SQUARE);
//graph.plotComponentCount("Component Count", Color.blue,
graph.SQUARE);
graph.plotClusterCoefficient("Cluster Coef.", Color.red,
NetSequenceGraph.SQUARE);
}
private void displayPrevalence() {
seqGraph = new OpenSequenceGraph (" HIV Prevalence", this,
"./prev.txt", OpenStats.CSV);
seqGraph.setAxisTitles("Time", "Prevalence");
seqGraph.setXRange(0, 50);
seqGraph.setYRange(0, 1.2);
seqGraph.addSequence("Prevalence", new Prevalence());
seqGraph.addSequence ("Mean Degree", new AvgNumNodes());
}
public void initialAction() {
randomMeeting();
graphLayout.updateLayout();
surface.updateDisplay();
valueDoubletList(doubletList);
printList (doubletList);
if (showHist) degreeDist.step();
if (showPlot) graph.step();
if (showPrev) seqGraph.step();
}
public void mainAction() {
randomMeeting();
removeRandomPartners();
infectPartners();
randomProgress();
valueDoubletList(doubletList);
printList (doubletList);
graphLayout.updateLayout();
surface.updateDisplay();
if (showHist) degreeDist.step();
if (showPlot) graph.step();
if (showPrev) seqGraph.step();
//System.out.println(NetUtilities.getComponents(agentList).size());
}
/**
* what if we use only main action, in other words, what if
randomMeeting and removeRandomPartners
* are exercuted simulatneously from the simulation start?
*/
public void removeInitialAction() {
schedule.removeAction(initialAction);
// uchicago.src.sim.engine.ScheduleBase is the superclass of schedule.
// .revomeAction is one of the methods.
}
private void buildSchedule() {
initialAction = schedule.scheduleActionAt(1, this, "initialAction");
schedule.scheduleActionAt(initialSteps, this, "removeInitialAction",
Schedule.LAST);
// initialAction and removeInitialAction occurs at the same tick cout,
// but removeInitialAction occurs after initial action
(Schedule.LAST).
schedule.scheduleActionBeginning(initialSteps + 1, this,
"mainAction");
schedule.scheduleActionAtEnd(seqGraph, "writeToFile"); // interface
Recorder.writeToFile()
}
public void begin() {
buildModel();
buildDisplay();
buildSchedule();
graphLayout.updateLayout();
surface.display();
if (showHist) degreeDist.display();
if (showPlot) graph.display();
if (showPrev) seqGraph.display();
}
public void setup() {
Random.createUniform();
if (surface != null) surface.dispose();
if (degreeDist != null) degreeDist.dispose();
if (graph != null) graph.dispose();
if (seqGraph != null) seqGraph.dispose();
surface = null;
schedule = null;
degreeDist = null;
graph = null;
seqGraph = null;
System.gc(); // runs the garbage collector
surface = new DisplaySurface(this, "Network Display");
registerDisplaySurface("Main Display", surface);
// Registers a DisplaySurface with this model (surface)
// and associates it with a particular name (Main Display).
schedule = new Schedule();
agentList = new ArrayList();
worldXSize = 500;
worldYSize = 500;
}
public String[] getInitParam() {
String[] params = {"numNodes", "worldXSize", "worldYSize",
"updateEveryN", "LayoutType", "RemoveProb", "PartnershipProb",
"StartRemoveAfter", "MaxDegree", "DegreeHist", "Plot",
"PrevGraph",
"initInfNodes", "probFromE1ToE2",
"probFromA1ToA2","probFromLToS",
"transProbAtE","transProbAtA",
"transProbAtL"};
return params;
}
// parametera are just dispalyed in alphabetical order.
public Schedule getSchedule() {
return schedule;
}
public String getName() {
return "Parameter";
}
public static void main(String[] args) {
uchicago.src.sim.engine.SimInit init =
new uchicago.src.sim.engine.SimInit();
GayModel model = new GayModel();
init.loadModel(model, "", false);
}
}
--------------------------------------------------------------------------------------------------------------------
Hendrik Maryns - 05 Jan 2006 23:31 GMT
Jonie uitte de volgende tekst op 5/01/2006 20:46:
> Hi all, for the last few days, I succeeded in constructing a code that
> represents a network and infection transmisson through it. This is the
> first time ever that i wrote a code. So i am wondering how i validate
> my code although it may sound a philosophical question. And also, I
> want someone who are good at java programming to review my code. I
> would really appreciate it if anyone would review my code ??
Well, I can give some small comments, but not very much.
> it has 4 classes.
>
> /**
> * DoubletsOrTriplets.java
> */
Do you know the difference between a Javadoc comment and an ordinary
comment? This should not be a Javadoc comment.
> import java.util.ArrayList;
>
> public class DoubletsOrTriplets {
>
> String string;
Use more descriptive names.
> int value;
>
> /**
> Constructors
> */
Use Javadoc comments here, and give some more information.
*Comment your code!!*
Do you really need all these constructors?
> public DoubletsOrTriplets () {
>
> string = "";
> value = 0;
Not really necessary, as Java does this by itself, but good for clarity.
> }
>
[quoted text clipped - 18 lines]
> value = x;
> }
Use two setters, one for each attribute. What if someone wants to
change only one value?
How about some getters?
> public void printSelf () {
> System.out.printf ("%s = %d\n", string, value);
>
> }
Better create a method toString(), then you can just do
System.out.print(myDoubletOrTriplet) in some other class.
> ----------------------------------------------------------------------------------------------
> /**
> * GayEdge.java
> *
> */
Javadoc comment
> import java.awt.Color;
>
[quoted text clipped - 20 lines]
> color = c;
> }
Getter?
> public void draw(SimGraphics g, int fromX, int toX, int fromY, int
> toY) {
[quoted text clipped - 68 lines]
> makeEdgeToFrom(node, Color.white);
> }
You might consider using Java 5 and generics here.
> public void removePartners() {
> GayNode jNode = (GayNode)getRandomNodeOut();
[quoted text clipped - 7 lines]
> }
> }
What does this do? Documentation?
> }
>
[quoted text clipped - 73 lines]
> public GayModel() {
> Vector vect = new Vector();
Do not use Vector if not necessary, use ArrayList instead.
> vect.add("Fruch");
> vect.add("KK");
[quoted text clipped - 155 lines]
> }
> }
The standard toString method of array list does almost exactly this.
Are you sure you don’t want a newline or any other separator?
<snip all the rest>: too technical to understand. Your indenting could
be a bit better.
2c
Cheers, H.

Signature
Hendrik Maryns
==================
www.lieverleven.be
http://aouw.org
Ricky Clarkson - 06 Jan 2006 13:22 GMT
> Do not use Vector if not necessary, use ArrayList instead.
Or rather, make an informed decision:
http://www.javaworld.com/javaworld/javaqa/2001-06/03-qa-0622-vector.html
Jonie - 06 Jan 2006 15:06 GMT
Thank you very much, Hendrik.
What you asked is for removing one edge (breaking a contact between two
individuals) from a node (an individual).
I get all of your points except the following.
"
Better create a method toString(), then you can just do
System.out.print(myDoubletOrTriplet) in some other class
"
Would you be so kind as to explain the above once again?
Hendrik Maryns - 07 Jan 2006 18:26 GMT
Jonie uitte de volgende tekst op 6/01/2006 16:06:
> Thank you very much, Hendrik.
> What you asked is for removing one edge (breaking a contact between two
[quoted text clipped - 8 lines]
>
> Would you be so kind as to explain the above once again?
How about you read it yourself in the API?
http://java.sun.com/j2se/1.3/docs/api/java/lang/Object.html#toString()
You can print any object, it will invoke the object's toString() method.
By default this just prints some memory address, so if you want
something more interesting, you have to override toString(). Look into
the source of PrintStream.print() if you want to know the details about
how this works.
H.
Jonie - 06 Jan 2006 21:29 GMT
Thank you for your comments, Hendrik. I get all of your points except
the following.
Better create a method toString(), then you can just do
System.out.print(myDoubletOrTriplet) in some other class.
Would you be so nice as to explain that once again?
And the code that you asked is for removing the edge (link between two
node) from a node (an individual) if the individual have neighbors.