Package teamwork :: Package dynamics :: Module arbitraryDynamics
[hide private]
[frames] | no frames]

Source Code for Module teamwork.dynamics.arbitraryDynamics

  1  from teamwork.math.Interval import Interval 
  2  from teamwork.utils.PsychUtils import * 
  3   
  4  DYNAMICS = 'object' 
  5  #DYNAMICS = 'function' 
  6   
7 -class BaseDynamics:
8 """Class of objects that represent the dynamics of a specific 9 state/belief feature in the simulation""" 10
11 - def __init__(self,args=None):
12 if args is None: 13 args = {} 14 self.args = args
15
16 - def apply(self,entity,value,action,world,debug=0):
17 """Takes an entity with the specified initial value for the 18 feature of interest and returns a new value in response to the 19 specified action being taken within the specified world 20 context (in the form of a dictionary of entity objects, 21 indexed by entity name)""" 22 return value
23
24 - def instantiate(self,entity,action):
25 return self
26
27 - def invert(self,entity,value,action,world,debug=0):
28 return value
29
30 - def getIncrement(self):
31 """Returns the incremental delta by which this dynamics 32 function adjusts values; this delta is stored in the 33 'increment' field of the 'args' attribute of this object; if 34 none is specified, defaults to 0.1""" 35 try: 36 return self.args['increment'] 37 except KeyError: 38 return 0.1
39
40 - def reset(self):
41 """Deletes any stored information (e.g., partial results) 42 before beginning dynamics computation (should be 43 overridden by subclass)""" 44 pass
45
46 -def genericDynamics(entity,actor,object,oldValue,actParam,objParam):
47 if entity.name == object.name: 48 return normalize(oldValue-objParam) 49 elif entity.name == actor.name: 50 return normalize(oldValue+actParam) 51 else: 52 return oldValue
53
54 -class FunDynamics(BaseDynamics):
55 - def apply(self,entity,value,action,world,debug=Debugger()):
56 actor = action['actor'] 57 if type(actor) is InstanceType: 58 actor = actor.name 59 if action['type'][:6] == 'moveTo': 60 destination = action['type'][6:] 61 origin = world[actor].getState('floor') 62 if entity == actor: 63 pass 64 elif entity.getState('floor') == origin: 65 pass 66 elif entity.getState('floor') == destination: 67 pass 68 else: 69 return value 70 else: 71 return value
72
73 -class SupportDynamics(BaseDynamics):
74 - def __init__(self,args={}):
75 BaseDynamics.__init__(self,args) 76 self.results = {}
77
78 - def apply(self,entity,value,action,world,debug=Debugger()):
79 try: 80 # move up a level to get supporter ? 81 supporter = world[entity].parent 82 except KeyError: 83 return value 84 actor = action['actor'] 85 if type(actor) is InstanceType: 86 actor = actor.name 87 ## if entity != actor or actor == self.args['entity']: 88 if actor == self.args['entity']: 89 return value 90 debug.message(8,'Computing support of %s for %s...' \ 91 % (supporter.ancestry(),actor)) 92 if not self.results.has_key(actor): 93 self.results[actor] = supporter.actionSupport(action,debug) 94 delta = self.results[actor] 95 if entity != actor: 96 try: 97 scale = supporter.getEntity(actor).getSupport(entity).mean() 98 except KeyError: 99 scale = 0.0 100 ## # A bit of a hack, to use parent's beliefs about the 101 ## # support if I don't have any of my own 102 ## try: 103 ## scale = supporter.parent.getEntity(actor).getSupport(entity).mean() 104 ## scale = scale / 1.5 105 ## except KeyError: 106 ## scale = 0.0 107 ## except AttributeError: 108 ## scale = 0.0 109 delta = delta * scale 110 if type(value) is FloatType: 111 value = normalize(value+delta) 112 else: 113 value += delta 114 debug.message(8,'New support value = %s' % (`value`)) 115 return value
116
117 - def reset(self):
118 for key in self.results.keys(): 119 del self.results[key]
120 121 # Dynamics functions
122 -def violence2power(entity,actor,victim,oldPower,invert=None):
123 """Dynamics function for change in power due to act of violence""" 124 if invert: 125 if entity.name == victim.name: 126 return normalize(oldPower+0.1) 127 elif entity.name == actor.name: 128 return normalize(oldPower-0.1) 129 else: 130 return oldPower 131 else: 132 if entity.name == victim.name: 133 return normalize(oldPower-0.1) 134 elif entity.name == actor.name: 135 return normalize(oldPower+0.1) 136 else: 137 return oldPower
138
139 -def violence2hardship(entity,actor,victim,oldHardship,invert=None):
140 """Dynamics function for change in hardship due to act of violence""" 141 if entity.name == victim.name: 142 if invert: 143 # Do something clever 144 pass 145 else: 146 return delta(actor,oldHardship,'power') + decay(oldHardship) 147 else: 148 return decay(oldHardship,invert)
149 150 decayRate = 0.9 151
152 -def decay(value,invert=None):
153 if invert: 154 return value / decayRate 155 else: 156 return value * decayRate
157
158 -def delta(actor,oldValue,feature):
159 try: 160 return ((1.0-decayRate)*actor.getState(feature))/oldValue 161 except ZeroDivisionError: 162 return ((1.0-decayRate)*actor.getState(feature))/0.1
163
164 -def nullDynamics(entity,actor,object,oldValue):
165 return oldValue
166