def play1(self, waves=False): pygame.init() self.DISPLAY = pygame.display.set_mode((self.W, self.H), 0) self.DISPLAY.fill(self.BACKGROUND) quit_draw = False for n, cycle in enumerate(self.complex_intervals): if n == 0: self.complex_cycles.append( Cycle(cycle[0], self.W / 2, self.H / 2, cycle[1], cycle[2])) else: self.complex_cycles.append( Cycle(cycle[0], self.complex_cycles[n - 1].x, self.complex_cycles[n - 1].y, cycle[1], cycle[2])) while not quit_draw: self.DISPLAY.fill(self.BACKGROUND) for n, cycle in enumerate(self.complex_cycles): self.draw_circle(cycle) cycle.angle = self.time cycle.update_point() if n < len(self.complex_cycles) - 1: self.complex_cycles[n + 1].x_center = cycle.x self.complex_cycles[n + 1].y_center = cycle.y self.complex_point = [ self.complex_cycles[-1].x, self.complex_cycles[-1].y ] pygame.draw.circle(self.DISPLAY, (255, 0, 0), self.complex_point, 2) self.complex_drawing.append(self.complex_point) # przesuwanie if waves: for n in range(len(self.complex_drawing)): self.complex_drawing[n] = (self.complex_drawing[n][0] + 1, self.complex_drawing[n][1]) self.complex_drawing = [ point for point in self.complex_drawing if point[0] < 1520 ] else: if self.time > 2 * math.pi: self.complex_drawing = [] self.time = 0 if len(self.complex_drawing) > 1: pygame.draw.lines(self.DISPLAY, (255, 255, 255), False, self.complex_drawing) for event in pygame.event.get(): if event.type == pygame.QUIT: quit_draw = True pygame.quit() quit() pygame.display.flip() self.time += self.dt self.CLOCK.tick(self.FPS)
def getCycle(self): query = "SELECT * from cycle" result = Database.connectDB(self, query) if not result: return Cycle(None, None, None, None, None, None, None, None, None, None, None, None, None, None) else: return Cycle(result[0][0], result[0][1], result[0][2], result[0][3], result[0][4], result[0][5], result[0][6], result[0][7], result[0][8], result[0][9], result[0][10], result[0][11], result[0][12], result[0][13])
def construct(self): a = "a" b = "b" c = "c" d = "d" preferences = {a: [b, c, d], b: [c, d, a], c: [d, b, a], d: [a, b, c]} T = PreferenceTable(preferences, center=[-3.5, 0, 0]) C = Cycle([a, b, c, a], [b, c, d, b], center=[3, 0, 0]) self.play(*T.create()) self.play(*T.propose(a, b), *T.propose(b, c), *T.propose(c, d)) self.play(*T.accept_proposal(a, b), *T.accept_proposal(b, c), *T.accept_proposal(c, d)) self.wait() self.play(*C.create()) self.play(*C.accept(0, 1)) self.play(*C.reject(0, 1)) self.wait() # for anim in C.create_from_table(T): # self.play(anim) # self.wait(2) # self.play(*C.cut_first_prefs(T)) # self.wait(2) # self.play(*C.accept_second_prefs(T)) # self.wait(2) # self.play(*C.uncreate()) self.wait(2)
def __loadConfig(self, config): if "numRelays" in config: self.numRelays = config["numRelays"] numRelays = self.numRelays if "cycles" in config: for key, cycleList in config["cycles"].items(): subCycles = [] if "numRelays" in cycleList: numRelays = cycleList["numRelays"] for sub in cycleList["subCycles"]: if "seconds" in sub and "mode" in sub: subCycles.append(SubCycle(sub["seconds"], sub["mode"])) elif "cycleName" in sub: subCycles.append(sub) break self.cycles.append(Cycle(subCycles, numRelays, key)) for cycle in self.cycles: subCycles = [] for subCycle in cycle.subCycles: if type(subCycle) is SubCycle: subCycles.append(subCycle) elif type(subCycle) is dict and "cycleName" in subCycle: newSubCycle = self.getCycle(subCycle["cycleName"]) if type(newSubCycle) is Cycle: subCycles.append(newSubCycle) break cycle.subCycles = subCycles
def construct(self): title = Text("Preference Cycles:") self.play(Create(title)) self.wait(1) self.play(ApplyMethod(title.shift, 3 * UP)) self.wait(1) definition = Tex("A sequence of players $a_1, a_2, \ldots, a_r$", color=WHITE).scale(0.8).shift(1.9 * UP) self.play(Create(definition)) self.wait(1) cycle = Cycle(["$a_1$", "$a_2$", "$a_3$", "$a_4$", "$a_5$", "$a_1$"], ["$b_1$", "$b_2$", "$b_3$", "$b_4$", "$b_5$", "$b_1$"], center=[0,-0.5,0]) self.play(*[Create(a) for a in cycle.A_mobs[:-1]]) definition2 = Tex("with first preferences $b_1, b_2, \ldots, b_r$", color=WHITE).scale(0.8).shift(1.4 * UP) self.play(Create(definition2)) self.wait(1) self.play(*[Create(b) for b in cycle.B_mobs[:-1]]) first_pref_arrows = [arrow for i,arrow in enumerate(cycle.arrows[:-2]) if i % 2 == 0] second_pref_arrows = [arrow for i,arrow in enumerate(cycle.arrows[:-2]) if i % 2 == 1] self.play(*[Create(ar.curr_arrow()) for ar in first_pref_arrows]) self.wait(2) definition3 = Tex("where the second favorite of $a_i$ is $b_{i+1},$", color=WHITE).scale(0.8).shift(2.4 * DOWN) self.play(Create(definition3)) self.wait(1) self.play(*[Create(ar.curr_arrow()) for ar in second_pref_arrows]) self.wait(2) definition4 = Tex("wrapping around for $a_r$ and $a_1$.", color=WHITE).scale(0.8).shift(2.9 * DOWN) self.play(Create(definition4)) self.play(Create(cycle.A_mobs[-1]), Create(cycle.B_mobs[-1])) self.play(Create(cycle.arrows[-2].curr_arrow()), Create(cycle.arrows[-1].curr_arrow())) self.wait(5) self.play(*[Uncreate(d) for d in (definition, definition2, definition3, definition4)]) self.wait(1) elimination = Tex("We \\emph{eliminate} the cycle by having $b_1$ reject $a_1$.", color=WHITE).scale(0.8).shift(1.5 * UP) self.play(Create(elimination)) self.wait(1) self.play(*(cycle.reject(0,0) + cycle.reject(5,5))) self.wait(1) elimination2 = Tex("Each $b_i$ has $a_i$ as its least favorite, so it rejects", color=WHITE).scale(0.8).shift(2.4 * DOWN) self.play(Create(elimination2)) elimination3 = Tex("$a_i$ to match with $a_{i-1}$.", color=WHITE).scale(0.8).shift(2.9 * DOWN) self.play(Create(elimination3)) self.wait(1) for i in range(5): self.play(*cycle.reject(i+1,i+1)) self.play(*cycle.accept(i,i+1)) self.wait(5)
def animate_rotation_elimination(self, p, q): C = Cycle(p + [p[0]], q + [q[0]], center=[3.5, 0, 0], width=0.9 * config['frame_x_radius']) for anim in C.create_from_table(self.T): self.scene.play(anim) self.scene.wait(1) self.scene.play(*C.cut_first_prefs(self.T)) self.scene.wait(1) self.scene.play(*C.accept_second_prefs(self.T)) self.scene.wait(1) self.scene.play(*C.uncreate()) self.scene.wait(1)
def generateCycles(markets, currencyFilter): cycles = [] counter = 0 for index1 in range(len(markets)): markets[index1].print_market() if (markets[index1].get_sourceCurrency() == currencyFilter): for index2 in range(index1 + 1, len(markets)): for i in range(2): for j in range(2): if markets[index1].currencies[ 1 - i] == markets[index2].currencies[j]: for index3 in range(index2 + 1, len(markets)): for k in range(2): if markets[index2].currencies[ 1 - j] == markets[index3].currencies[ k] and markets[ index1].currencies[ i] == markets[ index3].currencies[ 1 - k]: print('match ' + markets[index1].code + ' ' + markets[index2].code + ' ' + markets[index3].code + ' ' + str(i == 0) + ' ' + str(j == 0) + ' ' + str(k == 0)) currCycle = Cycle([ markets[index1], markets[index2], markets[index3] ], [(i == 0), (j == 0), (k == 0)]).rotate( ['ETH', 'BTC', 'USDT']) # if currCycle.leftCurrency(0) == 'ETH' and (currCycle.leftCurrency(1) == 'USDT' or currCycle.leftCurrency(2) == 'USDT'): if 1 == 1: cycles += [ currCycle, currCycle.reverse() ] counter += 1 print('counter %d' % counter) return cycles
# kruskal_tree = Mst(original_graph=G, method='kruskal') # kruskal_tree.plot_graph() # print 'Poids total du graphe : ' + header.__getitem__('NAME') + ' = ' + # str(kruskal_tree.weight) + '\n' # for i in prim_tree.get_nodes(): # print i.get_id() , prim_tree.get_neighbors(i) , ' ------ ' , # i.ancestor.get_id(), '\n\n' nb_nodes = G.get_nb_nodes() graph_min = None # graph_min = Graph() # graph_min.add_weight(float('infinity')) if method is None or method == 'kruskal': cycle1 = Cycle(name=header['NAME'], original_graph=G, method='kruskal') graph_min = cycle1 # print "nombre de noeuds : ", nb_nodes if method is None or method == 'prim': if nb_nodes > 600 and method is None: repetitions = [ randint(0, G.get_nb_nodes() - 1) for i in xrange(50) ] print repetitions else: repetitions = xrange(nb_nodes) for num_node in repetitions: cycle2 = Cycle(name=header['NAME'], original_graph=G, method='prim',
def __init__(self, body, header=None, maxsize=20): self.header = header or [] self.body = body self.maxsize = maxsize self.cycle = Cycle(chop(self.body, maxsize))
def get_visit_order(geo_points): """THIS IS THE ONLY FUNCTION THAT YOU NEED TO MODIFY FOR PHASE 5. The only argument, *geoPoints*, is a list of points that user has marked. Each element of geoPoints is an instance of the GeoPoint class. You need to create your graph using these points. You obtain the distance between two points by calling the *getDistance* function; for example: get_distance(geoPoints[0], geoPoints[1]) Run your tsp solver and return the locations visit order. The return value, *order*, must be a list of indices of points, specifying the visit order. In the example implementation below, we visit each point by the order in which they were marked (clicked). """ # longueur = len(geoPoints) print "first leg length: ", get_distance(geo_points[0], geo_points[1]) # order = range(longueur) # default order # order += [order[0]] # longueur_totale = 0 # for x in xrange(longueur-1): # longueur_totale += get_distance(geoPoints[x], geoPoints[x+1]) # longueur_totale += get_distance(geoPoints[-1],geoPoints[0]) # print "Longueur totale du cycle Google maps : " +str(longueur_totale) # print order graph = Graph(name='Graphe test') for k in xrange(len(geo_points)): coords = [geo_points[k].lat, geo_points[k].lng] test_node = Node(name='Ntest {0:d}'.format(k), data=coords) graph.add_node(test_node) for i in range(graph.get_nb_nodes()): for j in range( i ): # pas de redondance , donc nb_edges = nb_nodes * (nb_nodes-1) /2 start = graph.get_node(i) arrival = graph.get_node(j) # print start , arrival , '\n' e_data = [ start, arrival, get_distance(geo_points[i], geo_points[j]) ] e = Edge(name='Edge' + str(i) + '-' + str(j), data=e_data) graph.add_edge(e) graph.add_to_dict(e) cycle1 = Cycle("Kruskal", original_graph=graph, method='kruskal') graph_min = cycle1 repetitions = xrange(len(geo_points)) for num_node in repetitions: cycle2 = Cycle(name="Prim" + str(num_node), original_graph=graph, method='prim', num_node=num_node) if graph_min is None or cycle2.weight < graph_min.weight: graph_min = cycle2 print "Longueur totale du cycle etudiant : " + str(graph_min.weight) graph_min.plot_graph() return graph_min.ordrerd_list
from cycle import Cycle, Notice upstairs_bathroom = Cycle('Upstairs Bathroom', ['Nick', 'Lawrence', 'Hunter'], 'upstairs_bathroom') downstairs_bathroom = Cycle('Downstairs Bathroom', ['Sam', 'Ashleigh', 'Jess', 'Alex'], 'downstairs_bathroom') kitchen = Cycle( 'Kitchen', ['Alex', 'Jess', 'Hunter', 'Nick', 'Ashleigh', 'Lawrence', 'Sam'], 'kitchen') notices = [] Notice.save_list(notices, "notices") chefs = [ "Jess", "Sam", "Lawrence", "Hunter", "Nick", "No one loves you :(", "Ashleigh" ] Cycle.easy_save(chefs, 'cooking_schedule')
def __wm_screen_init__(self): log.debug("WorkspaceWindowManager.__wm_screen_init__") self.workspaces = Cycle() self.workspaceDict = {}
def construct(self): why_text = Tex("Why we can remove cycles") self.play(Write(why_text)) self.play(ApplyMethod(why_text.shift, UP * 3.5)) lemma1 = r""" \begin{align*} \text{Lemma 1: }&\text{in any stable $M$ in the reduced table,}\\ &a_i\text{ and }b_i \text{ are matched either}\\ &\text{for all }i \text{ or for no }i\end{align*}""" lemma1 = Tex(lemma1).next_to(why_text, DOWN * 2) self.play(Write(lemma1)) def math_list(base, start, end): return [ "$" + base + "_" + str(i) + "$" for i in range(start, end) ] As = math_list("a", 1, 5) + ["$a_1$"] Bs = math_list("b", 1, 5) + ["$b_1$"] c = Cycle(As, Bs, center = DOWN * 1.5) self.play(*c.create()) self.wait(1) say_text = Tex("Let $b_3$ reject $a_3$") \ .next_to(lemma1, DOWN * 2) \ .shift(LEFT * 3) self.play(Write(say_text)) next_text = Tex("$a_3$ proposes to $b_4$") \ .next_to(lemma1, DOWN * 2) \ .shift(RIGHT * 3) self.wait(1) self.play(*c.reject(2, 2)) self.wait(1) self.play(Write(next_text)) self.play(*c.accept(2, 3)) self.play(*c.reject(3, 3)) self.play(*c.accept(3, 4)) self.play(*(c.reject(4, 4) + c.reject(0, 0))) self.play(*c.accept(0, 1)) self.play(*c.reject(1, 1)) self.play(*c.accept(1, 2)) self.play(*(c.uncreate())) self.wait(1) thus_text = Tex(r"""Thus if any $a_i$ is not matched with its $b_i$\\ then no $a_i$ can match with its $b_i$""") \ .next_to(lemma1, DOWN * 6) self.play(Write(thus_text)) self.wait(1) self.play(*[ Uncreate(m) for m in [thus_text, next_text, say_text] ]) shift_amt = 15 let_m = Tex(r""" Let $M$ be a stable matching where each $a_i$ is matched\\ with its $b_i$. Let $M'$ be the same matching, but each\\ $a_i$ is matched with its second choice $b_{i+1}$ """).next_to(why_text, DOWN * 2).shift(RIGHT * shift_amt) self.add(let_m) self.play(*[ ApplyMethod(t.shift, LEFT * shift_amt) for t in [lemma1, let_m] ]) lemma2 = Tex("Lemma 2: $M'$ is stable if $M$ is stable") \ .next_to(let_m, DOWN * 2) self.play(Write(lemma2)) self.wait() As = ["$a_k$", ""] Bs = ["$b_k$", "$b_{k+1}$"] c = Cycle(As, Bs, center = DOWN * 1.5) c.reject(0, 0) c.accept(0, 1) mobjs = c.get_all_mobjs() mobjs.pop(2) self.play(*[ Create(m) for m in mobjs ]) b_better_text = Tex(r"Each $b_i$ is better\\ off in $M'$ than $M$") \ .shift(LEFT * 4 + DOWN) self.play(Write(b_better_text)) a_happy_text = Tex(r"$a_k$ can only prefer \\"+\ r"$b_k$ to its current match\\" +\ r"but $b_k$ is happier with\\" +\ r"$a_{k-1}$, so $M'$ is\\" +\ r"stable here") \ .shift(RIGHT * 4 + DOWN * 2) self.play(Write(a_happy_text)) self.play(*[ Uncreate(m) for m in mobjs + [b_better_text, a_happy_text] ]) continue_m = Tex(r"Thus $M$ stable $\Rightarrow$ $M'$ stable,\\" +\ r"so if there exists a stable matching, we\\" +\ r"can find it by proceeding with $M'$ and\\" +\ r"eliminating our cycle") \ .shift(DOWN * 1.5) self.play(Write(continue_m)) self.wait(2) self.play(*[ ApplyMethod(t.shift, RIGHT * shift_amt) for t in [lemma1, let_m] ] + [ Uncreate(m) for m in [continue_m] ] + [ ApplyMethod(t.shift, DOWN * 0.8) for t in [lemma2] ]) plus = TextMobject("+").shift(UP * 0.5) impl = TextMobject("$\\Leftarrow$") \ .rotate_in_place(PI/2).shift(DOWN) self.play(Create(impl), Create(plus)) self.wait(1) final = Tex(r"We can always eliminate cycles without\\"\ r"changing the result").next_to(impl, DOWN) self.play(Write(final)) self.wait(2)
def play(self): pygame.init() self.DISPLAY = pygame.display.set_mode((self.W, self.H), 0) self.DISPLAY.fill(self.BACKGROUND) quit_draw = False for n, cycle in enumerate(self.intervals_x): if n == 0: self.cycles_x.append( Cycle(cycle[0], 800, 100, cycle[1], cycle[2])) else: self.cycles_x.append( Cycle(cycle[0], self.cycles_x[n - 1].x, self.cycles_x[n - 1].y, cycle[1], cycle[2])) for n, cycle in enumerate(self.intervals_y): if n == 0: self.cycles_y.append( Cycle(cycle[0], 200, 600, cycle[1], cycle[2])) else: self.cycles_y.append( Cycle(cycle[0], self.cycles_y[n - 1].x, self.cycles_y[n - 1].y, cycle[1], cycle[2])) while not quit_draw: self.DISPLAY.fill(self.BACKGROUND) for n, cycle in enumerate(self.cycles_x): self.draw_circle(cycle) cycle.angle += self.time / 60 cycle.update_point() if n < len(self.cycles_x) - 1: self.cycles_x[n + 1].x_center = cycle.x self.cycles_x[n + 1].y_center = cycle.y for n, cycle in enumerate(self.cycles_y): self.draw_circle(cycle) cycle.angle += self.time / 60 cycle.update_point() if n < len(self.cycles_y) - 1: self.cycles_y[n + 1].x_center = cycle.x self.cycles_y[n + 1].y_center = cycle.y self.point_x = [self.cycles_x[-1].x, self.cycles_x[-1].y] self.point_y = [self.cycles_y[-1].x, self.cycles_y[-1].y] pygame.draw.circle(self.DISPLAY, (255, 0, 0), self.point_x, 2) pygame.draw.circle(self.DISPLAY, (255, 0, 0), self.point_y, 2) self.drawing.append([self.point_x[0], self.point_y[1]]) pygame.draw.line(self.DISPLAY, (255, 255, 255), self.point_x, self.drawing[-1]) pygame.draw.line(self.DISPLAY, (255, 255, 255), self.point_y, self.drawing[-1]) if self.time > 2 * math.pi: self.drawing = [] if len(self.drawing) > 1: pygame.draw.lines(self.DISPLAY, (255, 255, 255), False, self.drawing) for event in pygame.event.get(): if event.type == pygame.QUIT: quit_draw = True pygame.quit() quit() pygame.display.flip() self.CLOCK.tick(self.FPS)
def construct(self): text1 = Text("How do we know a preference cycle exists?") self.play(Write(text1)) self.wait(1) self.play(ApplyMethod(text1.shift, 3 * UP)) self.wait(2) As = [a1, a2, a3, a4] = ["$a_1$", "$a_2$", "$a_3$", "$a_4$"] Bs = [b1, b2, b3, b4] = ["$b_1$", "$b_2$", "$b_3$", "$b_5$"] C = Cycle(As, Bs, center=[-0.5, 0, 0]) A_mobs = C.A_mobs B_mobs = C.B_mobs self.play(Create(A_mobs[0])) self.play(Create(B_mobs[0]), Create(B_mobs[1]), Create(C.arrows[0].curr_arrow()), Create(C.arrows[1].curr_arrow())) self.wait(2) text2 = Tex( "$b_2$ must hold a proposal from someone other than $a_1$").shift( 2 * DOWN) text3 = Tex( "That player will prefer $b_2$ the most by construction").next_to( text2, DOWN) self.play(Write(text2)) self.wait(2) self.play(Write(text3)) self.wait(2) self.play(Create(C.arrows[2].curr_arrow())) self.play(Create(A_mobs[1])) self.play(Uncreate(text2), Uncreate(text3)) self.wait(2) text3 = Tex("$a_2$ must also hold a proposal from someone").shift(2 * DOWN) text4 = Tex("If $a_2$ holds a proposal from $b_2$ then").next_to( text3, DOWN) text5 = Tex("$b_2$ and $a_2$ would already be matched").next_to( text4, DOWN) text6 = Tex("$a_1$ would not be on $b_2$'s preference list").next_to( text4, DOWN) text7 = Tex("$b_2$ would not be on $a_1$'s preference list").next_to( text4, DOWN) self.play(Write(text3)) self.wait(2) self.play(Write(text4)) self.wait(2) self.play(Write(text5), Indicate(A_mobs[1]), Indicate(B_mobs[1])) self.wait(2) self.play(ReplacementTransform(text5, text6), Indicate(A_mobs[0])) self.wait(2) self.play(ReplacementTransform(text6, text7)) self.wait(2) self.play(Create(C.arrows[3].curr_arrow())) self.play(Create(B_mobs[2])) self.wait(2) self.play(Uncreate(text3), Uncreate(text4), Uncreate(text7)) text8 = Tex( "Repeating this will eventually lead us to cycling \\\\ since there are only a finite number of players" ).shift(2 * DOWN) dots = Tex("$\cdots$").next_to(C.arrows[6].curr_arrow(), RIGHT) self.play(Write(text8), Create(A_mobs[2]), Create(B_mobs[3]), Create(A_mobs[3]), Create(C.arrows[4].curr_arrow()), Create(C.arrows[5].curr_arrow()), Create(C.arrows[6].curr_arrow()), Write(dots)) self.wait(2)