Skip to content

Commit

Permalink
Mutation workin
Browse files Browse the repository at this point in the history
  • Loading branch information
William Farmer committed Oct 3, 2016
1 parent 8f8c259 commit 92768db
Show file tree
Hide file tree
Showing 3 changed files with 41 additions and 8 deletions.
Binary file added hw5/out1.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file added hw5/out2.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
49 changes: 41 additions & 8 deletions hw5/willfarmer_hw5.py
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@
import re
import numpy as np
import matplotlib.pyplot as plt
import seaborn
import queue


Expand All @@ -24,14 +25,9 @@ def simulated_annealing(system, numdistricts):
def genetic_algorithm(system, numdistricts):
initial = Solution(system, numdistricts)
initial.generate_random_solution()

fig, axarr = plt.subplots(1, 2, figsize=(8, 8))
axarr[0].imshow(system.matrix, interpolation='nearest')
axarr[1].imshow(initial.full_mask, interpolation='nearest', alpha=0.5)
axarr[1].set_title('Value: {}'.format(initial.value))
axarr[0].axis('off')
axarr[1].axis('off')
plt.show()
initial.show(save=True, name='out1.png')
initial.mutate()
initial.show(save=True, name='out2.png')


class Solution(object):
Expand All @@ -53,6 +49,18 @@ def __getitem__(self, key):
def __str__(self):
return str(self.full_mask)

def show(self, save=False, name='out.png'):
fig, axarr = plt.subplots(1, 2, figsize=(8, 8))
axarr[0].imshow(self.system.matrix, interpolation='nearest')
axarr[1].imshow(self.full_mask, interpolation='nearest')
axarr[1].set_title('Value: {}'.format(self.value))
axarr[0].axis('off')
axarr[1].axis('off')
if save:
plt.savefig(name)
else:
plt.show()

@property
def is_valid(self):
"""
Expand Down Expand Up @@ -135,6 +143,27 @@ def generate_random_solution(self):
def mutate(self):
i = np.random.randint(1, self.numdistricts)
y, x = self.get_openspots(i)
traversed = set()
q = queue.Queue()
q.put((y, x))
while not q.empty():
y, x = q.get()
traversed.add((y, x))

if (self.full_mask[y, x] != i and
self[self.full_mask[y, x]].size > 1):
self.full_mask[y, x] = i
break

neighbors = [(y + yi, x + xi)
for xi in range(-1, 2)
for yi in range(-1, 2)
if (0 <= y + yi < self.system.height) and
(0 <= x + xi < self.system.width) and
not (x == 0 and y == 0) and
(y + yi, x + xi) not in traversed]
for ii, jj in neighbors:
q.put((ii, jj))


class System(object):
Expand Down Expand Up @@ -201,6 +230,10 @@ def parse_list(self, listvals):
self.mask = np.array(listvals)
self.height, self.width = self.mask.shape

@property
def size(self):
return self.mask.sum()

@property
def is_valid(self):
"""
Expand Down

0 comments on commit 92768db

Please sign in to comment.