lik = np.log(np.array([0.00001])) hyp = np.log(np.array([1, 1, 10])) cov = NormalARD() gp = GaussianProcess(lik, hyp, cov) gp2 = GaussianProcess(lik, hyp, cov) sig =np.ones((3,)) * 0.001 sig2 = np.ones((3,)) * 0.1 start_z = np.array([[0., 0., 0.]]) agent = Agent(gp, reward, sig, start_z) agent2 = Agent(gp2, reward, sig2, start_z) fig = plt.figure(figsize=(20,7), dpi=300) zlim = (-10, 10, -10, 10) for i in xrange(0, 1000): agent.observe() agent.decide() agent.act() agent2.observe() agent2.decide() agent2.act() t = agent.gp.Z[-1].flatten()[-1] a = [0] * 4 a[0] = agent.gp.Z[-1].flatten()[0] a[1] = agent.gp.Z[-1].flatten()[1] a[2] = agent.gp.Z[-1].flatten()[0] a[3] = agent.gp.Z[-1].flatten()[1] extent = np.max(np.abs(a)) lim = extent + 3 if extent > 10 else 10 zlim = (-lim, lim, -lim, lim) fig.clf()
def execute(self): ## ## Initialize agents ## pDisease = {Constant.BETA: 1 - math.exp(-self.disease[Constant.BETA]), Constant.RHO: self.disease[Constant.RHO], Constant.GAMMA: 1 - math.exp(-self.disease[Constant.GAMMA])} self.decision = 1 - math.exp(-self.decision) N = 0 agents = [] infected = [] for state in self.nAgents: for x in range(self.nAgents[state]): agent = Agent(N, state, pDisease, self.fear, self.timeHorizon, self.payoffs) agents.append(agent) if (state == State.I): infected.append(agent) N += 1 ## ## Output variables ## num = [] num.append([0, self.nAgents[State.S], self.nAgents[State.P], 0, self.nAgents[State.I], 0, 0, self.nAgents[State.R], 0, 0, self.nAgents[State.S] * self.payoffs[State.S], self.nAgents[State.P] * self.payoffs[State.P], self.nAgents[State.I] * self.payoffs[State.I], self.nAgents[State.R] * self.payoffs[State.R]]) ## ## Run the simulation ## t = 1 i = self.nAgents[State.I] / float(N) while ((t < self.timeSteps) and (i > 0)): numagents = [0, 0, 0, 0] ## ## Interaction ## shuffle(agents) n = N infected = [] while(n > 1): a1 = agents[n - 1] a2 = agents[n - 2] a1State = a1.getState() a2State = a2.getState() a1S = a1State a2S = a2State if (a1State == State.I): infected.append(a1) a2S = a2.interact(a1State) if (a2State == State.I): infected.append(a2) a1S = a1.interact(a2State) numagents[a1S] += 1 numagents[a2S] += 1 n = n - 2 ## ## Decision ## for agent in agents: if (uniform(0.0, 1.0) < self.decision): state = agent.getState() numagents[state] -= 1 state = agent.decide(i) numagents[state] += 1 ## ## Recover ## for agent in infected: if (agent.recover() == State.R): numagents[State.I] -= 1 numagents[State.R] += 1 num.append([t, numagents[State.S], numagents[State.P], 0, numagents[State.I], 0, 0, numagents[State.R], 0, 0, numagents[State.S] * self.payoffs[State.S], numagents[State.P] * self.payoffs[State.P], numagents[State.I] * self.payoffs[State.I], numagents[State.R] * self.payoffs[State.R]]) i = numagents[State.I] / float(N) t += 1 return num