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)
Exemplo n.º 3
0
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 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 check_if_contiguous(district: District):
    """Returns True if district is contiguous. Assumes all units are from the same district"""

    district_units = district.units
    root = next(iter(district_units))

    queue = [root]
    result = set()
    result.add(root)
    while queue:
        local_root = queue.pop(0)
        neighbors = district.get_district_neighbors_of(local_root)
        for unit in neighbors:
            if unit not in result:
                result.add(unit)
                queue.append(unit)

    # print(result)
    # print(district_units)
    return len(result) > 0 and len(result) == len(district_units)
Exemplo n.º 6
0
 def test_check_if_contiguous_True(self):
     district = District.create_with_units({self.unit_1, self.unit_4, self.unit_5, self.unit_6})
     expected = True
     actual = check_if_contiguous(district)
     self.assertEqual(expected, actual)