Ejemplo n.º 1
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 generate_init_solution(k: int) -> RegionMap:
    """Randomly generates an initial solution (district) that contains k districts"""

    seeds = set()
    # gets a tuple of units neighboring unit 0
    units_on_border = Units.get_units_on_region_border()

    while len(seeds) < k:
        unit = np.random.choice(units_on_border, 1)[0]
        if unit not in seeds:
            seeds.add(unit)

    region = RegionMap.create_district_for_each_unit(tuple(seeds))
    pool = seeds
    while pool:
        unit = pool.pop()
        district_of_unit = region.find_district_by_unit(unit)
        neighbors = Units.get_neighbors_of(unit)

        for neighbor in neighbors:
            neighbor_district = region.find_district_by_unit(neighbor)

            if neighbor_district is None and neighbor not in pool:
                pool.add(neighbor)
                district_of_unit.add_unit(neighbor)
    return region
def generate_init_solution_new_seeding(k: int) -> RegionMap:
    seeds = set()
    # gets a tuple of units neighboring unit 0

    units = Units.units()

    while len(seeds) < k:
        unit = np.random.choice(units, 1)[0]
        if unit not in seeds:
            seeds.add(unit)

    region = RegionMap.create_district_for_each_unit(tuple(seeds))
    pool = seeds
    while pool:
        unit = pool.pop()
        district_of_unit = region.find_district_by_unit(unit)
        neighbors = Units.get_neighbors_of(unit)

        for neighbor in neighbors:
            neighbor_district = region.find_district_by_unit(neighbor)

            if neighbor_district is None and neighbor not in pool:
                pool.add(neighbor)
                district_of_unit.add_unit(neighbor)

    return region
Ejemplo n.º 4
0
 def test_check_if_has_holes_True(self):
     initial_units = (self.unit_1, self.unit_5)
     region = RegionMap.create_district_for_each_unit(initial_units)
     district_1 = region.find_district_by_unit(self.unit_1)
     district_1.add_units([self.unit_2, self.unit_3, self.unit_4, self.unit_6, self.unit_7,
                           self.unit_8, self.unit_9])
     expected = True
     actual = check_if_has_holes(region)
     self.assertEqual(expected, actual)
def check_if_has_holes(sln: RegionMap):
    districts = sln.districts

    for dist in districts:
        neighboring_districts = sln.get_neighboring_districts_of(dist)
        if len(neighboring_districts) > 1:
            continue

        units_on_region_border = dist.get_district_units_on_region_border()
        if len(units_on_region_border) == 0:
            return True

    return False
Ejemplo n.º 6
0
def create_neighboring_sln(cur_sln: RegionMap):
    """Generates a neighboring solution using the Ising models approach"""

    # if unit borders several districts, choose one of these districts randomly
    # decide randomly if unit should "go" to another district
    all_units = Units.units()
    for cur_unit in all_units:
        cur_district = cur_sln.find_district_by_unit(cur_unit)

        # if cur_unit is the only unit in the district, do not change cur_unit "membership"
        # (in favor of another district)
        if len(cur_district) <= 1:
            continue

        neighboring_districts_collection = cur_sln.get_neighboring_districts_of(cur_district)

        # find those districts that cur_unit borders
        districts_that_cur_unit_borders = []
        for neighboring_district in neighboring_districts_collection:
            bordering_units = cur_district.get_units_bordering_that_district(neighboring_district)

            if cur_unit in bordering_units:
                districts_that_cur_unit_borders.append(neighboring_district)

        if len(districts_that_cur_unit_borders) == 0:
            continue

        random_district = rd.choice(districts_that_cur_unit_borders)

        cur_district.remove_unit(cur_unit)
        random_district.add_unit(cur_unit)

        # revert the move if it breaks contiguity or if the move creates holes
        if not check_if_contiguous(cur_district) or check_if_has_holes(cur_sln):
            random_district.remove_unit(cur_unit)
            cur_district.add_unit(cur_unit)

    return cur_sln
Ejemplo n.º 7
0
    def take_sln_snapshot(self, sln: RegionMap, unfitness_score: float, fitness_score: float, sln_quality: float,
                          current_energy: float, current_temp: float):
        solution_json = sln.get_as_json_str()

        csv_row = [self.iteration,
                   CumulativeTime.get_cumtime(),
                   sln_quality,
                   unfitness_score,
                   fitness_score,
                   current_energy,
                   current_temp,
                   solution_json]
        write_to_csv_file(OUTPUT_FILE, csv_row)
        self.iteration += 1