utilities
Class Individual

java.lang.Object
  |
  +--utilities.Individual

public class Individual
extends java.lang.Object

A class that implements individuals for agent based modeling. This is compatible with any form of game and either reinforcement or bayesian learning. Very little processing is done in this class. Only some basic sanity checks for assignments. In addition, I have removed functionality for keeping track of history of play beyond one round. For learning rules which require longer memory of actions, one should implement another class. (Keeping track of past play was very taxing on resources, and so was removed.)

Author:
Kevin Zollman

Constructor Summary
Individual()
          Returns and individual object.
Individual(int i)
          Returns and individual object like the simple constructor, but also sets an id.
Individual(int i, int s, MersenneTwister r, Individual[] nw, ConnectionRule c, LearningRule l)
          Returns and individual object, but with more initialized.
 
Method Summary
 void addAlpha(double a, int d)
          Adds a value to the alpha function, used in updating
 void addBeta(double a, int d)
          Adds a value to the alpha function, used in updating
 void addConnected(Individual i)
          Adds a neighbor to an individuals current neighbors.
 void addPayoff(int p)
          Adds a value to the current payoff
 void addStratWeight(int p, double w)
          Adds a specified weight to a particular strategy.
 void addStratWeight(int d, int p, double w)
          Adds a specified weight to a particular strategy.
 int chooseStrat()
          Uses the learning rule to choose a strategy
 void clearConnected()
          Clears the list of neighbors.
 void clearDomainWeights(int d)
          A function that will clear the weights in only one domain, but leave the others intact
 void clearStratWeights()
          A function to reset all strategy weights to 1.0.
 void clearStratWeights(int n)
          A function to reset all strategy weights to 1.0.
 void clearStratWeights(int n, int d)
          A function to reset all stategy weights to 1.0.
 void formConnections()
          Uses the connection rule to update the neighborhood.
 double[] getAlpha()
          Gets the alpha value
 double getAlpha(int d)
          Gets the alpha value for a particular domain
 double getBelief(int b)
          Returns an agent's beliefs about a particular state in a model where the agent is only learning about one thing.
 double getBelief(int d, int b)
          Returns an agent's belief about a particular state in a model where there are many things about which the agent may be learning.
 double[][] getBeliefs()
          Returns an agent's beliefs.
 double[] getBeta()
          Gets the beta value
 double getBeta(int d)
          Gets the beta value for a particular domain
 java.util.HashSet getConnected()
          Returns the individuals neighbors
 ConnectionRule getConnectionRule()
          Returns the connection rule
 double getConstraint()
          Returns the constraints on generating new beliefs in a one domain model
 double getConstraint(int d)
          Returns the constraints on generating new beliefs for a particular domain.
 double getDiscount()
          Returns the discount factor
 double[] getDomainWeights(int d)
          Returns the full set of weights for a particular domain
 Game getGame()
          Returns the game
 int getId()
          Returns an the individuals id number
 int getLastPayoff()
          Returns the payoff from the last round
 int getLastStrat()
          This returns the strategy used on the last round
 LearningRule getLearningRule()
          Returns the learning rule
 int getMaxalpha()
          Returns the value of maxalpha used to contrain the reseting of alpha.
 int getMaxbeta()
          Returns the value of maxbeta.
 int getMaxWeight()
          Gets the maxWeight
 double getMutation()
          Calls the mutationRules get function.
 MutationRule getMutationRule()
           
 Individual[] getNetwork()
          Returns the network (all other individuals in the model).
 int getPayoff()
          Returns the payoff from the last round
 MersenneTwister getRandom()
          Returns the random number generator
 int getStrat()
          Returns the current strategy, if its not set chooses one.
 double[] getWeights()
          Returns the current weights.
 double getWeights(int p)
          Returns the weights for a current strategy
 double getWeights(int d, int p)
          Returns the weights for a current strategy
 void initAB(int d)
          Initializes the alpha's without setting them.
 void initDomains(int d)
          Initializes the beliefs for a multi-domain model.
 void initStart(int s)
          The initializes the individual for single domain models by giving them random beliefs and randomizing the weights The random beliefs are constrained by the zeroconstraint.
 void initStart(int d, int s)
          The initializes the individual for multi domain models by giving them random beliefs and randomizes the the wieghts The random beliefs are constrained by the zeroconstraint.
 void mutate()
          Calls the mutationRules mutate function.
 void playGame()
          Plays the game with each neighbor.
 void randBeliefs(int states)
          Randomizes an agents beliefs, this requires that the random be set.
 void randBeliefs(int states, boolean constrained)
          Randomizes an agents beliefs, this requires that the random be set.
 void randBeliefs(int states, int d)
          Randomizes an agents beliefs, this requires that the random be set.
 void randBeliefs(int states, int d, boolean constrained)
          Randomizes an agents beliefs, this requires that the random be set.
 void randomAB()
          Randomizes alpha and beta.
 void randomAB(int d)
          Randomizes alpha and beta.
 void randomAB(int a, int b, int d)
          Randomizes alpha and beta.
 void randStratWeights(int n)
          Randomizes the weights using the set maximum.
 void randStratWeightsD(int n, int d)
          Randomizes the weights using the set maximum.
 void reset()
          Resets for a new generation.
 void setAlpha(double[] a)
          Sets alpha at a specific value
 void setAlpha(double a, int d)
          Sets alpha at a specific value
 void setBeliefs(double[] b)
          A function to set the Bayesian beliefs for one domain.
 void setBeliefs(double[][] b)
          A function to set the Bayesian beliefs for multi-domain situations.
 void setBeliefs(double[] b, int d)
          A function to set the Bayesian beliefs for one domain in mutli-domain learning situations.
 void setBeta(double[] b)
          Sets beta at a specific value
 void setBeta(double b, int d)
          Sets beta at a specific value
 void setConnected(java.util.HashSet c)
          Sets the neighbors to a particular HashSet of individuals
 void setConnectionRule(ConnectionRule c)
          Sets the connection rule, which determines how the individual updates its neighborhood
 void setConstraint(double c)
          Sets a constraint that initializations can be constrained by.
 void setConstraint(double[] c)
          Sets a constraint that initializations can be constrained by.
 void setConstraint(int d, double c)
          Sets a constraint that initializations can be constrained by.
 void setDiscount(double d)
          Sets a discount value
 void setGame(Game g)
          Sets the game
 void setId(int i)
          Sets the id number.
 void setLastPayoff()
          Sets the last round payoff equal to the current payoff
 void setLastStrat()
          This updates the strategy for the next round by setting the last round strat equal to the current strat.
 void setLearningRule(LearningRule l)
          Sets the learning rule for updating strategies
 void setMaxalpha(int maxalpha)
          Sets the value of maxalpha.
 void setMaxbeta(int maxbeta)
          Sets the value of maxbeta.
 void setMaxWeight(int m)
          Sets the maxWeight
 void setMutation(double m)
          Calls the mutationRule set function.
 void setMutationRule(MutationRule m)
           
 void setNetwork(Individual[] nw)
          Sets the network (all other individuals in the model).
 void setPayoff(int p)
          Sets the payoff from the last generation
 void setRandom(MersenneTwister r)
          Sets the random number generator
 void setStrat(int s)
          Sets the current strategy of a user
 void setStratWeights(double[] s)
          Sets the strategy weights to a specific value.
 void setStratWeights(double[][] s)
          Sets the strategy weights to a specific value.
 void setStratWeights(int p, double s)
          Sets the strategy weight for a particular strategy to a specific value.
 void setStratWeights(int d, int p, double s)
          Sets the strategy weight for a particular strategy to a specific value.
 void uniformBeliefs(int states)
          Generates uniform beliefs over a number of states.
 void uniformBeliefs(int states, int d)
          Generates uniform beliefs over a number of states.
 void update()
          Updates after play is complete.
 
