def setUp(self): unit_1 = Unit(1, 100, 0.5, 0.5, []) unit_2 = Unit(2, 120, 0.6, 0.4, []) unit_3 = Unit(3, 140, 0.35, 0.65, []) unit_4 = Unit(4, 90, 0.55, 0.45, []) unit_5 = Unit(5, 50, 0.4, 0.6, []) self.dist_1 = District.create_with_units({unit_1, unit_3}) self.dist_2 = District.create_with_units({unit_2, unit_4, unit_5}) self.districts = [self.dist_1, self.dist_2]
def setUp(self): self.unit_1 = Unit(1, 100, 0.5, 0.5, [2, 3, 4, 5], True) self.unit_2 = Unit(2, 120, 0.6, 0.4, [1], True) self.unit_3 = Unit(3, 140, 0.35, 0.65, [1, 4, 5], False) self.unit_4 = Unit(4, 90, 0.55, 0.45, [1, 3, 5], True) self.unit_5 = Unit(5, 50, 0.4, 0.6, [1, 3, 4], True) self.units = [self.unit_1, self.unit_2, self.unit_3, self.unit_4, self.unit_5] self.units_on_border = [self.unit_1, self.unit_2, self.unit_4, self.unit_5] Units.initialize(self.units) district_units_1 = {self.unit_1, self.unit_2, self.unit_3} district_units_2 = {self.unit_4, self.unit_5} self.district_1 = District.create_with_units(district_units_1) self.district_2 = District.create_with_units(district_units_2)
def show_fitness_score(sln_file_path, data_file): with open(sln_file_path) as f: result = json.load(f)["result"] with open(data_file) as f: units = json.load(f)["units"] units_lst = [] for json_unit in units: units_lst.append( Unit(json_unit["id"], json_unit["pop"], json_unit["registration"]["rep"], json_unit["registration"]["dem"], json_unit["neighbors"], json_unit["is_on_region_border"])) units_lst.sort(key=lambda x: x.id) districts = [ District.create_with_units( {units_lst[unit_id - 1] for unit_id in dst["units_ids"]}) for dst in result ] region = RegionMap() region._districts = tuple(districts) print("solution quality: {0}".format( fitness.calc_solution_quality(region)))
def DoProcreate(self): from entities import Unit #do a step in the procreate action #There is a lot to add here, baby, child, care for baby, baby needs to be brought food, child can move around but do very little #pos, home stockpile, in same building, born hungry so you'd better have food new_entity = Unit(self.female.pos, self.female.stockpile, self.female.In_Building, hunger = 30) self.female.In_Building.unit_inventory.append(new_entity) self.DeleteAction() print '{0} {1} has been born'.format(type(new_entity).__name__, new_entity.name) return new_entity
def setUp(self): self.unit_1 = Unit(1, 100, 0.6, 0.4, [2, 4], True) self.unit_2 = Unit(2, 100, 0.4, 0.6, [1, 3, 5], True) self.unit_3 = Unit(3, 100, 0.7, 0.3, [2, 6], True) self.unit_4 = Unit(4, 100, 0.5, 0.5, [1, 5, 7], True) self.unit_5 = Unit(5, 100, 0.5, 0.5, [2, 4, 6, 8], False) self.unit_6 = Unit(6, 100, 0.3, 0.7, [3, 5, 9], True) self.unit_7 = Unit(7, 100, 0.3, 0.7, [4, 8], True) self.unit_8 = Unit(8, 100, 0.8, 0.2, [5, 7, 9], True) self.unit_9 = Unit(9, 100, 0.4, 0.6, [6, 8], True) self.units = [self.unit_1, self.unit_2, self.unit_3, self.unit_4, self.unit_5, self.unit_6, self.unit_7, self.unit_8, self.unit_9] Units.initialize(self.units) self.dist_1 = District.create_with_units({self.units[0], self.units[1], self.units[3], self.units[2], self.units[6]}) self.dist_2 = District.create_with_units({self.units[4]}) self.dist_3 = District.create_with_units({self.units[5], self.units[7], self.units[8]}) self.dists = [self.dist_1, self.dist_2, self.dist_3]
def setUp(self): self.unit_1 = Unit(1, 100, 0.5, 0.5, [2, 4], True) self.unit_2 = Unit(2, 120, 0.6, 0.4, [1, 3, 5], True) self.unit_3 = Unit(3, 140, 0.35, 0.65, [2, 6], True) self.unit_4 = Unit(4, 90, 0.55, 0.45, [1, 5, 7], True) self.unit_5 = Unit(5, 50, 0.4, 0.6, [2, 4, 6, 8], False) self.unit_6 = Unit(6, 50, 0.4, 0.6, [3, 5, 9], True) self.unit_7 = Unit(7, 50, 0.4, 0.6, [4, 8], True) self.unit_8 = Unit(8, 50, 0.4, 0.6, [5, 7, 9], True) self.unit_9 = Unit(9, 50, 0.4, 0.6, [6, 8], True) self.units = [self.unit_1, self.unit_2, self.unit_3, self.unit_4, self.unit_5, self.unit_6, self.unit_7, self.unit_8, self.unit_9] Units.initialize(self.units)
def test_unit_conversion(): g = Unit("g") kg = Unit("kg", {g: lambda k: k * 1000.}) assert g.to(g)(100) == 100 assert kg.to(g)(1) == 1000 assert Amount(1, kg) + Amount(1000, g) == Amount(2000, g)
class U: r = Unit("r") s = Unit("s") t = Unit("t")
assert (a + b) + c == a + (b + c) assert reduce(add, (a, b, a, b), Amount.zero) == Amount(30, U.r) def ingredient(name, amount, unit): return growser.Ingredient(name, Amount(amount, unit)) x = ingredient("x", 1, U.r) y = ingredient("y", 1, U.s) z = ingredient("z", 1, U.t) a = growser.IngredientList([x, y]) b = growser.IngredientList([x, z]) c = growser.IngredientList([y, z, Ingredient("q", Amount(1, Unit("#")))]) def test_ingredient_lists_behaves_as_a_monoid(): assert a == a assert a + a.zero == a assert a.zero + a == a assert (a + b) + c == a + (b + c) assert a + b == growser.IngredientList( [Ingredient("x", Amount(2, U.r)), y, z]) def test_ingredient_lists_can_be_scaled(): s = 2.0 * a