def rload(self, raw_file_name): """Loads performance testing data from .txt file in raw format (used in test_tsp only).""" with open(os.path.join("test_TSP", "raw_test", raw_file_name)) as raw_file: data = raw_file.read().strip('\n') self.solution = int(data.split('\n')[0]) self.parcels = [] for line in data.split('\n')[1:]: parcel_number, pos_x, pos_y = line.split(' ') self += Parcel(int(parcel_number), Pos(float(pos_x), float(pos_y))) self.position = Pos(float(pos_x), float(pos_y)) # Base overlaps with last point.
def test___add__2(self): """Check adding multiple parcels to the drone.""" self.assertEqual(self.drone0.parcels, []) self.drone0 += [Parcel(0, Pos(10, 10), 5), Parcel(1, Pos(20, 20), 15)] self.assertEqual(len(self.drone0.parcels), 2) self.assertIsInstance(self.drone0.parcels[0], Parcel) self.assertIsInstance(self.drone0.parcels[1], Parcel) self.assertEqual(len(self.drone0.path), 4) self.assertEqual(self.drone0.path_length, 56.568542494923804)
def setUp(self): """Prepare environment for testing.""" self.drone0 = Drone(0, max_capacity=50, max_speed=10) # Empty drone self.drone1 = Drone(1, max_capacity=140, max_speed=8) # Drone with parcels self.parcel1 = Parcel(1, Pos(1, 1), 10) self.parcel2 = Parcel(2, Pos(-20, -1), 5) self.drone1 += self.parcel1 self.drone1 += self.parcel2
def cload(self, coord_file_name): """Loads data from GPS coordinates. The solutions are approximate due to conversion to xy coordinates (used in test_tsp only).""" with open(os.path.join("test_TSP", "coord_test", coord_file_name)) as coord_file: data = coord_file.read().strip('\n') self.solution = int(data.split('\n')[0]) self.parcels = [] th0 = mean(float(line.split(' ')[1]) for line in data.split('\n')[1:]) for line in data.split('\n')[1:]: parcel_number, pos_x, pos_y = self.convert(line, th0) self += Parcel(parcel_number, Pos(pos_x, pos_y)) self.position = Pos(pos_x, pos_y) # Base overlaps with last point.
def setUp(self): """Prepare environment for testing.""" self.city = City(position=Pos(0, 0), wind=(1, 2), metric='simple') self.drone0 = Drone(0, max_capacity=50, max_speed=10) # Empty drone self.drone1 = Drone(1, max_capacity=140, max_speed=8) # Drone with parcels self.parcel1 = Parcel(1, Pos(1, 1), 10) self.parcel2 = Parcel(2, Pos(-20, -1), 5) self.drone1 += self.parcel1 self.drone1 += self.parcel2
def jload(self, json_file_name): """Loads data from json .txt file stored in "test_json" folder.""" # TODO Add full drone spec. with open(os.path.join("json_test", json_file_name)) as json_file: data = json.load(json_file) self.wind = tuple(data['wind']) self.position = Pos(data['position'][0], data['position'][1]) self.parcels = [] self.drones = [] for parcel in data['parcels']: self += Parcel(parcel['number'], Pos(parcel['x'], parcel['y']), parcel['weight']) for drone in data['drones']: self += Drone(drone['number'], drone['max_capacity'], drone['max_speed'])
def __init__(self, number, base=Pos(0, 0), wind=(0, 0), drone_mass=20, max_capacity=inf, max_speed=20, max_fuel=5, base_fuel_consumption=0.001, altitude=50, factor=3, waiting_at_base=50, waiting_at_client=30): self.number = number self.base = base self.position = base self.wind = wind self.drone_mass = drone_mass self.max_capacity = max_capacity self.used_capacity = 0 self.max_speed = max_speed self.max_fuel = max_fuel self.fuel = max_fuel self.base_fuel_consumption = base_fuel_consumption self.parcels = [] self.cargo = [] self.altitude = altitude self.factor = factor self.waiting_at_base = waiting_at_base self.waiting_at_client = waiting_at_client
def test___add__(self): """Check adding a parcel to the drone.""" self.assertEqual(self.drone0.parcels, []) self.drone0 += Parcel(0, Pos(10, 10), 50) self.assertEqual(len(self.drone0.parcels), 1) self.assertIsInstance(self.drone0.parcels[0], Parcel) self.assertEqual(len(self.drone0.path), 3) self.assertEqual(self.drone0.path_length, 28.284271247461902)
def __init__(self, position=Pos(0, 0), wind=(0, 0), metric='full'): self.metric = metric self.scale = None self.solution = None self.position = position self.wind = wind self.drones = [] self.parcels = [] self.total_cost = 0 self.best_total_cost = inf self.total_costs = {'accepted': [], 'attempted': [], 'best': []}
DRONE = { 'amount': (4, 4), 'mass': (30, 30), 'capacity': (25000, 25000), 'speed': (18, 18), 'fuel': (5, 5), 'consumption': (0, 0), 'altitude': (0, 0), 'factor': (3, 3), 'waiting_at_base': (0, 0), 'waiting_at_client': (0, 0) } PARCEL = {'amount': (50, 50), 'position': (-18_000, 18_000), 'weight': (0, 0)} city = City(position=Pos(*CITY['position']), wind=CITY['wind']) for drone_number in range(randint(*DRONE['amount'])): city += Drone(drone_number + 1, base=city.position, wind=CITY['wind'], drone_mass=randint(*DRONE['mass']), max_capacity=randint(*DRONE['capacity']), max_speed=randint(*DRONE['speed']), max_fuel=randint(*DRONE['fuel']), base_fuel_consumption=uniform(*DRONE['consumption']), altitude=randint(*DRONE['altitude']), factor=uniform(*DRONE['factor']), waiting_at_base=randint(*DRONE['waiting_at_base']), waiting_at_client=randint(*DRONE['waiting_at_client']))
"""Testing purposes.""" from random import shuffle from City import City from common import Position as Pos from Drone import Drone from Parcel import Parcel # City creation city = City(position=Pos(0, 0), wind=(1, 2)) city += Drone(1, 24000, 10) parcels = [] n = 1 for i in range(-40, 50, 10): for j in range(-40, 50, 10): if i != 0 or j != 0: parcels.append(Parcel(n, 10, Pos(i, j))) n += 1 shuffle(parcels) city += parcels city.store("grid.txt")