def park(_, registration_number, color): car = Car(registration_number, color) car.set_driver(Driver()) try: parking.get_manager().park(car) print("Allocated slot number: %d" % parking.get_manager().get_slot_of(car).get_serial_number()) except ParkingFullError: print("Sorry, parking lot is full")
def test_modificar_cotxe_import_total(self): p = Viatgers(4) past = p.get_num_viatgers() c1 = Car("COTXE1", "CITROEN", "LLOC1", past, 25.0) c2 = Car("COTXE2", "SEAT", "LLOC2", past, 19.0) cotxes = [c1] c = Cars(past, cotxes) c.modificar_cotxe(c1, c2) self.assertEqual(c.calcul_import_total_cotxe(), 76.0)
def default_car_list(): """ Fixture to create a list of Car objects from default values. """ c0 = Car(DEFAULT_CAR_CODE_0, DEFAULT_CAR_BRAND, DEFAULT_CAR_PICK_UP_PLACE, DEFAULT_CAR_DAYS_RESERVED) c1 = Car(DEFAULT_CAR_CODE_1, DEFAULT_CAR_BRAND, DEFAULT_CAR_PICK_UP_PLACE, DEFAULT_CAR_DAYS_RESERVED) return [c0, c1]
def test_car_driver(self): # Given car = Car("KA-01-HH-2701", "Red") driver = Driver() # When car.set_driver(driver) # Then self.assertEqual(car.get_driver(), driver)
def test_afegir_cotxe_import_total(self): p = Viatgers(4) past = p.get_num_viatgers() c1 = Car("COTXE1", "CITROEN", "LLOC1", past, 25.0) c2 = Car("COTXE2", "SEAT", "LLOC2", past, 19.0) c3 = Car("COTXE3", "MERCEDES-BENZ", "LLOC3", past, 32.0) cotxes = [c1, c2] c = Cars(past, cotxes) c.afegir_cotxe(c3) self.assertEqual(c.calcul_import_total_cotxe(), 304.0)
def default_new_car(): """ Fixture to create a Car object from a default values. """ return Car(DEFAULT_NEW_CAR_CODE, DEFAULT_CAR_BRAND, DEFAULT_CAR_PICK_UP_PLACE, DEFAULT_CAR_DAYS_RESERVED)
def test_park_vehicle(self): # Given car = Car("KA-01-HH-2701", "Red") parking_slot = CarParkingSlot(10) # When parking_slot.park(car) # Then self.assertEqual(parking_slot.get_parked_vehicle(), car)
class TestCar(unittest.TestCase): def setUp(self): self.temp = Car() def test_needs_fuel_true(self): # prepare mock self.temp.needsFuel = Mock(name='needs_fuel') self.temp.needsFuel.return_value = True # testing self.assertEqual(self.temp.needsFuel(), True, 'return value from needsFuel incorrect') def test_needs_fuel_false(self): # prepare mock self.temp.needsFuel = Mock(name='needs_fuel') self.temp.needsFuel.return_value = False # testing self.assertEqual(self.temp.needsFuel(), False, 'return value from needsFuel incorrect') def test_get_engine_temperature(self): self.temp.getEngineTemperature = Mock(name='get_engine_temperature') self.temp.getEngineTemperature.return_value = 30 self.assertEqual(self.temp.getEngineTemperature(), 30, 'return value from getEngineTemperature incorrect') @patch.object(Car, 'driveTo') def test_drive_to(self, mock_method): destination = 'Gdynia' mock_method.return_value = 'Driving to ' + destination self.assertEqual(self.temp.driveTo(destination), mock_method.return_value, 'return value from driveTo incorrect') def tearDown(self): self.temp = None
def test_comprovacio_tipus_tarjeta_incorrecte(self): p = Viatgers(4) past = p.get_num_viatgers() f = Vol("1234ABC", "MILAN", past, 10.0) f2 = Vol("5467DEF", "PARIS", past, 20.0) Vtg = [f, f2] v = Flights(past, Vtg) c1 = Car("COTXE1", "FERRARI", "MILAN", past, 30.0) cot = [c1] c = Cars(past, cot) h1 = Hotel("HOTEL1", "MILANO", past, 4, 20.0) h2 = Hotel("HOTEL2", "LE PARIS", past, 6, 25.0) hot = [h1, h2] h = Hotels(hot) p2 = PaymentData("PAYPAL", "Samu", "123456789", "ABC", v, c, h) self.assertEqual(p2.comprobar_targeta(), False)
def test_calcula_preu(self): p = Viatgers(4) past = p.get_num_viatgers() f = Vol("1234ABC", "MILAN", past, 10.0) f2 = Vol("5467DEF", "PARIS", past, 20.0) Vtg = [f, f2] v = Flights(past, Vtg) c1 = Car("COTXE1", "FERRARI", "MILAN", 4, 30.0) cot = [c1] c = Cars(past, cot) h1 = Hotel("HOTEL1", "MILANO", past, 4, 20.0) h2 = Hotel("HOTEL2", "LE PARIS", past, 6, 25.0) hot = [h1, h2] h = Hotels(hot) p = PaymentData("Visa", "Ruben", "1234", "1342", v, c, h) self.assertEqual(p.get_import_total(), 1160.0)
def test_car_brake(speed_brake): car = Car(50) car.brake() assert car.speed == speed_brake
def setUp(self): self.temp = Car()
def test_car(self): car = Car("KA-01-HH-2701", "Red") self.assertTrue(isinstance(car, Vehicle)) self.assertEqual(car._registration_number, "KA-01-HH-2701") self.assertEqual(car._color, "Red")
def run(self): current_dir = os.path.dirname(os.path.abspath(__file__)) voitures = [] voitures_sauve = [] max_fitness = 0 generation = 1 nbr_voit=[10, 20, 30] # initialisation circuit circuit = pygame.image.load("track.png").convert_alpha() track_mask = pygame.mask.from_surface(circuit) # initialisation generation voitures nbr = 0 for i in range(nbr_voit[nbr]): voitures.append(Car(0, 0)) car = voitures[0] while not self.exit: dt = self.clock.get_time() / 1000 # Event queue for event in pygame.event.get(): if event.type == pygame.QUIT: self.exit = True pressed = pygame.key.get_pressed() if pressed[pygame.K_UP]: if self.updates < 1000: self.updates += 1 if pressed[pygame.K_DOWN]: if self.updates > 1: self.updates -= 1 last_pos = (car.position.x, car.position.y) for i in range(self.updates): if car.fitness > 7000: car.save_neural() voitures_sauve = [] nbr += 1 generation = 1 max_fitness = 0 for _ in range(nbr_voit[nbr]): voitures.append(Car(0, 0)) car = voitures[0] if len(voitures) == 0: if generation > 250: print("gen") voitures_sauve = [] nbr += 1 generation = 1 max_fitness = 0 for _ in range(nbr_voit[nbr]): voitures.append(Car(0, 0)) else: car.savegen(generation, max_fitness, nbr) generation += 1 voitures = src.GA.nouvelle_gen(voitures_sauve) voitures_sauve = [] voitures.append(Car(0, 0)) voitures.append(Car(0, 0)) car = voitures[0] # Logic car.update(dt, pressed) # Drawing self.screen.fill((0, 0, 0)) self.screen.blit(circuit, (-500, -300) - car.car_position()) car.display(self.screen) if car.fitness > max_fitness: max_fitness = car.fitness car.fitness += math.sqrt((car.position.x - last_pos[0])**2 + (car.position.y - last_pos[1])**2) last_pos = (car.position.x, car.position.y) # Collision detection offset = (-(int(-500 - car.position.x * 32 - 640 + car.rect.width/2)), -(int(-300 - car.position.y * 32 - 360 + car.rect.height/2))) result = track_mask.overlap(car.mask, offset) if result or car.velocity.x < 0: voitures_sauve.append(voitures.pop(0)) text = [self.clock.get_fps(), "Generation :" + str(generation), "Genome:" + str(len(voitures_sauve)+1), car.angle, car.velocity, car.fitness, "Max fit:" + str(max_fitness), self.updates] for y in range(len(text)): self.screen.blit(self.font.render(str(text[y]), True, (0, 255, 0)), (10, 10 + (10 * y))) pygame.display.flip() self.clock.tick(self.ticks) pygame.quit()
def test_car_accelerate(speed_accelerate): car = Car(50) car.accelerate() assert car.speed >= speed_accelerate
rows, columns, numCars, numRides, timelyBonus, timeSteps, rides = importer.importDataSet( ) print("""Metadata: \t{} rows \t{} columns \t{} cars \t{} rides \t{} starting on time bonus \t{} time steps""".format(rows, columns, numCars, numRides, timelyBonus, timeSteps)) rides.sort(key=lambda ride: (ride.startTime, ride.distance()), reverse=False) cars: List[Car] = [Car() for _ in range(numCars)] score: int = 0 i: int = 0 finishedCars: List[int] = [] for frame in range(timeSteps): progress: int = int(100 * (frame + 1) / int(timeSteps)) sys.stdout.write( "\r{:d}% frame {:d} of {:d}. {:d} rides remaining.".format( progress, frame, (timeSteps - 1), (len(rides) - i))) sys.stdout.flush() for index, car in enumerate(cars): if index in finishedCars or not car.isAvailable(frame): # Nothing to do if the car is in transit or finished continue
def run(self): current_dir = os.path.dirname(os.path.abspath(__file__)) # initialisation circuit circuit = pygame.image.load("track.png").convert_alpha() track_mask = pygame.mask.from_surface(circuit) given_brain = ReseauNeurone(3, 6, 2) given_brain.E_C_matrice = [ [-8.97733451e-02, 3.88300394e-01, -4.33225819e-01], [-5.60947796e-01, -3.44441383e-01, 2.26400017e-01], [-4.05719496e-01, -8.51709163e-01, 7.65198411e-01], [-2.52344199e-01, 2.92995343e-01, 3.26502978e-04], [-2.92018717e-01, -5.59915332e-01, 4.38344407e-01], [4.77456562e-01, 7.88640334e-01, -4.51291827e-01] ] given_brain.C_S_matrice = [[ 0.35782455, 0.09804306, 0.04327138, -0.41764635, -0.41508099, 0.53154817 ], [ 0.51327193, 0.33521452, 0.36453928, -0.43719057, 0.7806659, 0.31162904 ]] given_brain.E_C_biais = [ -0.11689981, -0.14345427, 0.45680658, 0.1483759, 0.10124425, -0.1142477 ] given_brain.C_S_biais = [0.02099482, 0.80842289] car = Car(0, 0) car.brain.E_C_biais = [ -0.11689981, -0.14345427, 0.45680658, 0.1483759, 0.10124425, -0.1142477 ] car.brain.C_S_biais = [0.02099482, 0.80842289] times = 0 error = 1000 rate = 1 while not self.exit: dt = self.clock.get_time() / 1000 # Event queue for event in pygame.event.get(): if event.type == pygame.QUIT: self.exit = True pressed = pygame.key.get_pressed() if pressed[pygame.K_UP]: if self.updates < 1000: self.updates += 1 if pressed[pygame.K_DOWN]: if self.updates > 1: self.updates -= 1 max_fitness = 0 last_pos = (car.position.x, car.position.y) for i in range(self.updates): if car.fitness > 7000: print("Done") # Logic errorx = car.update(dt, given_brain, rate) # Drawing self.screen.fill((0, 0, 0)) self.screen.blit(circuit, (-500, -300) - car.car_position()) car.display(self.screen) if car.fitness > max_fitness: max_fitness = car.fitness car.fitness += math.sqrt((car.position.x - last_pos[0])**2 + (car.position.y - last_pos[1])**2) last_pos = (car.position.x, car.position.y) # Collision detection offset = (-(int(-500 - car.position.x * 32 - 640 + car.rect.width / 2)), -(int(-300 - car.position.y * 32 - 360 + car.rect.height / 2))) result = track_mask.overlap(car.mask, offset) if result or car.velocity.x < 0: temp_error = math.sqrt(np.dot(errorx, errorx)) temp_matrice = [ car.brain.E_C_matrice, car.brain.C_S_matrice ] if temp_error <= error: error = temp_error rate *= 1.05 else: car.brain.E_C_matrice = temp_matrice[0] car.brain.C_S_matrice = temp_matrice[1] rate *= 0.95 times += 1 f = open("savefile.txt", "a") f.write(str(times) + " " + str(error)) f.write("\n") f.close car.reset() text = [ self.clock.get_fps(), car.angle, car.velocity, self.updates ] for y in range(len(text)): self.screen.blit( self.font.render(str(text[y]), True, (0, 255, 0)), (10, 10 + (10 * y))) pygame.display.flip() self.clock.tick(self.ticks) pygame.quit()