Esempio n. 1
0
def createOrganicSuperteam(p,
                           subteamsSortedByStyle,
                           agentConstructor=CarDesignerWeighted):
    p.aiScore = None
    p.aiRange = None

    myTeam = createTeam(p, agentConstructor)
    agentsList = myTeam.agents
    agentStyles = [a.kai.KAI for a in agentsList]
    agentsSortedByStyle = [a for _, a in sorted(zip(agentStyles, agentsList))]
    #[t for _,t in sorted(zip(subTeamStyles,range(len(subTeamStyles))))]
    newSubTeamAllocations = [
        t for t in subteamsSortedByStyle
        for i in range(int(p.nAgents / p.nTeams))
    ]

    #now, re-allocate the same agents to teams suited to their style:
    for i, a in enumerate(agentsSortedByStyle):
        a.team = newSubTeamAllocations[i]

    myTeam.agents = agentsSortedByStyle

    for a in myTeam.agents:
        a.startSpeed = h.cp(a.speed)
        a.startTemp = h.cp(a.temp)
        a.myDims = p.teamDims[a.team]

    return myTeam
Esempio n. 2
0
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
Esempio n. 3
0
 def __init__(self, p):
     """
     Initialize the agent.
     """
     self.score = np.inf
     self.r = np.random.uniform(-1, 1, size=p.nDims)
     self.nmoves = 0
     self.kai = kai.KAIScore()
     self.speed = kai.calcAgentSpeed(self.kai.KAI, p)
     self.temp = kai.calcAgentTemp(self.kai.E, p)
     self.memory = [Solution(self.r, self.score, type(self))]
     self.team = -1
     self.decay = kai.calculateAgentDecay(self, 100)
     self.startTemp = h.cp(self.temp)
     self.startSpeed = h.cp(self.speed)
Esempio n. 4
0
    def getBestSolution(self):
        """
        Search an agent's memory for its best solution, and return the Solution.

        Returns:
        -------
        bestScore : Solution object, best of any past solution the agent has had
        """
        bestSolution = h.cp(self.memory[0])
        for mem in self.memory:
            if mem.score < bestSolution.score:
                bestSolution = mem
        return bestSolution
Esempio n. 5
0
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
Esempio n. 6
0
def share(a1,a2,selfBias=0):
    """
    Each agent chooses whether to accept shared solution or not

    Parameters
    ----------
    a1, a2 : Agent objects, the two agents communicating
    p : Params object, contains current model Parameters
    selfBias : float, (default: 0) agents percieve their own solution quality
    as this much better than reality.

    Returns
    -------
    True

    """
    copyOfA1 = h.cp(a1)
    considerSharedSoln(a1,a2,selfBias)
    considerSharedSoln(a2,copyOfA1,selfBias)
    return True
Esempio n. 7
0
 def __init__(self, r, score, agent_class=None):
     """ create a Solution object to hold parameters and score"""
     self.r = cp(r)  #note : cp() is a deep copy
     self.score = cp(score)
     self.agent_class = cp(agent_class)
Esempio n. 8
0
def asCarParameters(carList):
    """convert a parameter vector (list of float) to CarParameter object"""
    car = h.cp(blankParameterObject)
    for i in range(len(carList)):
        setattr(car,pNames[i],carList[i])
    return car
Esempio n. 9
0
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