GameMaster.java
From Santa Fe Institute Events Wiki
/*
* GameMaster.java * * Created on June 13 2006 * Last Modified June 14 2006 * * This class oversees games played between agents */
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 * * */
// GameMaster Constructor public class GameMaster {
// GUIModel: Stores the model class that runs the graphics public static GUIModel guiModel = null;
// model: stores the base model public static CoopNetBlue model;
// agentList: Stores a pointer to the agents list public ArrayList<CustomNode> agentList; public GameMaster (ArrayList agentList) {
// If the agent list pointer is sent from CoopNetBlue // then changing the list here changes it there as well. this.agentList = agentList;
}
//////////////////////////////////////////////////////////// // playPair // Inputs: nodei, nodej // Output: none // Plays a game between two nodes nodei, nodej (symmetric) public void playPair(CustomNode nodei, CustomNode nodej){
if (nodei.getUtility() > 0 & nodej.getUtility() > 0 ){
// Dumb game. Flip a coin. double randn = model.getUniformDoubleFromTo(0,1); if (randn < 0.5){ nodei.incUtility(); nodej.decUtility(); } else{ nodej.incUtility(); nodei.decUtility(); } }
}
///////////////////////////////////////////////////////////// // playAll // Inputs: none // Output: none // Traverses the agent list, playing a game once between // all pairs of neighbors on the network. public void playAll(){
//for (CustomNode nodej : agentList){ CustomNode nodei, nodej;
// Traverse the agent list for (int i = 0; i < agentList.size(); i++){ nodei = agentList.get(i);
// Get current node's neighbors: symmetric network // means that out neighbors are just neighbors. ArrayList<CustomNode> neighbors = nodei.getOutNodes(); for (int j = 0; j < neighbors.size(); j++){ nodej = neighbors.get(j);
// To ensure game played once between each pair // only play in one direction. if (nodej.getID() < nodei.getID()) playPair(nodej, nodei); } }
}
////////////////////////////////////////////////////////////// // evolveAgents // Inputs: none // Output: none public void evolveAgents(){
// Need this to draw the node in the graphics mode. OvalNetworkItem drawable = new OvalNetworkItem(0,0);
int i=0,j=1; // These will be two random values, unequal CustomNode nodei, nodej; int numAgents = agentList.size(); do{ i = model.getUniformIntFromTo(0, numAgents-1); j = model.getUniformIntFromTo(0, numAgents-1);
nodei = agentList.get(i); nodej = agentList.get(j);
} while (nodei.getUtility() == nodej.getUtility()); // This makes sure that there are no ties, and also that // i and j are different.
CustomNode winner, loser;
// Find which wins if (nodei.getUtility() > nodej.getUtility()){ winner = nodei; loser = nodej; } else{ loser = nodei; winner = nodej; } winner.bearChild(); loser.die();
}
//*********************************************************** // Getters and Setters
public static void setModel(CoopNetBlue m) {model = m;} public static void setGUIModel(GUIModel m) {guiModel = m;}
}