Sample7_SocialInteractionSimulations.java
Created with JBuilder |
import mads.core.*;
import mads.networks.*;
import mads.tools.init.*;
import mads.strategies.*;
/**
* <p><H1>Social Interaction Simulations</H1></p>
*
* <p><B>Sample project no. 7 </B><BR> </p>
* Social interaction simulations are the type of simulations where a group of actors
* (representing individuals, firms, atoms, molecules and so on) are positioned in the
* nodes of a network and interact with their direct neighbors. We will call these
* actors <I>agents</I>. <BR>
* In the cogeneration simulation that was previously presented, there was just one
* such agent which decided to adopt the cogeneration technology if and when the profit
* was maximum. The simulation was actually modeling the decision process for this one
* agent. Because in the social interaction simulations we have many agents, all having
* to take decisions, we use a different approach. The whole decision process will be
* put inside a special object called a <I>strategy</I> object. From time to time, each
* agent will use this to make a decision, which will be represented as a <I>state</I>.
* For example, in the cogeneration example, state = 0 could mean he chose a boiler
* and state = 1 that he chose cogeneration. After the simulation, the user can see how
* these states have evolved through time, how many agents are in one state or another
* and so on.<BR>
* *
* The user should notice the following things:<BR\>
* <UL>
* <LI> Network are expressed as Service Network Functions
* <LI> Service Network Functions can be obtained using Network Generators
* </UL>
*
* In this tutorial, an Ising Model will be implemented. In this model
* agents (in this case atoms) interact over a network (a grid). The states
* of the atoms (spin-up and spin-down) are represented by -1 and 1. A strategy
* is used to choose one of the two states.
*/
public class Sample7_SocialInteractionSimulations extends ASimulationAgent
{
public void doWork(){
// Declare initial conditions (constants)
final double Temperature = 100;
final int N = 10;
// Create a grid network topology, like in the previous tutorial
ServiceNetworkFunction snfGrid = (new TwoDimLatticeGenerator(N, false)).generateNetwork();
/**
* Every simulation has to start from a well defined initial state. In MADS,
* the initial state is represented as a vector, where the i-th element represents
* the initial state of i-th node from the network. Hence, the size of the
* initial state vector must match the number of nodes in the network.
* There are two ways to generate such vector. The first it to declare one
* and to fill it with value "by hand" (e.g. inside a for loop). This is
* the most flexible approach. If which node has which values is not important,
* but just what percent of each state is present (which is true for most
* simulations), there is another way to automatically generate a random
* vector with the given statistical properties. It is this later approach
* that we are going to use.
*/
// Generate an initial state vector with 1s and -1s in 50/50 proportion
// For this, one must include the "mads.tools.init" package.
int[] initialState = RandomInitialState.generateArray(N*N, new int[]{-1, 1}, new int[]{50,50});
/**
* We will now declare the interaction strategy according to the Ising model.
* This strategy will be implemented as a class inheriting the "Strategy" class.
* The user must implement the "decide" function, and can also use other
* additional ones, if necessary.
* A strategy for the Ising model is already present in MADS, in the
* package "mads.strategies", which has to be included as well
*/
//Declare the strategy
Strategy isingStrategy = new Ising(Temperature);
/**
* We we now run the simulation. The chosen time will be from 0 to 1000.
* The result of the simulation will be stored in a special class called
* SocialInteractionSimulationResult
*/
// Declare the simulation
// One must provide the network, the strategy and initial conditions
SocialInteractionSimulation isingSimulation = new SocialInteractionSimulation(snfGrid, isingStrategy, initialState, null);
// Run the simulation
SocialInteractionSimulationResult result = isingSimulation.runRandomized(0, 1000, 1);
/**
* Now, the simulation has ended and the results are stored in the "result"
* variable. We can now extract information regarding state evolution
* from here, in the form of service functions. We will the plot the result.
*/
// Get the evolution of the -1 state (spin-down)
ServiceFunction sfSpinDown = result.getStateEvolution(-1);
//Plot
sfSpinDown.plot("Percent (%)", "Spin-down evolution");
}
}
Sample7_SocialInteractionSimulations.java
Created with JBuilder |