def test_pline_append_lines(self): line_1 = objects.Line() line_2 = objects.Line() polyline = objects.Polyline() polyline.append(line_1, line_2) self.assertEqual(polyline.elements[line_1.handle], line_1) self.assertEqual(polyline.elements[line_2.handle], line_2)
def test_line_ne_false(self): line = objects.Line(objects.Point(), objects.Point(2010789, 483925, 3918.63517)) line_equal = objects.Line(objects.Point(), objects.Point(2010789, 483925, 3918.63517)) is_not_equal = line != line_equal self.assertEqual(is_not_equal, False)
def random1(data, num_of_lines=None, max_duration=None): """ creates a random solution with two constrains: n tracks with a max duration of n minutes and all the data.stations need to be connected. :argument data: a class with all important static information about run, such as max_duration :argument num_of_lines maximum number of lines that can be ridden :argument max_duration maximum duration that lines can be :returns a solution with the score and the generated lines. """ if not num_of_lines: num_of_lines = data.num_of_lines if not max_duration: max_duration = data.max_duration solution = obj.Solution(data) for i in range(num_of_lines): start = random.choice(list(data.stations)) a = obj.Line([data.stations[start]]) while a.total_time < max_duration: connections = a.stations[-1].connections track = data.tracks[random.choice(list(connections))] a.add_station_by_track(track) a.remove_last_station() solution.add_line(a) # create random line return solution
def test_line_ne_true(self): line = objects.Line(objects.Point(), objects.Point(2010789, 483925, 3918.63517)) line_differant_start = objects.Line(objects.Point(1,0,0), objects.Point(2010789, 483925, 3918.63517)) line_differant_end = objects.Line(objects.Point(), objects.Point(2010789, 483924, 3918.63517)) #line_differant_second_point = objects.Line(objects.Point(), # objects.Point(2010789, 483924, 3918.63517), # objects.Point(1,1,0)) line_all_bad = objects.Line(objects.Point(0,1,0), objects.Point(0,2,0)) #Will have to test for second point. self.assertNotEqual(line, line_differant_start) self.assertNotEqual(line, line_differant_end) #self.assertNotEqual(line, line_differant_second_point) self.assertNotEqual(line, line_all_bad)
def test_line_init_from_points(self): start_point = objects.Point() end_point = objects.Point(2010789, 483925, 3918.63517) middle_point = objects.Point(1, 1, 1) line = objects.Line(start_point, end_point, middle_point) self.assertEqual(line.start.handle, start_point.handle) self.assertEqual(line.end.handle, end_point.handle) self.assertEqual(line.middle.handle, middle_point.handle)
def random2(data, num_of_lines=None, max_duration=None): """ Creates random lines with a maximum duration with a extra constraint: trains can't go back to their previous station :param data: the data object containing information about the environment :param num_of_lines: the amount of lines to be created :param max_duration: the maximum duration of a single lines :return: a set of lines """ if not num_of_lines: num_of_lines = data.num_of_lines if not max_duration: max_duration = data.max_duration solution = obj.Solution(data) for i in range(num_of_lines): used_tracks = {} # choose a random start position start = random.choice(list(data.stations)) a = obj.Line([data.stations[start]]) while a.total_time < max_duration: station = a.stations[-1] # create copy of dict to edit it without editing the original destinations = {**station.connections} while True: # choose a random destination destination = random.choice(list(destinations)) # add track to list track = data.tracks[destination] key = track.key if key not in used_tracks: # track to the destination isnt already used: ready to go! used_tracks.update({key: 0}) break elif len(destinations) == 1: # track is already used but no other possibility break else: # track is already used: try again del (destinations[destination]) a.add_station_by_track(track) # total time is over max: remove the last one to fulfill the constraints a.remove_last_station() solution.add_line(a) # create random line return solution
def test_line_from_two_points(self): start_point = objects.Point() end_point = objects.Point(2010789, 483925, 3918.63517) line = objects.Line(start_point, end_point) self.assertEqual(line.start.handle, start_point.handle) self.assertEqual(line.end.handle, end_point.handle) self.assertEqual(line.middle.northing, 2010789 / 2) self.assertEqual(line.middle.easting, 483925 / 2) self.assertEqual(line.middle.elevation, 3918.63517 / 2)
def test_line_eq_false(self): line = objects.Line(objects.Point(), objects.Point(2010789, 483925, 3918.63517)) line_differant_start = objects.Line(objects.Point(1,0,0), objects.Point(2010789, 483925, 3918.63517)) line_differant_end = objects.Line(objects.Point(), objects.Point(2010789, 483924, 3918.63517)) #line_differant_second_point = objects.Line(objects.Point(), # objects.Point(2010789, 483924, 3918.63517), # objects.Point(1,1,0)) line_all_bad = objects.Line(objects.Point(0,1,0), objects.Point(0,2,0)) #Will have to test for second point. is_differant_start = line == line_differant_start is_differant_end = line == line_differant_end #is_differant_second_point = line == line_differant_second_point is_all_bad = line == line_all_bad self.assertEqual(is_differant_start, False) self.assertEqual(is_differant_end, False) #self.assertEqual(is_differant_second_point, False) self.assertEqual(is_all_bad, False)
def greedy_search(station, data, lookup_table): """ Searches for the best route from given station. It does this be choosing the connection with the highest given score, which are located in the lookup table. :argument station: station to start route from :argument data: overall information :argument lookup_table: a dict holding within it each track and its corresponding score :returns a line containing its track, station and duration """ ends = [station] new_line = obj.Line([station]) line_completed = False while not line_completed: new_connections = {} # find both end of route and collect all connections for end in ends: new_connections.update({**end.connections}) best_connection = helper.select_best_scoring_connection( new_connections, lookup_table) # check if adding connection would be allowed in constraint while data.invalid_function(new_line, best_connection, data): del new_connections[best_connection.key] if len(new_connections) == 0: line_completed = True break else: best_connection = helper.select_best_scoring_connection( new_connections, lookup_table) if line_completed: continue elif best_connection.key in new_line.stations[0].connections: new_line.add_station_by_track(best_connection, "first") else: new_line.add_station_by_track(best_connection, "last") ends = [new_line.stations[0], new_line.stations[-1]] return trim_line(new_line, data, lookup_table)
def addline(self, a, b, name=""): if name == "": name = getfreename_line(self.lines) self.lines.append(objects.Line(a, b, name, self.board_w, self.board_h))
def on_ok_button_clicked(self, widget): print("clicked") object_name = self.builder.get_object("entry_name").get_text() print(object_name) if len(self.main_window.available_id) == 0: self.main_window.object_id += 1 object_id = self.main_window.object_id elif len(self.main_window.available_id) != 0: object_id = self.main_window.available_id[0] self.main_window.available_id.pop(0) if object_name == "": object_name = "object"+str(object_id) print(object_id) current_page = self.builder.get_object("notebook_object").get_current_page() color = self.builder.get_object("color_button") rgba = color.get_rgba() object_rgb = [] object_rgb.append(rgba.red) object_rgb.append(rgba.green) object_rgb.append(rgba.blue) is_solid = self.builder.get_object("button_solid") # if current page = 0, add point if current_page == 0: x = float(self.builder.get_object("entry_x_point").get_text()) y = float(self.builder.get_object("entry_y_point").get_text()) new_point = objects.Point(x, y, object_id, object_name, "Point", object_rgb) new_point.rotate_scn(-self.main_window.window.theta, self.main_window.window.window_center.x, self.main_window.window.window_center.y) self.main_window.object_list.append([new_point.object_id, new_point.object_name, new_point.object_type]) self.main_window.append_log("Object " + new_point.object_name + " (" + new_point.object_type + ") created") self.main_window.saved_objects.append(new_point) if current_page == 1: x1 = float(self.builder.get_object("entry_x1_line").get_text()) x2 = float(self.builder.get_object("entry_x2_line").get_text()) y1 = float(self.builder.get_object("entry_y1_line").get_text()) y2 = float(self.builder.get_object("entry_y2_line").get_text()) new_line = objects.Line(objects.LinePoint(x1, y1), objects.LinePoint(x2, y2), object_id, object_name, "Line", object_rgb) new_line.rotate_scn(-self.main_window.window.theta, self.main_window.window.window_center.x, self.main_window.window.window_center.y) self.main_window.object_list.append([new_line.object_id, new_line.object_name, new_line.object_type]) self.main_window.append_log("Object " + new_line.object_name + " (" + new_line.object_type + ") created") self.main_window.saved_objects.append(new_line) if current_page == 2: new_list = [] for obj in self.wireframe_points: new_list.append(obj) new_wireframe = objects.Wireframe(new_list, object_id, object_name, "Wireframe", object_rgb, is_solid.get_active()) new_wireframe.rotate_scn(-self.main_window.window.theta, self.main_window.window.window_center.x, self.main_window.window.window_center.y) self.main_window.object_list.append([new_wireframe.object_id, new_wireframe.object_name, new_wireframe.object_type]) self.main_window.append_log("Object " + new_wireframe.object_name + " (" + new_wireframe.object_type + ") created") self.main_window.saved_objects.append(new_wireframe) self.wireframe_points.clear() if current_page == 3: x1 = float(self.builder.get_object("x1_bezier_entry").get_text()) x2 = float(self.builder.get_object("x2_bezier_entry").get_text()) x3 = float(self.builder.get_object("x3_bezier_entry").get_text()) x4 = float(self.builder.get_object("x4_bezier_entry").get_text()) y1 = float(self.builder.get_object("y1_bezier_entry").get_text()) y2 = float(self.builder.get_object("y2_bezier_entry").get_text()) y3 = float(self.builder.get_object("y3_bezier_entry").get_text()) y4 = float(self.builder.get_object("y4_bezier_entry").get_text()) points = [] points.append(objects.LinePoint(x1, y1)) points.append(objects.LinePoint(x2, y2)) points.append(objects.LinePoint(x3, y3)) points.append(objects.LinePoint(x4, y4)) new_curve = objects.Curve(points, object_id, object_name, "Curve", object_rgb, "bezier") new_curve.rotate_scn(-self.main_window.window.theta, self.main_window.window.window_center.x, self.main_window.window.window_center.y) self.main_window.object_list.append([new_curve.object_id, new_curve.object_name, new_curve.object_type]) self.main_window.append_log("Object " + new_curve.object_name + " (Bezier " + new_curve.object_type + ") created") self.main_window.saved_objects.append(new_curve) if current_page == 4: if len(self.bspline_points) >= 4: new_list = [] for obj in self.bspline_points: new_list.append(obj) new_bspline = objects.Curve(new_list, object_id, object_name, "Curve", object_rgb, "b-spline") new_bspline.rotate_scn(-self.main_window.window.theta, self.main_window.window.window_center.x, self.main_window.window.window_center.y) self.main_window.object_list.append([new_bspline.object_id, new_bspline.object_name, new_bspline.object_type]) self.main_window.append_log("Object " + new_bspline.object_name + " (B-Spline " + new_bspline.object_type + ") created") self.main_window.saved_objects.append(new_bspline) else: self.errordialog = self.builder.get_object("bspline_error") self.errordialog.run() self.errordialog.destroy() self.main_window.append_log("Insufficient points to generate a b-spline curve") self.main_window.append_log("Try again with at least 4 points") self.wireframe_points.clear() self.bspline_points.clear() cr = self.darea.get_window().cairo_create() self.darea.draw(cr) self.object_window.destroy()
def test_line_init_as_empty(self): line = objects.Line() self.assertEqual(str(line.start), "0.0, 0.0, 0.0") self.assertEqual(str(line.end), "0.0, 0.0, 0.0") self.assertEqual(str(line.middle), "0.0, 0.0, 0.0")
def test_pline_append_bad_objects(self): bad_object = "Nuaghty Zoot" good_object = objects.Line() polyline = objects.Polyline() with self.assertRaises(AttributeError): polyline.append(bad_object, good_object)
def test_pline_init_with_objects(self): line_1 = objects.Line() line_2 = objects.Line() polyline = objects.Polyline(line_1, line_2) self.assertEqual(polyline.elements[line_1.handle], line_1) self.assertEqual(polyline.elements[line_2.handle], line_2)
def test_line_get_2d_radius(self): line = objects.Line(objects.Point(), objects.Point(2010789, 483925, 3918.63517))
def test_line_get_length(self): line = objects.Line(objects.Point(), objects.Point(2010789, 483925, 3918.63517)) self.assertEqual(line.get_length(), 2068204.8167064101)