def build_graph(self): """ Arrange all the nodes (or all but one) as a ring. Then link them depending on the kind of network desired. """ nbr_nodes = SimEngine.gui_get(NBR_NODES) graph_type = SimEngine.gui_get(GRAPH_TYPE) # If we are generating a star or a wheel network, arrange nbr_nodes-1 as # a ring and use the other node as the center node. # If we are generating another type of graph, arrange all the nodes as a ring. nbr_ring_nodes = (nbr_nodes - 1) if graph_type in ['star', 'wheel'] else nbr_nodes # create_ordered_agents() creates the indicated number of nodes and arranges # them in a ring. It returns a list of the nodes in ring-order. ring_node_list = self.create_ordered_agents(nbr_ring_nodes) if graph_type in ['star', 'wheel'] and nbr_nodes > 0: self.create_agents(1) # Now link the nodes according to the desired graph. if nbr_nodes: self.link_nodes_for_graph(graph_type, nbr_nodes, ring_node_list)
def build_initial_line(self): """ Construct the initial CA line. It is a random line if SimEngine.gui_get('Random?'). It is a line (of length ca_display_size) of 0's if SimEngine.gui_get('init_line') == ''. Otherwise it is the string in SimEngine.gui_get('init_line') converted into 0's and 1's. (' ' and '0' are converted to 0; everything else is converted to 1.) However, if the rule includes 000 -> 1,pad the line with 0's on both ends to fill the display. How much to put on each end depends on the user-specific initial line and the requested justification. """ if SimEngine.gui_get('Random?'): line = [choice([0, 1]) for _ in range(self.ca_display_size)] if self.lists else \ ''.join([choice(['0', '1']) for _ in range(self.ca_display_size)]) else: padding = self.padding_element * (self.ca_display_size) if SimEngine.gui_get('init_line') == '': line = padding else: line_0 = SimEngine.gui_get('init_line') # Convert line_0 to 0's and 1's # Treat '0' and ' ' as "not on". line = [0 if c in ' 0' else 1 for c in line_0] if self.lists else \ ''.join(['0' if c in ' 0' else '1' for c in line_0]) if SimEngine.gui_get('000'): justification = SimEngine.gui_get('justification') line_len = len(line) actual_padding = padding[line_len:] line = actual_padding + line if justification == 'Right' else \ line + actual_padding if justification == 'Left' else \ actual_padding[len(actual_padding)//2:] + line + actual_padding[len(actual_padding)//2:] return line
def end_commute(self): BraessParadoxWorld.travel_time = (World.ticks - self.birth_tick) / 450 if BraessParadoxWorld.avg == 0: BraessParadoxWorld.avg = BraessParadoxWorld.travel_time else: BraessParadoxWorld.avg = ((19 * BraessParadoxWorld.avg + BraessParadoxWorld.travel_time) / 20) if self.route == 0: if BraessParadoxWorld.top == 0: BraessParadoxWorld.top = BraessParadoxWorld.travel_time else: BraessParadoxWorld.top = (BraessParadoxWorld.travel_time + ( SimEngine.gui_get(SMOOTHING) - 1) * BraessParadoxWorld.top) / SimEngine.gui_get(SMOOTHING) else: if self.route == 1: if BraessParadoxWorld.bottom == 0: BraessParadoxWorld.bottom = BraessParadoxWorld.travel_time else: BraessParadoxWorld.bottom = (BraessParadoxWorld.travel_time + ( SimEngine.gui_get(SMOOTHING) - 1) * BraessParadoxWorld.bottom) / SimEngine.gui_get( SMOOTHING) else: if BraessParadoxWorld.middle == 0: BraessParadoxWorld.middle = BraessParadoxWorld.travel_time else: BraessParadoxWorld.middle = (BraessParadoxWorld.travel_time + ( SimEngine.gui_get(SMOOTHING) - 1) * BraessParadoxWorld.middle) / SimEngine.gui_get( SMOOTHING) # World.agents.remove gave errors, so move the agent to off-road instead self.move_to_patch(World.patches[BOTTOM_RIGHT + 3]) self.set_heading(0) print(BraessParadoxWorld.travel_time)
def step(self): """ Update the world by moving the agents. """ spawn_rate = SimEngine.gui_get('spawn rate') middle_on = SimEngine.gui_get("middle_on") delay_on = SimEngine.gui_get("delay") # check if the checkboxes have changed (Middle On? and Move by Delay?) World.highway.check_delay(middle_on, delay_on) World.highway.check_middle(middle_on, delay_on) # set the route count of each route to 0 World.num_top = 0 World.num_bot = 0 World.num_mid = 0 # move the computers self.move_commuters() # set the patch color and patch delay for patch in World.patches: if patch.road_type == 1: patch.determine_congestion(spawn_rate, World.highway, delay_on) # spawn agents self.spawn_commuter()
def determine_congestion(self): '''Determines the congestion in the top and bottom road segments''' #calculate congestion for the top road #patches in the top road top_road = self.patches_line(self.top_left_patch, self.top_right_patch)[1:-1] top_road_commuters = [ x for x in World.agents if x.current_patch() in top_road ] delay = len(top_road_commuters) * SimEngine.gui_get( VARIABLE_CONGESTION_DELAY) # print(len(top_road_commuters)) for patch in top_road: patch.delay = delay #calcultate the congestion for the bottom road delay = 1 bottom_road = self.patches_line(self.bottom_left_patch, self.bottom_right_patch)[1:-1] bottom_road_commuters = [ x for x in World.agents if x.current_patch() in bottom_road ] delay = len(bottom_road_commuters) * SimEngine.gui_get( VARIABLE_CONGESTION_DELAY) for patch in bottom_road: patch.delay = delay
def select_route(self): if SimEngine.gui_get(SELECTION_ALGORITHM) == EMPIRICAL_ANALYTICAl: return self.probabilistic_analytic() if SimEngine.gui_get(SELECTION_ALGORITHM) == PROBABILISTIC_GREEDY: return self.greedy() if SimEngine.gui_get(SELECTION_ALGORITHM) == BEST_KNOWN: return self.best_route()
def best_random_route(self): if SimEngine.gui_get(MIDDLE_ROUTE): if BraessParadoxWorld.middle == 0 or BraessParadoxWorld.top == 0 or BraessParadoxWorld.bottom == 0: return randint(0, 2) else: if randint(0, 99) < 100 - SimEngine.gui_get(RANDOMNESS): if BraessParadoxWorld.middle < BraessParadoxWorld.top and BraessParadoxWorld.middle < BraessParadoxWorld.bottom: return 2 else: if BraessParadoxWorld.top < BraessParadoxWorld.middle and BraessParadoxWorld.top < BraessParadoxWorld.bottom: return 0 else: return 1 else: return randint(0, 2) else: if BraessParadoxWorld.top == 0 or BraessParadoxWorld.bottom == 0: return randint(0, 1) else: if randint(0, 99) < 100 - SimEngine.gui_get(RANDOMNESS): if BraessParadoxWorld.top < BraessParadoxWorld.bottom: return 0 else: return 1 else: return randint(0, 1)
def build_initial_line(self): """ Construct the initial CA line. It is a random line if SimEngine.gui_get('Random?'). It is a line (of length ca_display_size) if SimEngine.gui_get('init_line') == ''. Otherwise it is the string in SimEngine.gui_get('init_line') converted into 0's and 1's. (' ' and '0' are converted to 0; everything else is converted to 1.) However, if the rule includes 000 -> 1,pad the line with 0's on both ends to fill the display. How much to put on each end depends on the user-specific initial line and the requested justification. """ if SimEngine.gui_get('Random?'): line = ... else: # A line of 0's. padding = [0] * (self.ca_display_size) if SimEngine.gui_get('init_line') == '': line = padding else: line_0 = SimEngine.gui_get('init_line') # Convert line_0 to 0's and 1's line = [... for c in line_0] # If the rule include 000 -> 1, fill out the new line with 0's. if SimEngine.gui_get('000'): justification = SimEngine.gui_get('justification') line_len = len(line) actual_padding = padding[line_len:] line = ... if justification == 'Right' else \ ... if justification == 'Left' else \ ... # justification == 'Center' return line
def force_as_dxdy(pixel_a: Pixel_xy, pixel_b: Pixel_xy, screen_distance_unit, repulsive): """ Compute the force between pixel_a pixel and pixel_b and return it as a velocity: direction * force. """ direction: Velocity = normalize_dxdy(( pixel_a - pixel_b) if repulsive else (pixel_b - pixel_a)) d = pixel_a.distance_to(pixel_b, wrap=False) if repulsive: dist = max( 1, pixel_a.distance_to(pixel_b, wrap=False) / screen_distance_unit) rep_coefficient = SimEngine.gui_get('rep_coef') rep_exponent = SimEngine.gui_get('rep_exponent') force = direction * (10**rep_coefficient) / 10 * dist**rep_exponent return force else: # attraction dist = max(1, max(d, screen_distance_unit) / screen_distance_unit) att_exponent = SimEngine.gui_get('att_exponent') force = direction * dist**att_exponent # If the link is too short, push away instead of attracting. if d < screen_distance_unit: force = force * (-1) att_coefficient = SimEngine.gui_get('att_coef') return 10**(att_coefficient - 1) * force
def setup(self): Agent.id = 0 Minority_Game_World.steps_to_win = SimEngine.gui_get(STEPS_TO_WIN) # Adjust how far one step is based on number of steps needed to win Minority_Game_World.one_step = (gui.PATCH_COLS - 2) * gui.BLOCK_SPACING() / Minority_Game_World.steps_to_win # For longer/shorter races, speed up/slow down frames/second gui.set_fps(round(6*Minority_Game_World.steps_to_win/50)) # self.done will be True if this a repeat game with the same agents. if self.done: self.reset_agents() return # This is the normal setup. Minority_Game_World.nbr_agents = SimEngine.gui_get(NBR_AGENTS) if Minority_Game_World.nbr_agents % 2 == 0: Minority_Game_World.nbr_agents += (1 if Minority_Game_World.nbr_agents < gui.WINDOW[NBR_AGENTS].Range[1] else (-1)) # gui.WINDOW[NBR_AGENTS].update(value=Minority_Game_World.nbr_agents) SimEngine.gui_set(NBR_AGENTS, value=Minority_Game_World.nbr_agents) Minority_Game_World.random_agent_ids = {0, Minority_Game_World.nbr_agents - 1} # Generate a random initial history self.history_length = SimEngine.gui_get(HISTORY_LENGTH) self.history = [choice([0, 1]) for _ in range(self.history_length)] self.generate_the_agents()
def select_route(self): if SimEngine.gui_get(SELECTION_ALGORITHM) == EMPIRICAL_ANALYTICAl: return self.analytical_route() if SimEngine.gui_get(SELECTION_ALGORITHM) == PROBABILISTIC_GREEDY: return self.probabilistic_greedy_route() if SimEngine.gui_get(SELECTION_ALGORITHM) == BEST_RANDOM: return self.best_random_route()
def selfish_route(self): agents_on_dynamic_road = 0 agents_on_static_road = 0 agents_on_middle_road = 0 for commuter in self.agents: if commuter.route == dynamic_road: agents_on_dynamic_road += 1 elif commuter.route == static_road: agents_on_static_road += 1 else: agents_on_middle_road += 1 static_road_rate = SimEngine.gui_get('static') dynamic_road_rate = SimEngine.gui_get('dynamic') static_road_time = static_road_rate + ( agents_on_middle_road + agents_on_static_road) / dynamic_road_rate middle_road_time = ((agents_on_dynamic_road + agents_on_middle_road) / dynamic_road_rate) \ + ((agents_on_static_road + agents_on_middle_road) / dynamic_road_rate) dynamic_road_time = (agents_on_dynamic_road / dynamic_road_rate) + static_road_rate if SimEngine.gui_get('middle_on'): three_roads = [(static_road, static_road_time / 20), (middle_road, middle_road_time / 20), (dynamic_road, dynamic_road_time / 20)] selection = min(three_roads, key=lambda x: x[1]) else: two_roads = [(static_road, static_road_time / 20), (dynamic_road, dynamic_road_time / 20)] selection = min(two_roads, key=lambda x: x[1]) return selection
def flock(self, showing_flockmates): # NetLogo allows one to specify the units within the Gui widget. # Here we do it explicitly by multiplying by BLOCK_SPACING(). vision_limit_in_pixels = SimEngine.gui_get('vision') * BLOCK_SPACING() flockmates = self.agents_in_radius(vision_limit_in_pixels) if len(flockmates) > 0: # If showing_flockmates, create links to flockmates if they don't already exist. if showing_flockmates: for flockmate in flockmates: # Don't make a link if it already exists. if not link_exists(self, flockmate): Link(self, flockmate, color=Color('skyblue3')) nearest_neighbor = min( flockmates, key=lambda flockmate: self.distance_to(flockmate)) min_separation = SimEngine.gui_get( 'minimum separation') * BLOCK_SPACING() if self.distance_to(nearest_neighbor) < min_separation: self.separate(nearest_neighbor) else: self.align(flockmates) self.cohere(flockmates)
def spawn_commuters(self): if self.spawn_time >= 250//SimEngine.gui_get('spawn_rate'): time = 0 if SimEngine.gui_get('mode') == selfish: route, time = self.route_dict[SimEngine.gui_get('mode')]() else: route = self.route_dict[SimEngine.gui_get('mode')]() # self.agent_class() creates a class at a certain pixel. # This line creates an agent at the center pixel of the top left patch. new_commuter: Commuter = self.agent_class(spawn_pixel=self.top_left_center_pixel, speed=0.0, birth_tick=self.ticks) new_commuter.route = route new_commuter.base_speed = new_commuter.base_speed - time new_commuter.speed = new_commuter.base_speed if new_commuter.route in (middle_road, dynamic_road): new_commuter.face_xy(self.top_right_center_pixel) # Static Route else: new_commuter.face_xy(self.bottom_left_center_pixel) self.spawn_time = 1 else: self.spawn_time += 1 # Note we probably won't use 250. # track spawn-time, if spawn-time is greater than 250 / spawn-rate, # at the patch in the upper left corner, spawn a commuter # with several values associated with time spawned, the time it is still commuting, # the route it will take, and it's behavior when choosing a route pass
def determine_congestion(self): trafic_variable_top_road = self.draw_line(self.top_left, self.top_right)[1:-1] trafic_variable_top_road_commuters = [ x for x in World.agents if x.current_patch() in trafic_variable_top_road ] delay = len(trafic_variable_top_road_commuters) * SimEngine.gui_get( VARIABLE_CONGESTION_DELAY) for patch in trafic_variable_top_road: patch.delay = delay #calcultate the congestion for the bottom road delay = 1 bottom_road = self.draw_line(self.bottom_left, self.bottom_right)[1:-1] bottom_road_commuters = [ x for x in World.agents if x.current_patch() in bottom_road ] delay = len(bottom_road_commuters) * SimEngine.gui_get( VARIABLE_CONGESTION_DELAY) for patch in bottom_road: patch.delay = delay
def best_random_route(self): middle_on = SimEngine.gui_get("middle_on") randomness = SimEngine.gui_get("randomness") middle = Commuter_World.middle bottom = Commuter_World.bot top = Commuter_World.top if middle_on: if middle == 0 or bottom == 0 or top == 0: return randint(0, 2) elif randint(0, 100) < (100 - randomness): if middle < top and middle < bottom: return 2 elif top < middle and top < bottom: return 0 else: return 1 else: return randint(0, 2) elif top == 0 or bottom == 0: return randint(0, 1) elif randint(0, 100) < (100 - randomness): if top < bottom: return 0 else: return 1 else: return randint(0, 1)
def mutate(self) -> Individual: if randint(0, 100) <= SimEngine.gui_get('replace_gene'): (self.chromosome, self.fitness, _) = self.replace_gene_in_chromosome(self.fitness, self.chromosome) if randint(0, 100) <= SimEngine.gui_get('reverse_subseq'): self.chromosome = self.reverse_subseq(self.chromosome) self.fitness = self.compute_fitness() return self
def __init__(self, **kwargs): color = SimEngine.gui_get(COLOR) color = Color(color) if color != RANDOM else None if 'shape_name' not in kwargs: shape_name = SimEngine.gui_get(SHAPE) kwargs['shape_name'] = shape_name super().__init__(color=color, **kwargs) # Is the node selected? self.selected = False
def setup(self): # Create a list of Individuals as the initial population. # self.pop_size must be even since we generate children two at a time. self.pop_size = (SimEngine.gui_get('pop_size')//2)*2 self.tournament_size = SimEngine.gui_get('tourn_size') GA_World.fitness_target = SimEngine.gui_get('fitness_target') self.population = self.initial_population() self.best_ind = None self.generations = 0 self.set_results()
def init_globals(self): gb.randomness = SimEngine.gui_get('Randomness') gb.mode = SimEngine.gui_get('Algorithm') gb.middle_on = SimEngine.gui_get('Middle') gb.spawn_rate = SimEngine.gui_get('Spawn Rate') gb.smoothing = SimEngine.gui_get('Smoothing') gb.top_left = World.patches_array[4, 4] gb.top_right = World.patches_array[4, 46] gb.bottom_right = World.patches_array[46, 46] gb.bottom_left = World.patches_array[46, 4]
def set_display_from_lines(self): """ Copy values from self.ca_lines to the patches. One issue is dealing with cases in which there are more or fewer lines than Patch row. """ y = 1 maxlin = CA_World.ca_display_size - 1 limy = len(self.ca_lines) + maxlin for i in self.ca_lines: x = 1 if limy >= maxlin: if SimEngine.gui_get('init') == "Right": # Right limx = len(i) + maxlin + 2 for j in range(len(i) - 2): if limx >= maxlin: b = bool(i[j]) self.pixel_tuple_to_patch( ((maxlin - len(i) + 2 + x) * 4, (maxlin - len(self.ca_lines) + y) * 4)).set_on_off(b) x += 1 else: limx -= 1 elif SimEngine.gui_get('init') == "Left": # Left limx = 0 for j in range(len(i) - 2): if limx <= maxlin + 2: b = bool(i[j]) self.pixel_tuple_to_patch( ((x - 3) * 4, (maxlin - len(self.ca_lines) + y) * 4)).set_on_off(b) x += 1 limx += 1 else: # Center and Random limx = int((len(i) - maxlin) / 2) k = 0 for j in range(len(i)): if limx < 0: b = bool(i[j]) self.pixel_tuple_to_patch( ((maxlin - len(i) + x - 1 + limx) * 4, (maxlin - len(self.ca_lines) + y) * 4)).set_on_off(b) else: if k < maxlin + 1: b = bool(i[j + limx]) self.pixel_tuple_to_patch( (k * 4, (maxlin - len(self.ca_lines) + y) * 4)).set_on_off(b) x += 1 k += 1 y += 1 else: limy -= 1
def step(self): dist_unit = SimEngine.gui_get(DIST_UNIT) screen_distance_unit = sqrt(SCREEN_PIXEL_WIDTH()**2 + SCREEN_PIXEL_HEIGHT()**2) / dist_unit if SimEngine.gui_get(LAYOUT) == FORCE_DIRECTED: for node in World.agents: node.adjust_distances(screen_distance_unit, self.velocity_adjustment) self.compute_metrics()
def probabilistic_greedy_route(self): if self.middle_active: if self.middle == 0 or self.top == 0 or self.bottom == 0: return randint(0,2) else: top_different = 2 - self.top if top_different < 0: top_different = 0 top_different = top_different ** (int(SimEngine.gui_get(RANDOMNESS)) / 10) bottom_different = 2 - self.bottom if bottom_different < 0: bottom_different = 0 bottom_different = bottom_different ** (int(SimEngine.gui_get(RANDOMNESS)) / 10) middle_different = 2 - self.middle if middle_different < 0: middle_different = 0 middle_different = middle_different ** (int(SimEngine.gui_get(RANDOMNESS)) / 10) sigma1 = 0 sigma2 = 0 if not (top_different + bottom_different + middle_different) == 0: sigma1 = top_different / (top_different + bottom_different + middle_different) sigma2 = bottom_different / (top_different + bottom_different + middle_different) else: sigma1 = 0.33 sigma2 = 0.33 self.top_prob = sigma1 self.bottom_prob = sigma2 self.middle_prob = 1 - sigma1 - sigma2 split1 = 1000 * sigma1 split2 = 1000 * (sigma1 + sigma2) rand = randint(0, 999) if rand < split1: return 0 else: if rand < split2: return 1 else: return 2 else: if self.top == 0 or self.bottom == 0: return randint(0,1) else: top_different = (2 - self.top) ** (int(SimEngine.gui_get(RANDOMNESS)) / 10) bottom_different = (2 - self.bottom) ** (int(SimEngine.gui_get(RANDOMNESS)) / 10) sigma = top_different / (top_different + bottom_different) top_prob = sigma bottom_prop = 1 - sigma split = 1000 * sigma if randint(0, 999) < split: return 0 else: return 1
def new_route(self): if SimEngine.gui_get(MODE) == BRR: return self.best_random_route() else: if SimEngine.gui_get(MODE) == AR: return self.analytical_route() else: if SimEngine.gui_get(MODE) == PGR: return self.probabilistic_greedy_route() else: return 0
def __init__(self, **kwargs): shape_name = SimEngine.gui_get('shape') color = SimEngine.gui_get('color') color = Color(color) if color != 'Random' else None super().__init__(shape_name=shape_name, color=color, **kwargs) self.forward(randint(50, 300)) # If there are any (other) agents, create links to them with probability 0.25. agents = World.agents - {self} if agents: self.make_links(agents) # Has the node been selected for shortest path? self.highlighted = False
def step(self): World.links = set() show_flockmates = SimEngine.gui_get('Show flockmate links?') # World.agents is the set of all agents. for agent in World.agents: # agent.flock() resets agent's heading. Agent doesn't move. agent.flock(show_flockmates) # Here's where the agent actually moves. # The move depends on the heading, which was just set in agent.flock(), and the speed. speed = SimEngine.gui_get('speed') agent.forward(speed)
def generate_new_line_from_current_line(self, prev_line): """ The argument is (a copy of) the current line. We call it prev_line because that's the role it plays in this method. Generate the new line in these steps. 1. Add '00' or (0, 0) to both ends of prev_line. (We do that because we want to allow the new line to extend the current line on either end. So start with a default extension. In addition, we need a triple to generate the symbols at the end of the new line.) Strings are immutable; string concatenation (+) does not change the original strings. 2. Apply the rules (i.e., the switches) to the triples extracted from the line resulting from step 1. a. Look up the truth value of each triple. Is its switch on or off? b. Convert that boolean first to an int (0 or 1) and then to a character ('0' or '1'). These two steps are done in a single list comprehension. The result is new_line_chars: List[str]. Each element of new_line_chars is a string of one character, either '0' or '1'. c. Use join to combine that list of characters into a new string. This produces a line which is one symbol shorter than the current prev_line on each end. That is, it is one symbol longer on each end than the original current line. It may have 0 or 1 at each end. Args: prev_line: The current state of the CA. Returns: The next state of the CA. """ # Extend the current line two to the left and right. # Want to be able to generate one additional value at each end. if self.lists: prev_line.insert(0, 0) prev_line.insert(0, 0) prev_line.extend([0, 0]) triples = [ ''.join(map(str, prev_line[i:i + 3])) for i in range(len(prev_line) - 2) ] new_line = [int(SimEngine.gui_get(triple)) for triple in triples] else: prev_line = '00' + prev_line + '00' # For each triple of characters in the prev_line, look up the setting of the corresponding switch. # (SimEngine.gui_get(prev_line[i:i + 3])) # Convert its Truth value (rule is on/off) to an int and then to a one character str. new_line_chars = [ str(int(SimEngine.gui_get(prev_line[i:i + 3]))) for i in range(len(prev_line) - 2) ] # Finally, join those strings together into a new string. new_line = ''.join(new_line_chars) return new_line
def mutate(self) -> Individual: chromosome = self.chromosome if randint(0, 100) <= SimEngine.gui_get('replace_gene'): (chromosome, self.fitness, _) = chromosome.replace_gene_in_chromosome(self.fitness) elif randint(0, 100) <= SimEngine.gui_get('reverse_subseq'): chromosome = chromosome.reverse_subseq() self.fitness = self.compute_fitness() self.chromosome: Chromosome = GA_World.chromosome_class(chromosome) return self
def probablisitic_greedy_route(self): middle_on = SimEngine.gui_get("middle_on") randomness = SimEngine.gui_get("randomness") if middle_on: if World.middle == 0 or World.bot == 0 or World.top == 0: return randint(0, 2) t_dif = 2 - World.top if t_dif < 0: t_dif = 0 t_dif = t_dif**(randomness/10) b_dif = 2 - World.bot if b_dif < 0: b_dif = 0 b_dif = b_dif ** (randomness / 10) m_dif = 2 - World.top if m_dif < 0: m_dif = 0 m_dif = m_dif ** (randomness / 10) if not t_dif + b_dif + m_dif == 0: sigma1 = t_dif / (t_dif + b_dif + m_dif) sigma2 = b_dif / (t_dif + b_dif + m_dif) else: sigma1 = 0.33 sigma2 = 0.33 split1 = 1000 * sigma1 split2 = 1000 * (sigma1 + sigma2) rand = random() * 1000 if rand < split1: return 0 else: if rand < split2: return 1 else: return 2 else: if World.top == 0 or World.bot == 0: return randint(0, 1) t_dif = (2-World.top)**(randomness/10) b_dif = (2-World.bot)**(randomness/10) sigma = t_dif / (t_dif + b_dif) split = 1000 * sigma if (random() * 1000) < split: return 0 else: return 1
def check_middle(self): middle_route = 0 for agent in World.agents: if agent.route == 2: middle_route += 1 if SimEngine.gui_get(MIDDLE_ROUTE) != BraessParadoxWorld.middle_prev: if SimEngine.gui_get(MIDDLE_ROUTE): self.draw_middle() BraessParadoxWorld.middle_prev = SimEngine.gui_get(MIDDLE_ROUTE) else: if middle_route == 0: BraessParadoxWorld.middle_prev = SimEngine.gui_get(MIDDLE_ROUTE)