Methods inherited from class java.lang.Object
equals, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait
 

Constructor Detail

Individual

public Individual()
Returns and individual object. Sets up an empty list for neighbors, the strategy to -1, the payoff to zero, the discount factor to 1.0 (no discounting), the zero constraint to 0.0, maxalpha and maxbeta to 4, and maxWeights to 1.


Individual

public Individual(int i)
Returns and individual object like the simple constructor, but also sets an id.

Parameters:
i - The id

Individual

public Individual(int i,
                  int s,
                  MersenneTwister r,
                  Individual[] nw,
                  ConnectionRule c,
                  LearningRule l)
Returns and individual object, but with more initialized.

Parameters:
i - The id
s - The number of strategies
r - The random number generator
nw - The network
c - The connection rule
l - The learning rule
Method Detail

setId

public void setId(int i)
Sets the id number.

Parameters:
i - the id number

getId

public int getId()
Returns an the individuals id number

Returns:
the id number

setBeliefs

public void setBeliefs(double[] b)
                throws ModelError
A function to set the Bayesian beliefs for one domain. This should be used for situations where the agent is only inferring about one state of the world.

Parameters:
b - A 1-D double array containing beliefs.
Throws:
ModelError - if there is something wrong with the beliefs

setBeliefs

public void setBeliefs(double[][] b)
                throws ModelError
