def constrain(car,dimsToConstrain=np.ones(len(carParamsDF))): # p_attribute = getAttr(car) paramIndices = [i for i in range(len(dimsToConstrain)) if dimsToConstrain[i] ==1] for i in paramIndices: #range(len(carParamsDF)): # we need to do the equations bounds last # if not dimsToConstrain[i]: #we don't need to check this dimension, it didn't change # continue param = carParamsDF.loc[i] variable = param.variable value = getattr(car,variable) if param.kind == 1: #continuous param with min and max if hasNumericBounds[i]: newValue = h.bounds(value,float(param["minV"]),float(param["maxV"])) setattr(car,variable,newValue) #do the equation ones after setting all other parameters elif param.kind == 2: #choose a material based on density materialID,density,modulusE = findMaterialByDensity(value) setattr(car,variable,density) #find other variables that are driven by this one: for i, otherParam in carParamsDF[carParamsDF.team == variable].iterrows():#dimension is driven by this one setattr(car,otherParam.variable,modulusE) elif param.kind == 3: #choose tires tireID,radius,weight = findTireByRadius(value) setattr(car,variable,radius) #find other variables that are driven by this one: for i, otherParam in carParamsDF[carParamsDF.team == variable].iterrows(): setattr(car,otherParam.variable,weight) elif param.kind == 4: #choose motor tableRow= findEngineByPower(value) #Phi_e,l_e,w_e,h_e,T_e,m_e setattr(car,variable,tableRow[variable]) #find other variables that are driven by this one: for i, otherParam in carParamsDF[carParamsDF.team == variable].iterrows(): setattr(car,otherParam.variable,tableRow[otherParam.variable]) elif param.kind == 5: #choose brakes tableRow = findBrakesByR(value) # r is driving variable setattr(car,variable,tableRow[variable]) #df columns need to be same as variable names #find other variables that are driven by this one: for i, otherParam in carParamsDF[carParamsDF.team == variable].iterrows(): #their "team" is THIS VAR: dimension is driven by this setattr(car,otherParam.variable,tableRow[otherParam.variable]) elif param.kind == 6: #choose suspension tableRow = findSuspensionByK(value) #kfsp, cfsp, mfsp setattr(car,variable,tableRow[variable]) #find other variables that are driven by this one: for i, otherParam in carParamsDF[carParamsDF.team == variable].iterrows():#their "team" is THIS VAR: dimension is driven by this setattr(car,otherParam.variable,tableRow[otherParam.variable]) #now we can do the ones that depend on other variables for i in paramIndices: param = carParamsDF.loc[i] variable = param.variable value = getattr(car,variable) if param.kind == 1 and not hasNumericBounds[i]: f = minMaxParam[i] #list of minMax functions for each variable minV, maxV = f(car) newValue = h.bounds(value,minV,maxV) setattr(car,variable,newValue) return car
def constrain(self, minDim=.007, maxDim=1): #constrain all dimensions of the beam to a min and max dimension self.thickness = bounds(self.thickness, minDim, maxDim) self.innerHeight = bounds(self.innerHeight, minDim, maxDim) self.outerHeight = bounds(self.outerHeight, minDim, maxDim) self.width = bounds(self.width, minDim, maxDim) if (self.outerHeight - self.innerHeight) < (2 * minDim): self.innerHeight = self.outerHeight - minDim * 2
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 constrain(self, x, p): """ constrain solution [x] to the feasible space bounded by [p.spaceSize] Parameters: ---------- p : Params object, contains current model Parameters Returns: ------- x : constrained solution """ x[0] = h.bounds(x[0], 0, .9999) x[1] = h.bounds(x[1], 0, .9999) return x
def calculateAgentDecay(agent, steps): """ Calculate the gemoetric decay ratio of speed and temp for a specific agentTeams Agents with high (innovative) E score don't decay in Temperature as much as low-E score agents. Therefore, adaptive agents will converge / cool quickly while innovative agents may never converge or cool down. parameters: ---------- agent: Agent object steps: number of steps in the simulation (stored in Params as p.steps) returns: ------- ratio : geometric decay ratio for temperature and speed Warning: if T0 < Tf or T0<=0, will return zero """ E_N = standardizedE(agent.kai.E) E_transformed = np.exp((E_N*-1)+2) startEndRatio = h.bounds(1/E_transformed, 1E-10,1) T0 = agent.temp TF = T0 * startEndRatio ratio = calculateDecay(steps,T0,TF) return ratio
def calcAgentSpeed(kai, p): """ Calculate starting speed of agent based on KAI score and parameters parameters: ---------- kai: KAIScore object, for this agent p : Params object, contains current model Parameters returns: ------- speed : float, agent's starting speed """ speed = h.bounds(np.exp(standardizedAI(kai))* p.AVG_SPEED, p.MIN_SPEED ,np.inf) return speed
def constrain(self, x, p): """ constrain solution [x] to the feasible space bounded by [p.spaceSize] Parameters: ---------- p : Params object, contains current model Parameters Returns: ------- x : constrained solution """ for i in range(len(x)): x[i] = h.bounds(x[i], -1 * p.spaceSize, 1 * p.spaceSize) return x
def constrain(self, x, p): for i in range(len(x)): x[i] = h.bounds(x[i], -1 * p.spaceSize, 1 * p.spaceSize) return x