def testBadRange(self): """Tests the function with minValue > maxValue.""" try: util.limit(2.0, 5.0, 1.0) except ValueError: pass else: self.fail()
def myfun(t, x): #function used by the ODE solver #must return dx/dt global count count+=1 N=param.N state1,state2,prod=x_to_state(x) #fixup some negative values that cause us a problem - ODE solver tries to #explore disallowed regions fix=True if gverbose: sys.stdout.write('1 yA {}\n'.format(state1.yA)) sys.stdout.write('1 yB {}\n'.format(state1.yB)) sys.stdout.write('1 P {}\n'.format(state1.P)) sys.stdout.write('2 yA {}\n'.format(state2.yA)) sys.stdout.write('2 yB {}\n'.format(state2.yB)) sys.stdout.write('2 P {}\n'.format(state2.P)) #NOTE: since we have 1/P in our equations, we can't let P be zero, which #The ODE solver seems to want to explore if fix: state1.yA=util.limit(state1.yA,0,1) state1.yB=util.limit(state1.yB,0,1) state1.P=util.limit(state1.P,0.01,2) state1.T=util.limit(state1.T,0.01,1.5) state1.Tw=util.limit(state1.Tw,0.01,1.5) state2.yA=util.limit(state2.yA,0,1) state2.yB=util.limit(state2.yB,0,1) state2.P=util.limit(state2.P,0.01,2) state2.T=util.limit(state2.T,0.01,1.5) state2.Tw=util.limit(state2.Tw,0.01,1.5) #Figure out what state we are in #Now calculate the input and output pressures and orifices based on the mode mode, half=find_mode(t,state1,state2,prod) if count % 5000 == 0: sys.stdout.write('***{:10.2f} myfun {:7.4f} half {} mode {} min {:6.2f} max {:6.2f}\n'.format( time.time()-tstart,t,half,mode,min(x),max(x))) #two pressures we will use P_feed=param.feed_pressure*1e5/norm.P0 P_atm=1e5/norm.P0 P_product=param.product_pressure*1e5/norm.P0 conn1=connections(state1,state2,prod,mode,half) conn2=connections(state2,state1,prod,mode,1-half) prod_orifice=0 supply_tank=state1 if mode==2: prod_orifice=param.output_orifice if half==0: supply_tank=state1 else: supply_tank=state2 rate1=rate_of_change(state1,**conn1, verbose=gverbose, modehalf=(mode,half)) rate2=rate_of_change(state2,**conn2, modehalf=(mode,1-half)) prod_rate=rate_of_change_product(prod,supply_tank,prod_orifice,verbose=gverbose) ret=np.hstack(rate1+rate2+prod_rate) return ret
def step(self, action, npc_action=None): """Advances the environment forward by one time step. Args: action: The action the agent should take, in format (linear acceleration, angular acceleration). npc_action: A list of actions to control the NPCs. Optional. """ # Bound the action values. limit(action[0], -1, 1) limit(action[1], -1, 1) # Convert the actions, which represent percentages, to the correct units. acc, theta = input_to_action(action) # Move the agent. self.agent.move(acc, theta) # Impose a limit on the agent's speed. self.agent.set_speed( limit(self.agent.get_speed(), -self.max_speed, self.max_speed)) # Move the traffic. self.npc_manager.step(self.agent.bounding_box, npc_action) # Update the view if we're in rendering or vision mode. if self.render or self.vision: # Collect a list of all the cars and their images and states. cars = self._get_cars() # Get the view. surf = self.view.update(self.agent.get_x(), self.agent.get_y(), cars) # Render, if necessary. if self.render: self.display_surface.blit(surf, (0, 0)) pygame.display.update() obs = self._get_observation() if self.tick: time.sleep(1.0 / global_var.FPS) done = self._get_done() self._keep_agent_in_map() return obs, self.reward(obs), done
def accelerate(self): """Updates speed at appropriate acceleration level.""" curr_speed = self.speed_boost * (self.speed + ( (self.settings["top_speed"] / self.settings["acceleration_factor"]) * self.acceleration)) self.speed = u.limit(curr_speed, 0, self.speed_boost * self.settings["top_speed"])
def _checkEntryValue(self, entry, name): ok = True try: value = float(entry.get_text()) except ValueError: fixedValue = getattr(self, name) ok = False else: if name in ('heatingCurrentWhileIdle', 'heatingCurrentInSafeMode'): minValue = 0.0 elif name == 'maxHeatingCurrent': iSafe = self.heatingCurrentInSafeMode iIdle = self.heatingCurrentWhileIdle minValue = max(iSafe, iIdle, 0.1) else: minValue = 0.1 if name in ('heatingCurrentWhileIdle', 'heatingCurrentInSafeMode'): maxValue = self.maxHeatingCurrent else: maxValue = 10000.0 fixedValue = limit(value, minValue, maxValue) if value != fixedValue: ok = False if not ok and BEEP: gtk.gdk.beep() entry.set_text(str(float(fixedValue))) setattr(self, name, fixedValue) return ok
def steer(self, segment): """Updates x to simulate steering.""" bounds = s.TUNNEL_BOUNDS if self.in_tunnel else s.BOUNDS self.x = u.limit(self.x + self.direction, -bounds, bounds) # Apply centrifugal force if we are going around a corner. if segment.curve != 0 and self.status == self.ALIVE: self.x -= (self.direction_speed() * self.speed_percent() * segment.curve * self.settings["centrifugal_force"])
def steer(self, segment): """Updates x to simulate steering.""" self.x = u.limit(self.x + self.direction, -s.BOUNDS, s.BOUNDS) # Apply centrifugal force if we are going around a corner. if segment.curve != 0: self.x -= (self.direction_speed() * self.speed_percent() * segment.curve * s.CENTRIFUGAL_FORCE)
def steer(self, segment): """Updates x to simulate steering.""" bounds = s.TUNNEL_BOUNDS if self.in_tunnel else s.BOUNDS self.x = u.limit(self.x + self.direction, -bounds, bounds) # Apply centrifugal force if we are going around a corner. if segment.curve != 0 and self.status == PlayerStatus.alive: # Congratulate player if they've broken personal record. self.x -= (self.direction_speed() * self.speed_percent() * segment.curve * self.settings["centrifugal_force"])
def limit(self): " limit all values to 0-1 range " self.up=util.limit(self.up,0,1) self.right=util.limit(self.right,0,1) self.down=util.limit(self.down,0,1) self.left=util.limit(self.left,0,1)
def accelerate(self): """Updates speed at appropriate acceleration level.""" self.speed = u.limit(self.speed + (s.ACCELERATION * self.acceleration), 0, s.TOP_SPEED)
def testFloat(self): """Tests the function with float arguments.""" for arguments, expected in self.TESTS: result = util.limit(*arguments) self.assertEqual(result, expected) self.assertTrue(isinstance(result, float))
def OnMouseButtonEvent(self, event): """mouse events, handle selection, clicks""" fullline = False update = False x, y = event.GetX(), event.GetY() line = (y / self.fh + self.sy) offset = offset_line = line * self.formater.width #calculate offset of fist char in line, x handled below xchar = x / self.fw """ if xchar < self.hex_start: #address area fullline = True editmode = EDIT_NIBBLE_HIGH elif xchar < self.hex_end: #hex area offset += (xchar - self.hex_start) / 3 if ((xchar - self.hex_start) % 3 < 1) or fullline: editmode = EDIT_NIBBLE_HIGH else: editmode = EDIT_NIBBLE_LOW """ if xchar < self.ascii_start: #between hex and ascii area fullline = True editmode = EDIT_BYTES elif xchar < self.ascii_end: #ascii area offset += xchar - self.ascii_start editmode = EDIT_BYTES else: #right of ascii area fullline = True editmode = EDIT_BYTES size = max(0, self.model.size() - 1) offset = util.limit(0, size, offset) if event.LeftDown(): self.CaptureMouse() self.leftdragging = True if fullline: offset = util.limit(0, size, offset_line + self.formater.width - 1) self.selection_start = offset_line else: self.selection_start = offset self.selection_end = self.offset = offset update = True elif event.Dragging() and self.leftdragging: if fullline: offset = util.limit(0, size, offset_line + self.formater.width - 1) if self._mouse_lastoffset != offset: self.selection_end = self.offset = offset update = True elif event.LeftUp(): if self.leftdragging: self.ReleaseMouse() self.leftdragging = False if fullline: offset = util.limit(0, size, offset_line + self.formater.width - 1) if self.leftdragging: if self.selection_end != offset: self.selection_end = offset update = True if update: self.editmode = editmode self.ensureVisible() self._mouse_lastoffset = offset
def mouse_on(self) -> bool: """click_area-의 영역 안에 마우스 커서가 있는지를 확인합니다.""" return limit(self.mouse_manager.x, self.x, self.x + self.width) == self.mouse_manager.x \ and limit(self.mouse_manager.y, self.y, self.y + self.height) == self.mouse_manager.y
def accelerate(self): """Updates speed at appropriate acceleration level.""" curr_speed = self.speed_boost * (self.speed + ((self.settings["top_speed"] / self.settings["acceleration_factor"]) * self.acceleration)) self.speed = u.limit(curr_speed, 0, self.speed_boost * self.settings["top_speed"])
def _keep_agent_in_map(self): """Keeps the agent inside the map by limiting its position.""" x = limit(self.agent.get_x(), 0, self.width) y = limit(self.agent.get_y(), 0, self.height) self.agent.set_x(x) self.agent.set_y(y)
def testInteger(self): """Tests the function with integer arguments.""" for arguments, expected in self.TESTS: result = util.limit(*[self._intIfNotNone(n) for n in arguments]) self.assertEqual(result, int(expected)) self.assertTrue(isinstance(result, int))
def train_log(X, y, eps, delta): """ : param a: start search point of a :param X: :param y: :param lamda: maximum alpha :param eps: eps limit for each class :param delta: :return: """ #generate grid search for alpha log_term = np.log(2 / delta) a = np.maximum(1 / eps * (1 + log_term), 0) + 2 alpha, gamma = util.limit(a, eps) #alpha = 100 num_l = 10 d = X.shape[1] + 1 x1 = X num_cls = len(np.unique(y)) #num_cls = np.max(y[:2]) + 1 alpha_list = np.linspace(a + 0.2, alpha, num=num_l) utility_list = np.zeros([num_l, num_cls]) min_eigen = np.zeros_like(utility_list) # shape of number_lambda * class gamma_list = np.zeros_like(utility_list) coeff_list = [] # shape is number of lamda * cls * d+1 hessian_list = [] for idx, lamda in enumerate(alpha_list): clf = LogisticRegression(penalty='l2', C=2 / lamda, solver='sag', multi_class='ovr').fit(X, y) # add bias column for coef coeff = clf.coef_ #doesn't involve bias here, bias is self.intercept_ bias = clf.intercept_ bias = np.expand_dims(bias, axis=1) # coeff refer to theta star in paper, should be cls * d+1 coeff = np.concatenate((coeff, bias), axis=1) coeff_list.append(coeff) hessian_array = [] print('lambda={} score={}'.format(lamda, clf.score(X, y))) p1 = clf.predict_proba( x1) #shape n*k, k is class, remove the normalize part p2 = np.ones([p1.shape[0], p1.shape[1]]) - p1 p3 = p1 * p2 # shape of p3 is n * k #p3 = p3.transpose() # shape of p3 is k*n new_col = np.ones([len(x1), 1]) # for bias term, \belta* 1 new_X = np.append(x1, new_col, axis=1) for cls in range(len(p3[0])): #enumerate all class diag_w = np.diag(p3[:, cls]) hessian = -np.dot(new_X.T, diag_w) hessian = np.dot(hessian, new_X) hessian_array.append(hessian) cur_lamda = np.linalg.cond(hessian, p=-2) min_eigen[idx][cls] = cur_lamda gamma = util.obtain_gamma(cur_lamda + lamda, eps, delta) gamma_list[idx][cls] = gamma cur_utility = 1 / gamma * np.exp( np.sqrt(np.log(1 / delta) / (gamma * (cur_lamda + lamda)))) # need to check utility_list[idx][cls] = cur_utility hessian_list.append(hessian_array) record = [] utility_list = utility_list.transpose( ) #after transpose, shape should be class* number of lamda gamma_list = gamma_list.transpose() min_eigen = min_eigen.transpose() theta = [] for idx in range(num_cls): max_utility = np.argmin(utility_list[idx]) cur_record = {} lamda = min_eigen[idx][max_utility] gamma = gamma_list[idx][max_utility] cur_record['lamda'] = lamda + alpha_list[max_utility] cur_record['gamma'] = gamma #cur_record['hessian'] = 1/(cur_record['lamda']*cur_record['gamma'])*np.eye() #cur_record['theta'] = coeff[max_utility][idx] theta_hat = coeff_list[max_utility][idx] theta_hat = coeff_list[max_utility][idx] + (1 / np.sqrt( cur_record['lamda'] * gamma)) * np.random.standard_normal(d) #theta_hat = np.random.multivariate_normal(coeff[max_utility][idx], hessian_list[max_utility][idx]) theta.append(theta_hat) return theta
def limit(self): " limit all values to 0-1 range " self.up = util.limit(self.up, 0, 1) self.right = util.limit(self.right, 0, 1) self.down = util.limit(self.down, 0, 1) self.left = util.limit(self.left, 0, 1)
def cheesydrive(self, throttle, wheel, quickturn, highgear): negintertia = wheel - self.oldwheel self.oldwheel = wheel if highgear: # TODO if is high gear wheel = util.sinscale(wheel, 0.6, count=2) else: wheel = util.sinscale(wheel, 0.5, count=3) negintertiabuffer = 0 if highgear: # TODO if high gear sensitivity = 0.65 negintertiascalar = 5 else: sensitivity = 0.75 if wheel * negintertia > 0: negintertiascalar = 2.5 else: if abs(wheel) > 0.65: negintertiascalar = 5 else: negintertiascalar = 3 negintertiapower = negintertia * negintertiascalar negintertiabuffer += negintertiapower wheel += negintertiabuffer linearpower = throttle if quickturn: if abs(linearpower) < 0.2: alpha = 0.1 self.quickstopbuffer = (1 - alpha) * self.quickstopbuffer + alpha * util.limit(wheel, 1.0) * 5 overpower = 1 angularpower = wheel else: overpower = 0 angularpower = abs(linearpower) * wheel * sensitivity - self.quickstopbuffer if self.quickstopbuffer > 1: self.quickstopbuffer -= 1 elif self.quickstopbuffer < -1: self.quickstopbuffer += 1 else: self.quickstopbuffer = 0 lpower = linearpower + angularpower rpower = linearpower - angularpower if lpower > 1: rpower -= overpower * (lpower - 1) lpower = 1 elif rpower > 1: lpower -= overpower * (-1 - lpower) rpower = 1 elif lpower < -1: rpower += overpower * (-1 * lpower) lpower = -1 elif rpower < -1: lpower += overpower * (-1 - rpower) rpower = -1 self.drive.setpower(lpower, rpower)