A function to set the Bayesian beliefs for multi-domain situations. This should be used for cases where the agent is trying to learn several different things at once

Parameters:
b - A 2-D double array containing beliefs.
Throws:
ModelError - if there is something wrong with the beliefs

setBeliefs

public void setBeliefs(double[] b,
                       int d)
                throws ModelError
A function to set the Bayesian beliefs for one domain in mutli-domain learning situations. This should be used for cases where the agent is trying to learn several different things at once

Parameters:
b - A 2-D double array containing beliefs.
d - The domain to set
Throws:
ModelError - if there is something wrong with the beliefs

getBeliefs

public double[][] getBeliefs()
Returns an agent's beliefs. Generally this shouldn't be used, prefer the specific ones.

Returns:
The agent's beliefs

getBelief

public double getBelief(int b)
Returns an agent's beliefs about a particular state in a model where the agent is only learning about one thing.

Parameters:
b - the state we're interested in
Returns:
the agent's belief in that state

getBelief

public double getBelief(int d,
                        int b)
Returns an agent's belief about a particular state in a model where there are many things about which the agent may be learning.

Parameters:
d - the domain that the agent is learning about
b - the thing about which where interested
Returns:
the agent's belief about that state in that domain

setConstraint

public void setConstraint(double c)
                   throws ModelError
Sets a constraint that initializations can be constrained by. The constraint is a minimum for beliefs in state zero. This should only be used for single domain learning. Currently only zero state constraints are supported.

Parameters:
c - A double representing the constraint
Throws:
ModelError - if the constraint is outside of [0,1]

setConstraint

public void setConstraint(double[] c)
                   throws ModelError
Sets a constraint that initializations can be constrained by. The constraint is a minimum for beliefs in state zero. This function is for multiple domain models. Currently only zero state constraints are supported.

Parameters:
c - A double array representing the constraints
Throws:
ModelError - if the constraint is outside of [0,1]

setConstraint

public void setConstraint(int d,
                          double c)
                   throws ModelError
Sets a constraint that initializations can be constrained by. The constraint is a minimum for beliefs in state zero. This function is for multiple domain models. Currently only zero state constraints are supported. This function should only be used for reseting constraints once already set.

Parameters:
d - the domain in which the constraint applies
c - A double representing the constraint
Throws:
ModelError - if the constraint is outside of [0,1]

getConstraint

public double getConstraint()
Returns the constraints on generating new beliefs in a one domain model

Returns:
the constraints

getConstraint

public double getConstraint(int d)
Returns the constraints on generating new beliefs for a particular domain.

Parameters:
d - the domain for the constraint
Returns:
the constraints

initDomains

public void initDomains(int d)
Initializes the beliefs for a multi-domain model. This muse be called prior to accessing beliefs in a particular domain.

Parameters:
d - the number of domains in the model

randBeliefs

public void randBeliefs(int states,
                        boolean constrained)
Randomizes an agents beliefs, this requires that the random be set. The randomized beliefs will be on the interior of [0,1]. It is generated by the logrithm method. This is for one domain models.

Parameters:
states - the number of states over which to generate beliefs
constrained - a boolean to decide if we'll pay attention to the constraint

randBeliefs

public void randBeliefs(int states,
                        int d,
                        boolean constrained)
Randomizes an agents beliefs, this requires that the random be set. The randomized beliefs will be on the interior of [0,1]. It is generated by the logrithm method. This function only initializes for a single domain, and it requires that initDomain() be called first.

Parameters:
states - the number of states over which to generate beliefs
d - which domain should be randomly assigned
constrained - a boolean to decide if we'll pay attention to the constraint

