Last active
April 13, 2016 18:38
-
-
Save Fs02/48246440201c2764a67beb496ced6b73 to your computer and use it in GitHub Desktop.
Emergency Facility Location Problem
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| 3 | 6 | 4 | 3 | 3 | 1 | 6 | 6 | 9 | 2 | 1 | 10 | 10 | 10 | 7 | 2 | 3 | 7 | 6 | 2 | 8 | 0 | 7 | 3 | 5 | 9 | |
|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|
| 6 | 1 | 0 | 7 | 3 | 3 | 4 | 0 | 10 | 3 | 6 | 4 | 10 | 9 | 9 | 1 | 2 | 8 | 2 | 6 | 10 | 0 | 2 | 3 | 0 | 1 | |
| 0 | 9 | 5 | 6 | 0 | 6 | 8 | 4 | 9 | 4 | 2 | 4 | 10 | 2 | 1 | 3 | 5 | 7 | 7 | 9 | 5 | 2 | 6 | 8 | 3 | 5 | |
| 0 | 7 | 2 | 4 | 2 | 2 | 10 | 1 | 0 | 2 | 9 | 2 | 9 | 5 | 10 | 7 | 4 | 10 | 1 | 10 | 3 | 7 | 10 | 9 | 2 | 0 | |
| 2 | 7 | 7 | 10 | 7 | 1 | 6 | 5 | 0 | 7 | 4 | 9 | 4 | 6 | 7 | 4 | 8 | 6 | 1 | 3 | 10 | 6 | 9 | 4 | 4 | 3 | |
| 9 | 8 | 2 | 2 | 7 | 9 | 3 | 2 | 8 | 5 | 10 | 2 | 8 | 7 | 2 | 5 | 7 | 9 | 2 | 10 | 0 | 8 | 9 | 9 | 10 | 8 | |
| 8 | 1 | 4 | 9 | 3 | 4 | 10 | 6 | 3 | 6 | 6 | 7 | 9 | 5 | 4 | 8 | 7 | 4 | 5 | 5 | 10 | 2 | 2 | 7 | 2 | 4 | |
| 3 | 0 | 6 | 1 | 4 | 10 | 7 | 0 | 1 | 7 | 10 | 4 | 0 | 5 | 2 | 0 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6 | 2 | 2 | 9 | 3 | 10 | 4 | 2 | 9 | 1 | 7 | 8 | 7 | 5 | 3 | 7 | 8 | 0 | 6 | 4 | 6 | 0 | |
| 2 | 4 | 4 | 8 | 7 | 9 | 0 | 7 | 2 | 0 | 1 | 9 | 2 | 9 | 4 | 5 | 9 | 1 | 6 | 5 | 7 | 3 | 4 | 5 | 2 | 5 | |
| 0 | 2 | 6 | 10 | 9 | 1 | 8 | 0 | 8 | 3 | 7 | 1 | 3 | 3 | 3 | 2 | 6 | 0 | 4 | 6 | 7 | 0 | 2 | 10 | 3 | 3 | |
| 8 | 6 | 2 | 0 | 2 | 8 | 9 | 0 | 4 | 8 | 5 | 5 | 5 | 4 | 9 | 10 | 1 | 2 | 8 | 4 | 9 | 10 | 0 | 10 | 7 | 5 | |
| 0 | 2 | 0 | 1 | 0 | 3 | 0 | 5 | 4 | 8 | 10 | 5 | 8 | 5 | 6 | 7 | 5 | 9 | 0 | 2 | 3 | 9 | 9 | 8 | 2 | 2 | |
| 2 | 3 | 2 | 9 | 7 | 6 | 4 | 4 | 5 | 0 | 10 | 4 | 2 | 5 | 0 | 1 | 4 | 9 | 4 | 10 | 2 | 6 | 4 | 9 | 4 | 5 | |
| 2 | 2 | 6 | 6 | 0 | 3 | 2 | 1 | 10 | 8 | 10 | 1 | 9 | 5 | 2 | 10 | 9 | 2 | 0 | 9 | 1 | 2 | 10 | 5 | 0 | 8 | |
| 1 | 6 | 5 | 2 | 6 | 6 | 7 | 9 | 4 | 2 | 3 | 8 | 7 | 3 | 7 | 8 | 5 | 6 | 7 | 0 | 4 | 3 | 10 | 4 | 8 | 1 | |
| 7 | 6 | 4 | 2 | 7 | 10 | 6 | 5 | 8 | 10 | 10 | 10 | 5 | 8 | 2 | 9 | 1 | 2 | 4 | 1 | 5 | 6 | 1 | 10 | 1 | 4 |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| import numpy as np | |
| import math | |
| import visual | |
| max_iter = 1000 | |
| pop_size = 100 | |
| facility = 1 | |
| c1 = 2 | |
| c2 = 2 | |
| class Particle: | |
| pass | |
| def facility_coverage(position, world): | |
| fitness = 0.0 | |
| for x in xrange(world.shape[0]): | |
| for y in xrange(world.shape[1]): | |
| distance = 999999999999.0 | |
| for f in range(position.shape[0]/2): | |
| distance = min(distance, np.abs(x - position[f * 2 + 0]) + np.abs(y - position[f * 2 + 1])) | |
| demand = world[x, y] | |
| fitness += distance * demand | |
| return 1/fitness | |
| world = np.loadtxt(open("map.csv","rb"),delimiter=",",skiprows=0) | |
| v = visual.Visual(world) | |
| # Initialize the particles | |
| particles = [] | |
| for i in range(pop_size): | |
| p = Particle() | |
| # each facility have 2 axis (x, y) coordinates | |
| p.position = np.random.random_integers(0, world.shape[0], 2 * facility) | |
| p.velocity = 0.0 | |
| p.fitness = 0.0 | |
| particles.append(p) | |
| gbest = particles[0] | |
| i = 0 | |
| while i < max_iter: | |
| for p in particles: | |
| fitness = facility_coverage(p.position, world) | |
| if fitness > p.fitness: | |
| p.fitness = fitness | |
| p.best = p.position | |
| if fitness > gbest.fitness: | |
| gbest = p | |
| velocity = p.velocity + c1 * np.random.rand() * (p.best - p.position) \ | |
| + c2 * np.random.rand() * (gbest.position - p.position) | |
| p.position = p.position + velocity | |
| i += 1 | |
| stats = "Iterations : " + str(i) + "\nFitness : " + str(gbest.fitness) | |
| v.update(particles, stats) | |
| if i % (max_iter/10) == 0: | |
| print str(gbest.position) + " => " + str(gbest.fitness) | |
| print '\nParticle Swarm Optimisation\n' | |
| print 'Population size : ', pop_size | |
| print 'c1 : ', c1 | |
| print 'c2 : ', c2 | |
| print 'facility : ', facility | |
| print 'RESULTS\n', '-'*7 | |
| print 'gbest fitness : ', gbest.fitness | |
| print 'gbest params : ', gbest.position | |
| print 'iterations : ', i | |
| raw_input("Press Enter to continue...") |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| import sfml as sf | |
| class Visual: | |
| def __init__(self, world): | |
| self.window = sf.RenderWindow(sf.VideoMode(1024, 600), "Emergency Facility Location Problem") | |
| self.facilities = [] | |
| self.world = [] | |
| font = sf.Font.from_file("font.ttf") | |
| for x in xrange(world.shape[0]): | |
| for y in xrange(world.shape[1]): | |
| demand = sf.Text(str(world[x, y]), font) | |
| demand.character_size = 10 | |
| demand.color = sf.Color.BLUE | |
| demand.position = (x * 20, y * 20) | |
| area = sf.RectangleShape() | |
| area.size = (20, 20) | |
| area.fill_color = sf.Color(0, 0, 0, world[x, y]/10*255) | |
| area.position = (x * 20, y * 20) | |
| self.world.append((area, demand)) | |
| self.stats = sf.Text( "Iterations : 0000000000000000000 \nFitness : 0000000000000000000", font) | |
| self.stats.character_size = 14 | |
| self.stats.color = sf.Color.MAGENTA | |
| self.stats.position = (self.window.size.x - self.stats.local_bounds.width, 0) | |
| def __del__(self): | |
| self.window.close() | |
| def update(self, particles, stats): | |
| self.window.clear(sf.Color.WHITE) | |
| # Render map | |
| for area in self.world: | |
| self.window.draw(area[0]) | |
| self.window.draw(area[1]) | |
| count = 0 | |
| for p in particles: | |
| for f in range(p.position.shape[0]/2): | |
| if count >= len(self.facilities): | |
| self.facilities.append(sf.CircleShape()) | |
| self.facilities[count].radius = 3 | |
| self.facilities[count].fill_color = sf.Color.RED | |
| self.facilities[count].position = (10 + p.position[f * 2 + 0] * 20, 10 + p.position[f * 2 + 1] * 20) | |
| self.window.draw(self.facilities[count]) | |
| self.stats.string = stats | |
| self.window.draw(self.stats) | |
| self.window.display() |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment