Base model
/*
* CustomNode.java
*
* Created on January 22, 2005, 2:41 PM
* Modified on June 14, 2006
*
* This class contains the parameters and methods
* for an agent acting on the network
*/
package CoopNetBlue;
import uchicago.src.sim.gui.NetworkDrawable;
import uchicago.src.sim.network.DefaultDrawableNode;
import uchicago.src.sim.gui.DrawableNonGridNode;
import uchicago.src.sim.util.Random;
import uchicago.src.sim.gui.OvalNetworkItem;
import java.awt.Color;
import uchicago.src.sim.gui.ColorMap;
import uchicago.src.sim.engine.CustomProbeable;
import java.util.ArrayList;
/**
*
* @author Jack Waddell
*
*/
// DefaultDrawableNode lets us draw it in the GUI
// CustomProbeable lets us define which attributes appear when
// probed in the GUI
public class CustomNode extends DefaultDrawableNode implements CustomProbeable {
//***********************************
// Static Parameters
// tracks next unique id
public static int nextID = 0;
public static GUIModel guiModel = null;
public static CoopNetBlue model;
//********************************8**
// Instance parameters
// Two colormaps are generated for color-coding the nodes in GUI
public static ColorMap centerColorMap;
public static ColorMap edgeColorMap;
public static final int colorMapSize = 16;
public static final int colorMapMax = colorMapSize - 1;
private int id; // uniquely ids node
public Color myColor; // used to draw color in GUI
public String myPajekColor = "Red"; // color to appear in Pajek output
public int utility; // current wealth or stored utility
public double strategy; // strategy parameter
///////////////////////////////////////////////////////////////
// constructor
// Inputs: NetworkDrawable drawable, to draw in GUI
// double strategy, the strategy parameter
public CustomNode (NetworkDrawable drawable, double strategy) {
super(drawable);
id = nextID++; // set id and iterate to next
this.strategy = strategy;
utility = 50; // Generalize. Currently hard-coded
if (guiModel != null)
setNodeEdgeColorFromStrategy();
}
///////////////////////////////////////////////////////////
// getProbedProperties
// Required to implement CustomProbeable
// Inputs: none
// Outputs: array of strings holding parameter names
public String[] getProbedProperties(){
return new String[] {"id", "utility", "strategy"};
}
//////////////////////////////////////////////////////////
// step
// Input: none
// Output: none
// (Can be) Called by main model to have nodes execute a single step
public void step(){
}
//////////////////////////////////////////////////////////
// setNodeColorFromUtility
// Input: maxUtility, the highest utility from all agents
// Output: none
// Sets the node color to the colormap leval depending on utility
public void setNodeColorFromUtility(int maxUtility){
int i = (int) Math.round(colorMapMax*((double)utility)/ ((double) maxUtility));
this.setColor(centerColorMap.getColor(i));
/*if (rDebug > 1){
double rat = ((double) getUtility())/((double) maxUtility);
int ival = (int) Math.round(rat*colorMapSize);
System.out.printf("%d/%d = %f -> %d\n", getUtility(), maxUtility, rat, ival);
}*/
}
//////////////////////////////////////////////////////////
// setNodeEdgeColorFromStrategy
// Input: none
// Output: none
// Sets the node edge color to the colormap level depending on strategy
public void setNodeEdgeColorFromStrategy(){
int i = (int) Math.round(colorMapMax*strategy);
this.setBorderColor(edgeColorMap.getColor(i));
this.setBorderWidth(3);
/*if (rDebug > 1){
double rat = ((double) getUtility())/((double) maxUtility);
int ival = (int) Math.round(rat*colorMapSize);
System.out.printf("%d/%d = %f -> %d\n", getUtility(), maxUtility, rat, ival);
}*/
}
////////////////////////////////////////////////////////////
// bearChild
// Input: none
// Output: node
// Creates a child node
public void bearChild(){
double deviation = model.getNormalDouble(0, 0.1); // mutate child
double childStrategy = strategy + deviation;
childStrategy = Math.min(1, Math.max(0, childStrategy));
OvalNetworkItem drawable = new OvalNetworkItem(0,0);
CustomNode child = new CustomNode(drawable, childStrategy);
ArrayList<CustomNode> parentNeighbors = this.getOutNodes();
for (CustomNode neighbor : parentNeighbors){
CustomEdge edge1 = new CustomEdge(child, neighbor);
CustomEdge edge2 = new CustomEdge(neighbor, child);
// I think you have to add edges in and out to make it
// undirected.
child.addOutEdge(edge1);
neighbor.addInEdge(edge1);
child.addInEdge(edge2);
neighbor.addOutEdge(edge2);
}
CustomEdge edge1 = new CustomEdge(this, child);
CustomEdge edge2 = new CustomEdge(child, this);
child.addOutEdge(edge2);
this.addInEdge(edge2);
child.addInEdge(edge1);
this.addOutEdge(edge1);
if (guiModel!=null)
guiModel.addAgent(child);
else
model.addAgent(child);
}
//////////////////////////////////////////////////////
// die
// Inputs: none
// Outputs: none
// Kills an agent
public void die(){
// Go through neighbors, removing links to or from node
// Make sure you get them all!
ArrayList<CustomNode> neighbors = this.getOutNodes();
for (CustomNode neigh : neighbors){
neigh.removeEdgesFrom(this);
neigh.removeEdgesTo(this);
}
if (guiModel != null)
guiModel.delAgent(this);
else
model.delAgent(this);
}
///////////////////////////////////////////////////////
// getDegree
// Input: none
// Output: int degree
// Calculates the degree of the node (assuming symmetric)
public int getDegree(){
return (this.getOutNodes()).size();
}
//***************************************************************
// Getters and Setters
public int getID() {return id;}
public void setMyPajekColor(String i) {myPajekColor = i;}
public String getMyPajekColor() {return myPajekColor;}
public int getUtility() {return utility;}
public void setUtility(int i) {utility = i;}
public int incUtility() {return ++utility;}
public int decUtility() {return --utility;}
public double getStrategy() {return strategy;}
public void setStrategy(double i) {strategy = i;}
//***************************************************************
// Static Methods
public static void setModel(CoopNetBlue m) {model = m;}
//////////////////////////////////////////////////////////
// setUpNodeDrawing
// Input: GUIModel m, a pointer to the GUIModel
// Output: none
public static void setUpNodeDrawing (GUIModel m) {
guiModel = m;
centerColorMap = new ColorMap();
edgeColorMap = new ColorMap();
double minColor = 0.1;
for (int i = 0; i < colorMapSize; i++){
double fracColor = minColor + ( (1.0-minColor)*i/colorMapMax );
centerColorMap.mapColor(i, 0, 0, fracColor);
edgeColorMap.mapColor(i, 0, fracColor, 0);
}
}
///////////////////////////////////////////////////////////
// resetNextID
// Inputs: none
// Outputs: none
// Resets nextID. Called when the model is reset, and a
// new set of nodes is generated
public static void resetNextID(){
nextID = 0;
}
}