def __init__(self, state, n_districts, bins=16, n_seeds=200, **kwargs): self.bins = bins super().__init__(state, n_districts, **kwargs) seeds = [ districts.make_random(state, n_districts) for _ in range(n_seeds) ] self.archive = self._features(seeds)
def __init__(self, state, n_districts, n_seeds=200, use_MCA=False, use_binary_features=True, binary_n=2, n_components=10, **kwargs): self.use_MCA = use_MCA self.use_binary_features = use_binary_features self.binary_n = binary_n # How many pairs to use, multiplier of the number of tiles. if use_MCA: try: from prince import MCA except ImportError: print('price not imported. Run "pip install prince"') exit() self.dim_reduction = MCA(n_components=n_components) else: try: from sklearn.decomposition import PCA except ImportError: print('sklearn not imported. Run "pip install sklearn"') exit() self.dim_reduction = PCA(n_components=n_components) seeds = [ districts.make_random(state, n_districts) for _ in range(n_seeds) ] if use_binary_features: n = state.n_tiles * self.binary_n self.binary_idxs_a = np.random.randint(0, high=state.n_tiles - 1, size=n) self.binary_idxs_b = np.random.randint(0, high=state.n_tiles - 1, size=n) seeds = [self._makeBinaryFeature(f) for f in seeds] self.dim_reduction.fit(np.array(seeds)) self.archive = np.array(self.dim_reduction.transform(seeds)).tolist() super().__init__(state, n_districts, **kwargs)
def feasible_seeds(state, config, max_iters=400): seeds = [] n_failures = 0 allowed_failures = 2 * config.pop_size with tqdm(total=config.pop_size) as pbar: while len(seeds) < config.pop_size: try: rand_dist = districts.make_random(state, config.n_districts) if not config.dont_fix_seeds and config.equality_constraint > 0: fix_pop_equality(state, rand_dist, config.n_districts, tolerance=config.equality_constraint, max_iters=max_iters) seeds.append(rand_dist) pbar.update(1) except Exception as e: n_failures += 1 if n_failures == allowed_failures: raise ValueError('Too many failures in fix_seeds') return seeds
from src import districts, mutation, metrics from src.connectivity import can_lose from src.constraints import fix_pop_equality from src.draw import draw_districts # state = State.fromFile('data/t500-c3.json') state = State.makeRandom(400, seed=1) for _ in range(1): state, _ = state.contract(seed=0) # met = metrics.compactness_convex_hull met = metrics.polsby_popper mutate = False n_districts = 5 districts = districts.make_random(state, n_districts) # districts = np.random.randint(0, n_districts, (state.n_tiles,), dtype='i') tolerance = 0.5 draw_kwargs = { "draw_bounding_hulls": False, "draw_bounding_circles": False, "draw_district_edges": True, "draw_vertices": False, "draw_neigbors_lines": False } # print(fix_pop_equality(state, districts, n_districts, tolerance=tolerance, max_iters=1000)) pygame.init() w, h = (1200, 1200) screen = pygame.display.set_mode((w, h)) screen.fill((255, 255, 255))
if len(sys.argv) == 1: ### Subdivide voronoi grid map. ### n_districts = 10 n_divisions = 2 mappings = [ None ] screen = pygame.display.set_mode(( 2000, 500 )) screen.fill(( 255, 255, 255 )) for _ in range(n_divisions): state, mapping = states[-1].contract(seed=seed) states.append(state) mappings.append(mapping) dists = districts.make_random(states[-1], n_districts, seed=seed) colors = np.random.randint(0, 255, (n_districts, 3)) for div_i, (state, mapping) in enumerate(zip(states[::-1], mappings[::-1])): dx = div_i * 500 draw_districts( state, dists, n_districts, screen, colors, dx=dx, draw_district_edges=True, draw_vertices=False, draw_neigbors_lines=False ) if mapping is not None: dists = upscale(dists[np.newaxis], mapping)[0] else: ### Subdivide real state data. ###