def work(AgentConstructor,p=defaultParams, color = 'red',speed=None,temp=None,startPosition=None): """ Simulate one agent solving a problem independently, without a team. Parameters ---------- AgentConstructor : constructor class for the desired agent type p : Params object, contains current model Parameters color : string or rgb color, color to plot the path of the agent (only plots path if p.showViz == True) speed : float (default = None) If given, will give the agent a specific speed. Else drawn randomly. temp : float (default = None) If given, will give the agent a specific temperature. Else drawn randomly. startPosition : list of float, shape = [nDims] (default = none) If given, will start the agent a specific position. Else drawn randomly. Returns ------- a : AgentConstructor, an agent of class given by the first argument this agent contains it's history in agent.memory you can access the agent's best score using a.getBestScore() """ a = AgentConstructor(p.nDims) if p.aiScore is not None: a.kai = kai.findAiScore(p.aiScore,kaiPopulation) a.speed = h.bounds(p.AVG_SPEED + kai.standardizedAI(a.kai.KAI) * p.SD_SPEED, p.MIN_SPEED ,np.inf) a.temp = h.bounds(p.AVG_TEMP + kai.standardizedE(a.kai.E) * p.SD_TEMP, 0 ,np.inf) if p.startPositions is not None: a.startAt(p.startPositions[0]) if temp is not None: a.temp = temp if speed is not None: a.speed = speed a.startTemp = h.cp(a.temp) a.startSpeed = h.cp(a.speed) a.decay = kai.calculateAgentDecay(a, p.steps) scores = [] for i in range(p.steps): didMove = a.move(p,teamPosition = None) if didMove: scores.append(h.cp(a.score)) if(p.showViz and a.nmoves>0): # plt.scatter(a.rNorm[0],a.rNorm[1],c=color) plt.scatter(a.r[0],a.score,c=color) a.speed *= a.decay a.temp *= a.decay return a
def __init__(self, agentConstructor, p, kaiPopulation): """ Create a Team of given Agent class according to model parameters. Parameters ---------- agentConstructor : costructor Class for desired agents (eg, Steinway) p : Params object, contains current model Parameters kaiPopulation: list of KAIScore, virtual population to draw from Returns ------- self : Team object """ #initialize the team with the desired agents self.agents = [] aiScores = np.zeros(p.nAgents) #curated team composition: if (p.aiScore is not None) and (p.aiRange is not None): minScore = np.max([40, p.aiScore - p.aiRange / 2.0]) maxScore = np.min([150, p.aiScore + p.aiRange / 2.0]) aiScores = np.linspace(minScore, maxScore, p.nAgents) np.random.shuffle( aiScores) #randomly assign these to agents, not in order... for i in range(p.nAgents): a = agentConstructor(p) #creates an agent with random kai score if p.startPositions is not None: a.startAt(p.startPositions[i]) #if necessary, give the agent a specific style (kai score): if (p.aiScore is not None) and (p.aiRange is not None): aiScore = aiScores[i] a.kai = kai.findAiScore(aiScore, kaiPopulation) a.speed = kai.calcAgentSpeed(a.kai.KAI, p) a.temp = kai.calcAgentTemp(a.kai.E, p) a.startSpeed = a.speed a.startTemp = a.temp #by default, all dimensions are owned by every agent a.myDims = np.ones(p.nDims) self.agents.append(a) self.nAgents = p.nAgents aiScores = [a.kai.KAI for a in self.agents] self.dAI = np.max(aiScores) - np.min(aiScores) #record the team's process and behaviors: self.nMeetings = 0 self.shareHistory = [] self.nTeamMeetings = 0 self.subTeamMeetings = 0 self.meetingDistances = [] self.scoreHistory = [] #if there are subteams owning certain dimensions, #each subteams dimensions are listed in a matrix self.specializations = p.teamDims
def createCustomTeam(p, agentConstructor = CarDesignerWeighted, subTeamToVary = 0, subTeamKAI = 95): """ Create a team of agents before running it through the simulation. Design the team with a specific composition of KAI scores, and subdivision of a problem into specialized subteams Parameters ---------- p : Params object, contains current model Parameters including p.nAgents, p.nTeams, p.nDims, p.AVG_SPEED, p.AVG_TEMP AgentConstructor : constructor class (default = Steinway) Returns ------- myTeam: Team object with desired characteristics, ready to run in the model """ p.aiScore = 95 p.aiRange = 0 myTeam = createTeam(p,agentConstructor) #now, we need to make a specific sub-team have a different ai composition for i, a in enumerate(myTeam.agents): if a.team == subTeamToVary: a.kai = kai.findAiScore(subTeamKAI) a.speed = kai.calcAgentSpeed(a.kai.KAI,p) a.temp = kai.calcAgentTemp(a.kai.E,p) a.decay = kai.calculateAgentDecay(a,p.steps) for a in myTeam.agents: a.startSpeed = h.cp(a.speed) a.startTemp = h.cp(a.temp) return myTeam
def createTeam(p,agentConstructor = Steinway): """ Create a team of agents before running it through the simulation. Design the team with a specific composition of KAI scores, and subdivision of a problem into specialized subteams Parameters ---------- p : Params object, contains current model Parameters including p.nAgents, p.nTeams, p.nDims, p.AVG_SPEED, p.AVG_TEMP AgentConstructor : constructor class (default = Steinway) Returns ------- myTeam: Team object with desired characteristics, ready to run in the model """ np.random.seed() # p.agentTeams = specializedTeams(p.nAgents,p.nTeams) # p.teamDims = teamDimensions(p.nDims,p.nTeams) # print(teamDims) myTeam = Team(agentConstructor,p,kaiPopulation) for i in range(len(myTeam.agents)): a = myTeam.agents[i] aTeam = p.agentTeams[i] a.team = aTeam a.myDims = p.teamDims[aTeam] a.decay = kai.calculateAgentDecay(a,p.steps) #adding an option to specify the exact list of KAI scores for a team (Sam 2020/02/11) #to use this, add a list of KAI scores as an attribute called "kaiList" to the Parameters object p #for example: p.kaiList = [70,80,80,101,84] #the list must have the same number of elements as there are agents on the team #to explicitly NOT use this option, set p.kaiList = None (which is the default value) if p.kaiList is not None: if len(p.kaiList) != len(myTeam.agents): raise ValueError(f'The length of Parameters.kaiList ({len(p.kaiList)}) does not match the number of agents on the team ({len(myTeam.agents)}).') for i,a in enumerate(myTeam.agents): a.kai = kai.findAiScore(p.kaiList[i]) a.speed = kai.calcAgentSpeed(a.kai.KAI,p) a.temp = kai.calcAgentTemp(a.kai.E,p) a.decay = kai.calculateAgentDecay(a,p.steps) elif p.curatedTeams and p.aiRange is not None and p.aiScore is not None: for team in range(len(p.teamDims)): teamAgents=[a for a in myTeam.agents if a.team == team] if len(teamAgents)<2: a = teamAgents[0] a.kai = kai.findAiScore(p.aiScore,kaiPopulation) a.speed = kai.calcAgentSpeed(a.kai.KAI,p) a.temp = kai.calcAgentTemp(a.kai.E,p) #should a.decay be recalculated here? I think it should... (Sam 2020/02/11) else: for i in range(len(teamAgents)): myKai = p.aiScore - p.aiRange/2.0 + p.aiRange*(float(i)/(len(teamAgents)-1)) a = teamAgents[i] a.kai = kai.findAiScore(myKai,kaiPopulation) a.speed = kai.calcAgentSpeed(a.kai.KAI,p) a.temp = kai.calcAgentTemp(a.kai.E,p) #should a.decay be recalculated here? I think it should... (Sam 2020/02/11) for a in myTeam.agents: a.startSpeed = h.cp(a.speed) a.startTemp = h.cp(a.temp) return myTeam