randBeliefs

public void randBeliefs(int states)
Randomizes an agents beliefs, this requires that the random be set. The randomized beliefs will be on the interior of [0,1]. It is generated by the logrithm method. This function pays attention to the constraint by default.

Parameters:
states - the number of states over which to generate beliefs

randBeliefs

public void randBeliefs(int states,
                        int d)
Randomizes an agents beliefs, this requires that the random be set. The randomized beliefs will be on the interior of [0,1]. It is generated by the logrithm method. This function pays attention to the constraint by default.

Parameters:
states - the number of states over which to generate beliefs
d - the domain in which the beliefs should be generated

uniformBeliefs

public void uniformBeliefs(int states)
Generates uniform beliefs over a number of states. Used for one domain models this function ignores the zero constraint, of course

Parameters:
states - the number of states

uniformBeliefs

public void uniformBeliefs(int states,
                           int d)
Generates uniform beliefs over a number of states. Used for multi domain models Init domains must be called first. This function ignores the zero constraint, of course

Parameters:
states - the number of states
d - the domain in which to create uniform beliefs

setMaxWeight

public void setMaxWeight(int m)
Sets the maxWeight

Parameters:
m - The max weight

getMaxWeight

public int getMaxWeight()
Gets the maxWeight

Returns:
The max weight

clearStratWeights

public void clearStratWeights()
A function to reset all strategy weights to 1.0. This function presumes that strat wieghts have already once been set, otherwise an error is thrown.


clearDomainWeights

public void clearDomainWeights(int d)
A function that will clear the weights in only one domain, but leave the others intact

Parameters:
d - The domain to clear

clearStratWeights

public void clearStratWeights(int n)
A function to reset all strategy weights to 1.0. This function does not presume that strategy weights are already set. This is for only a single domain.

Parameters:
n - the number of states which require weights

clearStratWeights

public void clearStratWeights(int n,
                              int d)
A function to reset all stategy weights to 1.0. This function does not presume that strategy weights have already been set. This for multi-domain weights

Parameters:
n - the number of strategies that require weights
d - the number of domains

randStratWeights

public void randStratWeights(int n)
Randomizes the weights using the set maximum. This function presumes that random has been set. This is for single domain models.

Parameters:
n - The number of strategies

randStratWeightsD

public void randStratWeightsD(int n,
                              int d)
Randomizes the weights using the set maximum. This function presumes that random has been set. This is for single domain models.

Parameters:
n - The number of strategies

setStratWeights

public void setStratWeights(double[] s)
                     throws ModelError
Sets the strategy weights to a specific value. This function will return an error if any of the weights are negative. 0 weights are allowed, although keep in mind this means that that strategy will never be selected.

Parameters:
s - the weights
Throws:
ModelError - if any weight is negative

setStratWeights

public void setStratWeights(double[][] s)
                     throws ModelError
Sets the strategy weights to a specific value. This function will return an error if any of the weights are negative. 0 weights are allowed, although keep in mind this means that that strategy will never be selected.

Parameters:
s - the weights
Throws:
ModelError - if any weight is negative

setStratWeights

public void setStratWeights(int p,
                            double s)
                     throws ModelError
Sets the strategy weight for a particular strategy to a specific value. This function will return an error if any of the weights are negative. 0 weights are allowed, although keep in mind this means that that strategy will never be selected.

Parameters:
p - the strategy whose weight we will change
s - the new weight
Throws:
ModelError - if the weight is less than 0

setStratWeights

public void setStratWeights(int d,
                            int p,
                            double s)
                     throws ModelError
Sets the strategy weight for a particular strategy to a specific value. This function will return an error if any of the weights are negative. 0 weights are allowed, although keep in mind this means that that strategy will never be selected.

Parameters:
d - the domain
p - the strategy whose weight we will change
s - the new weight
Throws:
ModelError - if the weight is less than 0

addStratWeight

public void addStratWeight(int p,
                           double w)
                    throws ModelError
Adds a specified weight to a particular strategy. This function will return an error if the weight is negative.

Parameters:
p - the strategy to update
w - the amount to add
Throws:
ModelError - if the number to add is negative

addStratWeight

public void addStratWeight(int d,
                           int p,
                           double w)
                    throws ModelError
