-
Notifications
You must be signed in to change notification settings - Fork 0
/
individualClass.py
120 lines (50 loc) · 2.86 KB
/
individualClass.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
import importlib
import configManager
config = importlib.import_module(configManager.configName)
import runtimeFunctions as rf
import time
import math
import numpy as np
np.set_printoptions(linewidth=300)
class Portfolio():
def __init__(self, agent, inputColumns, leisureColumns):
self.agent = agent
self.n = len(inputColumns[:,0])
self.currentIndex = -1
self.inputColumns = inputColumns
self.output = np.zeros((self.n, len(config.outputTypes)))
self.outputFilled = False
self.temporaryMatrix = np.zeros((self.n, len(self.inputColumns[0,:]) + len(config.outputTypes) + 1))
if "leisure" in config.outputTypes:
leisureIndex = config.outputTypes.index("leisure")
self.output[:,leisureIndex] = leisureColumns[:,0]
def getOutput(self, activityIndex):
# You have been given an index, which corresponds to the activity associated with the input vector, described in config.activities. This will tell you which function to use.
activity = config.activities[activityIndex]
blockLength = config.numInputs*config.numActivities
inputVector = self.inputColumns[:, np.arange(activityIndex, blockLength, config.numInputs)]*self.agent.resources
outputVector = getattr(rf, activity["outputFunction"])(self.agent, inputVector, activity)
outputIndex = activity["outputIndex"]
self.output[:, outputIndex] = np.add(self.output[:, outputIndex], outputVector)
def assembleOutput(self):
# For loop for activities because you have to call the functions explicitly, sorry.
if not self.outputFilled:
np.column_stack([self.getOutput(activityIndex) for activityIndex in np.arange(len(config.activities))])
self.outputFilled = True
util = np.sum(np.sqrt(self.output)*self.agent.typeWeights, axis = 1) * np.exp(-1*self.agent.getNormativeDistance(self.inputColumns[:, 0: config.numActivities*config.numInputs], None))
self.temporaryMatrix = np.column_stack((self.inputColumns, self.output, util))
def findBestPortfolio(self):
self.assembleOutput()
bestPortfolio = self.temporaryMatrix[np.where(self.temporaryMatrix[:,-1] == np.max(self.temporaryMatrix[:,-1]))]
portfolioInfo = {"util": bestPortfolio[0,-1]}
self.agent.inputVector = bestPortfolio[0, 0 : config.numInputs*config.numActivities]
self.agent.agreedUponTransfer = None
outputStart = config.numInputs*config.numActivities
for outputIndex, outputType in enumerate(config.outputTypes):
agentTypeOutput = bestPortfolio[0, outputStart + outputIndex]
if outputType in config.inputTypes:
self.agent.resources[outputIndex] += agentTypeOutput
elif outputType in config.nonInputTypes:
self.agent.nonInputResources[outputIndex] += agentTypeOutput
# I don't remove spent resources because you always have the time available to you. If activities cost money (like planting a crop), you'd remove resources here.
return [bestPortfolio, portfolioInfo]