Adds a specified weight to a particular strategy. This function will return an error if the weight is negative.

Parameters:
d - the domain
p - the strategy to update
w - the amount to add
Throws:
ModelError - if the number to add is negative

getWeights

public double[] getWeights()
Returns the current weights.

Returns:
a double array which are the agents weights

getWeights

public double getWeights(int p)
Returns the weights for a current strategy

Parameters:
p - the strategy for which we want weights
Returns:
the agent's weight for that strategy

getWeights

public double getWeights(int d,
                         int p)
Returns the weights for a current strategy

Parameters:
d - the domain weights.
p - the strategy for which we want weights
Returns:
the agent's weight for that strategy

getDomainWeights

public double[] getDomainWeights(int d)
Returns the full set of weights for a particular domain

Parameters:
d - the domain

setDiscount

public void setDiscount(double d)
                 throws ModelError
Sets a discount value

Parameters:
d - the discount factor
Throws:
ModelError - if the discount factor is outside [0,1]

getDiscount

public double getDiscount()
Returns the discount factor

Returns:
the discount factor

getMaxalpha

public int getMaxalpha()
Returns the value of maxalpha used to contrain the reseting of alpha.


setMaxalpha

public void setMaxalpha(int maxalpha)
Sets the value of maxalpha.

Parameters:
maxalpha - The value to assign maxalpha.

getMaxbeta

public int getMaxbeta()
Returns the value of maxbeta.


setMaxbeta

public void setMaxbeta(int maxbeta)
Sets the value of maxbeta.

Parameters:
maxbeta - The value to assign maxbeta.

getAlpha

public double[] getAlpha()
Gets the alpha value

Returns:
alpha

getBeta

public double[] getBeta()
Gets the beta value

Returns:
beta

getAlpha

public double getAlpha(int d)
Gets the alpha value for a particular domain

Parameters:
d - the domain
Returns:
alpha

getBeta

public double getBeta(int d)
Gets the beta value for a particular domain

Parameters:
d - the domain
Returns:
beta

addAlpha

public void addAlpha(double a,
                     int d)
Adds a value to the alpha function, used in updating

Parameters:
a - amount to add to alpha
d - the domain to which to add

addBeta

public void addBeta(double a,
                    int d)
Adds a value to the alpha function, used in updating

Parameters:
a - amount to add to alpha
d - the domain to which to add

setAlpha

public void setAlpha(double a,
                     int d)
Sets alpha at a specific value

Parameters:
a - the value to set alpha
d - the domain

setBeta

public void setBeta(double b,
                    int d)
Sets beta at a specific value

Parameters:
b - the value to set beta
d - the domain

setAlpha

public void setAlpha(double[] a)
Sets alpha at a specific value

Parameters:
a - the value to set alpha

setBeta

public void setBeta(double[] b)
Sets beta at a specific value

Parameters:
b - the value to set beta

randomAB

public void randomAB(int a,
                     int b,
                     int d)
Randomizes alpha and beta. Numbers are chosen between 0 and the specified upper bound. Sets maxalpha and maxbeta to a and b. Requires random to be set.

Parameters:
a - Upper bound for alpha
b - Upper bound for beta
d - The number of domains

randomAB

public void randomAB(int d)
Randomizes alpha and beta. Numbers are chosen between 0 and the specified upper bound. Uses maxalpha and maxbeta which must already be set Requires random to be set.

Parameters:
d - The number of domains

randomAB

public void randomAB()
Randomizes alpha and beta. Numbers are chosen between 0 and the specified upper bound. Uses maxalpha and maxbeta which must already be set, and resets for the same size domain. Requires random to be set.


initAB

public void initAB(int d)
Initializes the alpha's without setting them.

Parameters:
d - the number of domains

setMutationRule

public void setMutationRule(MutationRule m)

getMutationRule

public MutationRule getMutationRule()

setMutation

public void setMutation(double m)
                 throws ModelError
Calls the mutationRule set function.

Parameters:
m - the mutation rate
Throws:
ModelError - if the mutation rate is outside of [0,1]

getMutation

public double getMutation()
Calls the mutationRules get function.

Returns:
the mutation rate

mutate

public void mutate()
            throws ModelError
Calls the mutationRules mutate function.

ModelError

setNetwork

public void setNetwork(Individual[] nw)
Sets the network (all other individuals in the model).

Parameters:
nw - an array of Individuals that constitutes the network

getNetwork

public Individual[] getNetwork()
Returns the network (all other individuals in the model).

Returns:
the network

clearConnected

public void clearConnected()
Clears the list of neighbors.


setConnected

public void setConnected(java.util.HashSet c)
Sets the neighbors to a particular HashSet of individuals

Parameters:
c - The new neighbors

addConnected

public void addConnected(Individual i)
Adds a neighbor to an individuals current neighbors.

Parameters:
i - The individual to add to the list of neighbors

getConnected

public java.util.HashSet getConnected()
Returns the individuals neighbors

Returns:
A HashSet of Individuals

setRandom

public void setRandom(MersenneTwister r)
Sets the random number generator

Parameters:
r - The MersenneTwister random number generator

getRandom

public MersenneTwister getRandom()
Returns the random number generator

Returns:
The MersenneTwister random number generator

setConnectionRule

public void setConnectionRule(ConnectionRule c)
Sets the connection rule, which determines how the individual updates its neighborhood

Parameters:
c - The connection rule

formConnections

public void formConnections()
Uses the connection rule to update the neighborhood.


getConnectionRule

public ConnectionRule getConnectionRule()
Returns the connection rule

Returns:
The connection rule

setLearningRule

public void setLearningRule(LearningRule l)
Sets the learning rule for updating strategies

Parameters:
l - The learning rule to set

getLearningRule

public LearningRule getLearningRule()
Returns the learning rule

Returns:
The learning rule

setStrat

public void setStrat(int s)
              throws ModelError
Sets the current strategy of a user

Parameters:
s - The strategy
Throws:
ModelError - if the strategy is negative or bigger than the possible number of strats

chooseStrat

public int chooseStrat()
                throws ModelError
Uses the learning rule to choose a strategy

Returns:
the strategy just selected
Throws:
ModelError - if there is a problem selecting the strategy

getStrat

public int getStrat()
             throws ModelError
Returns the current strategy, if its not set chooses one.

Returns:
the strategy
Throws:
ModelError - if there is a problem choosing the strategy.

getLastStrat

public int getLastStrat()
This returns the strategy used on the last round

Returns:
The strategy

setLastStrat

public void setLastStrat()
This updates the strategy for the next round by setting the last round strat equal to the current strat.


getPayoff

public int getPayoff()
Returns the payoff from the last round

Returns:
the payoff

setPayoff

public void setPayoff(int p)
Sets the payoff from the last generation

Parameters:
p - the payoff

addPayoff

public void addPayoff(int p)
Adds a value to the current payoff

Parameters:
p - The amount to add

getLastPayoff

public int getLastPayoff()
Returns the payoff from the last round


setLastPayoff

public void setLastPayoff()
Sets the last round payoff equal to the current payoff


setGame

public void setGame(Game g)
Sets the game

Parameters:
g - The game

getGame

public Game getGame()
Returns the game

Returns:
The game

reset

public void reset()
           throws ModelError
Resets for a new generation. This runs the new generation function of the connection and learning rules, chooses a new strategy, forms new connections, trys to mutate, and sets the payoff to zero.

Throws:
ModelError - If there is some problem

playGame

public void playGame()
              throws ModelError
Plays the game with each neighbor. If the game is marked as strategic, this goes through each neighbor and palys the game adding the payoff to the total payoff. Otherwise, plays the game once and adds this to the payoff.

Throws:
ModelError - If there are any problems

update

public void update()
            throws ModelError
Updates after play is complete. This only calls the learningrule process payoff function

Throws:
ModelError - From processpayoff

initStart

public void initStart(int s)
               throws ModelError
The initializes the individual for single domain models by giving them random beliefs and randomizing the weights The random beliefs are constrained by the zeroconstraint.

Parameters:
s - The number of strategies
Throws:
ModelError - If there are any problems

initStart

public void initStart(int d,
                      int s)
               throws ModelError
The initializes the individual for multi domain models by giving them random beliefs and randomizes the the wieghts The random beliefs are constrained by the zeroconstraint. BTW, this is not configured for multidomain weight functions, only beleifs.

Parameters:
s - The number of strategies
Throws:
ModelError - If there are any problems