forked from EmilienDupont/facilityLocation
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathindex.html
756 lines (641 loc) · 24.8 KB
/
index.html
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
<!DOCTYPE html>
<meta charset="utf-8">
<script type="text/javascript" src="http://cdn.mathjax.org/mathjax/latest/MathJax.js?config=TeX-AMS-MML_HTMLorMML">
</script>
<script>
MathJax.Hub.Config({
tex2jax: {inlineMath: [['$', '$'], ['\\(','\\)']]},
TeX: { equationNumbers: {autoNumber: "AMS"} },
"HTML-CSS": { showMathMenu: false,
scale: 90 }
});
</script>
<link rel="stylesheet" href="http://examples.gurobi.com/base.css">
<style>
a:hover.screenshot {
opacity: .7;
}
.site {
fill: white;
stroke: black;
stroke-width: 1;
}
.client {
fill: black;
opacity: .5;
}
.c0 { fill: blue; stroke: #000099 }
.c1 { fill: green; stroke: #006600 }
.c2 { fill: red; stroke: #990000}
.c3 { fill: yellow; stroke: #999900 }
.c4 { fill: gray; stroke: #404040 }
.v0 { stroke: blue; }
.v1 { stroke: green; }
.v2 { stroke: red; }
.v3 { stroke: yellow; }
.v4 { stroke: gray; }
.place,
.place-label {
fill: black;
opacity: 1;
stroke: none;
}
</style>
<body>
<ul id="nav">
<li class="current"><a href="#intro">Intro</a></li>
<li><a href="#problem">Problem</a></li>
<li><a href="#model">Model</a></li>
<li><a href="#implementation">Implementation</a></li>
<li><a href="#demo">Live Demo</a></li>
<li><a href="#try">Try Gurobi for Free</a></li>
</ul>
<div id="example_container">
<div class="example_section" id="intro">
<h1>Facility Location</h1>
<subtitle>with integer programming and Gurobi</subtitle>
</div>
<p>
In this example we'll solve a simple facility location problem:
where to build warehouses to supply a large number of supermarkets.
</p>
<p>
We'll construct a mathematical model of the business problem,
implement this model in Gurobi's Python interface, and compute and
visualize an optimal solution.
</p>
<p>
Although your own business may not involve supermarkets, the
same basic techniques used in this example can be used for many
other applications in supply chain, logistics and transportation.
</p>
<h3>
Click the screenshot to skip directly to the Live Demo!
</h3>
<p>
<a href="#demo" class="screenshot">
<img src="screenshot.png" alt="Live Demo" style="width: 100%; vertical-align: middle;">
</a>
</p>
<div class="example_section" id="problem">
<h2><a href="#problem" name="problem">Problem Description</a></h2>
<div style="float:right;">
<a href="https://en.wikipedia.org/wiki/Logistics">
<img src="warehouse.jpg" alt="Warehouse" style="width:200px;">
</a>
</div>
<p>
A large supermarket chain in the UK needs to build warehouses
for a set of supermarkets it is opening in Northern England.
The locations of the supermarkets have been decided, but the
locations of the warehouses are yet to be chosen.
</p>
<p>
Several good candidate locations for the warehouses have been
determined, but it remains to decide how many warehouses to
open and at which candidate locations to build them.
</p>
<aside> A typical warehouse. <span style="font-size: 10px">Source:
<a href="https://en.wikipedia.org/wiki/Logistics#/media/File:Modern_warehouse_with_pallet_rack_storage_system.jpg">Axisadman</a> - <a href="https://creativecommons.org/licenses/by-sa/3.0/deed.en">CC BY-SA 3.0</a></span>
</aside>
<p>
Opening many warehouses would be advantageous as this would
reduce the average distance a truck has to drive from
warehouse to supermarket, and hence reduce the delivery cost.
However, opening a warehouse is costly.
</p>
<p>
We will use Gurobi to find the optimal tradeoff between delivery
cost and the cost of building new facilities.
</p>
</div>
<div class="example_section" id="model">
<h2><a href="#model" name="model">Mathematical Model</a></h2>
<p>
Our example is an instance of the
<a href="http://www.sciencedirect.com/science/article/pii/S0377221703008191">
Uncapacitated Facility Location Problem</a>. There are many
different types of facility location problems. For more details,
see the book <i><a href="http://www.springer.com/us/book/9783540421726">
Facility Location: Applications and Theory</a></i>.
</p>
<p>
Let us now formulate a mathematical model for our problem.
Let $I$ be the set of supermarket (or <em>customer</em>) locations.
Let $J$ be the set of candidate warehouse (or <em>facility</em>)
locations. The goal is to choose which locations in $J$ should
be used to construct a facility. Therefore, for each location we
define a binary variable
\[
x_j = \left\{\begin{array}{ll}
1 & \text{if we locate facility at candidate site $j \in J$, }\\
0 & \mathrm{otherwise.}
\end{array}\right.
\]
</p>
<p>
There is a cost associated with constructing each warehouse. We
denote this fixed charge by $f_j$.
</p>
<p>We also define continuous variables $y_{ij}$ to be the
fraction of supply received by customer $i$ from facility $j$.
These quantities are positive, so we have the constraints:
\[
y_{ij} \ge 0, \quad \forall i \in I, j \in J.
\]
</p>
<p>
We denote by $c_{ij}$ the cost of shipping between candidate
warehouse site $j$ and supermarket location $i$. This
cost is usually proportional to the distance $d_{ij}$ between
the facility and the customer:
\[
c_{ij} = \alpha d_{ij}
\]
The constant $\alpha$ is the cost per mile of driving, adjusted
to incorporate the average number of a trips a delivery truck
would be expected to make over a 5 year period.
</p>
<p>
We wish to minimize the total cost to open and operate the
facilites. This is the sum of the cost of opening facilities
and the cost related to shipping between facilities and
customers:
\[
\text{total cost} = \sum_{j \in J} f_j x_j + \sum_{j \in J} \sum_{i \in I} c_{ij} y_{ij}.
\]
This total cost measures the tradeoff between the cost of building
a new warehouse and the total delivery cost over a 5 year period.
</p>
<p>
Finally, we need to add two constraints. First, the demand for each
customer must be fulfilled. That is, the sum of the fraction received
from each facility for each customer must be equal to 1:
\[
\sum_{j \in J} y_{ij} = 1, \quad \forall i \in I.
\]
Second, we can only ship from facility $j$ if that facility has
actually been built. So we have the following constraints:
\[
y_{ij} \leq x_{ij}, \quad \forall i \in I \quad \forall j \in J.
\]
</p>
<p>
Thus, the uncapacitated facility location problem is defined
by the following model in the variables $x_j$ and $y_{ij}$:
\[
\begin{array}{ll}
\text{minimize} & {\displaystyle \sum_{j \in J} f_j x_j + \sum_{j \in J} \sum_{i \in I} c_{ij} y_{ij}} \\
\text{subject to} & {\displaystyle \sum_{j \in J} y_{ij}} = 1, \quad \forall i \in I, \\
& y_{ij} \leq x_{ij}, \quad \forall i \in I, \forall j \in J, \\
& y_{ij} \geq 0, \quad \forall i \in I, \forall j \in J, \\
& x_j \in \{ 0, 1 \}, \quad \forall j \in J.
\end{array}
\]
</p>
</div>
<div class="example_section" id="implementation">
<h2><a href="#implementation" name="implementation">Implementation</a></h2>
<p>Below is the full implementation of the model (and the associated data) in
Gurobi's Python interface:
</p>
<examplecode>
from gurobipy import *
import math
def distance(a,b):
dx = a[0] - b[0]
dy = a[1] - b[1]
return math.sqrt(dx*dx + dy*dy)
# Problem data
clients = [[0, 1.5],[2.5, 1.2]]
facilities = [[0,0],[0,1],[0,1],
[1,0],[1,1],[1,2],
[2,0],[2,1],[2,2]]
charge = [3,2,3,1,3,3,4,3,2]
numFacilities = len(facilities)
numClients = len(clients)
m = Model()
# Add variables
x = {}
y = {}
d = {} # Distance matrix (not a variable)
alpha = 1
for j in range(numFacilities):
x[j] = m.addVar(vtype=GRB.BINARY, name="x%d" % j)
for i in range(numClients):
for j in range(numFacilities):
y[(i,j)] = m.addVar(lb=0, vtype=GRB.CONTINUOUS, name="t%d,%d" % (i,j))
d[(i,j)] = distance(clients[i], facilities[j])
m.update()
# Add constraints
for i in range(numClients):
for j in range(numFacilities):
m.addConstr(y[(i,j)] <= x[j])
for i in range(numClients):
m.addConstr(quicksum(y[(i,j)] for j in range(numFacilities)) == 1)
m.setObjective( quicksum(charge[j]*x[j] + quicksum(alpha*d[(i,j)]*y[(i,j)]
for i in range(numClients)) for j in range(numFacilities)) )
m.optimize()
</examplecode>
</div>
<div class="example_section" id="demo">
<h2><a href="#demo" name="demo">Live Demo</a></h2>
<p>
Below is a visualization of our example. We are using the
location data from <a href="#geolytix">GeoLytix</a> for a large
supermarket chain in the UK, and visualizing its outlets in
Northern England.
</p>
<p>
The supermarkets are represented by:
<svg height="20" width="20">
<circle cx="10" cy="15" r="3" fill="black" opacity=".5" />
</svg>
</p>
<p>
By clicking the map you can add <em>potential</em> warehouse locations.
These are represented by:
<svg height="20" width="20">
<circle cx="10" cy="10" r="8" stroke="black" stroke-width="1" fill="white" />
</svg>
</p>
<p>
Click "Compute Warehouse Locations" to find the locations where warehouses
<em>will</em> be built using Gurobi. These will be represented by:
<svg height="20" width="100">
<circle cx="10" cy="10" r="8" stroke="#000099" stroke-width="3" fill="blue" />
<circle cx="30" cy="10" r="8" stroke="#006600" stroke-width="3" fill="green" />
<circle cx="50" cy="10" r="8" stroke="#990000" stroke-width="3" fill="red" />
<circle cx="70" cy="10" r="8" stroke="#999900" stroke-width="3" fill="yellow" />
<circle cx="90" cy="10" r="8" stroke="#404040" stroke-width="3" fill="gray" />
</svg>
</p>
<p>
A few potential warehouse locations have already been set up, but
you can add more by clicking the screen.
</p>
<p>
The cost of transportation is 3 pounds/mile. You can use the slider to
vary the cost of building a warehouse. When the cost is low, many
facilities will be built. When the cost is high, it will dominate the
transportation cost and there will be fewer facilities with greater
driving distances.
</p>
Cost to build warehouse:
<p>
<input type="range" min = 500000 max = 3000000 step = 500000 id="cost" value="1500000" oninput="outputUpdate(value)" class="slider-width">
<output for=cost id=costDisplay>1.5</output> million pounds
<script>
function outputUpdate(value) {
document.querySelector('#costDisplay').value = value/1000000;
}
</script>
</p>
<div id="demoarea">
</div>
<div id="spinnerdiv">
</div>
<button class="pure-button" onclick="compute()">Compute Warehouse Locations</button>
<button onclick="restart()">Restart</button>
<p>
<button class="pure-button" onclick="toggle_div()">Gurobi Log</button>
</p>
<script src="https://cdnjs.cloudflare.com/ajax/libs/d3/3.5.5/d3.min.js"></script>
<script>
function toggle_div() {
var logfile = d3.select('#logfile');
if (logfile.style("display") === "none") {
logfile.style("display", "block");
} else {
logfile.style("display", "none");
}
}
</script>
<examplecode id=logfile>
</examplecode>
<p style="font-size:8px">
<a name="geolytix">Location data:</a>
<a href="http://geolytix.co.uk/downloads/OpenSupermarkets.zip">Supermarket
locations © GeoLytix</a> copyright and database right 2014.
</p>
</div>
<div class="example_section" id="try">
<h2><a href="#try" name="try">Try Gurobi for Free</a></h2>
<p> We hope this example has taught you a bit about the
facility location problem and using Gurobi. We encourage you
to try the example out for yourself with Gurobi. To enable
this, we provide easy access to a full-featured evaluation
version of Gurobi.
</p>
<div class="col_5 column">
<a href="http://www.gurobi.com/downloads/evaluation-request">
<button class="red stack-button">
<i class="fa fa-lg fa-line-chart"></i>
Commercial Users: Free Evaluation Version
</button>
</a>
</div>
<div class="col_5 column">
<a href="http://www.gurobi.com/downloads/download-center">
<button class="red stack-button">
<i class="fa fa-lg fa-line-chart"></i>
Academic Users: Free Academic Version
</button>
</a>
</div>
<p>
We are always happy to discuss your needs and answer your questions.
Just <a href="http://www.gurobi.com/company/contact-us">contact us</a>
at your convenience.
</p>
</div>
<div style="min-height:100px"></div>
<!--[if gt IE 8]><!--><script src="//ajax.googleapis.com/ajax/libs/jquery/2.1.0/jquery.min.js"></script><!--<![endif]-->
<script src="jquery.nav.js"></script>
<script>
$(document).ready(function() {
console.log('calling onePageNav');
$('#nav').onePageNav({scrollOffset:120});
});
</script>
<script src="https://cdnjs.cloudflare.com/ajax/libs/d3/3.5.5/d3.min.js"></script>
<script src="http://d3js.org/topojson.v1.min.js"></script>
<script src="https://cdnjs.cloudflare.com/ajax/libs/spin.js/1.2.7/spin.min.js"></script>
<script>
// Hide Log File intially
d3.select('#logfile').style("display", "none");
//Width and height
var width = 800;
var height = 500;
var padding = 30;
var miles_per_px = 0.19;
var price_per_px = 3*miles_per_px;
var alpha = 5*365*price_per_px; // Consider the cost over 5 years
var spinner;
var svg = d3.select("#demoarea")
.append("svg")
.attr("width", width)
.attr("height", height);
// Potential sites for facilities
var sites = [[.3*width, .8*height], [.4*width, .3*height], [516.40625, 380.34375],
[486.40625, 55.34375], [766.40625, 179.34375], [728.40625, 439.34375]];
var cost = parseFloat(document.getElementById("cost").value)/alpha;
var charge = d3.range(sites.length).map(function(d) { return cost; });
// G object for background
var backgroundG = svg.append("g");
// G object for map
var mapG = svg.append("g");
// G object for animations
var animationG = svg.append("g");
// G object for sites
var sitesG = svg.append("g");
// G object for points
var circleG = svg.append("g");
// G object for voronoi
var pathG = svg.append("g");
// G object for facilities
var facilitiesG = svg.append("g");
// G object for location names
var locationG = svg.append("g");
// G object for clickable areas
var clickableG = svg.append("g");
// G object for spinner
var spinnerG = svg.append("g").attr("id", "spinid");
backgroundG.append("rect")
.attr("x", 0).attr("y", 0)
.attr("width", width).attr("height", height)
.attr("fill", "#66B2FF");
sitesG.selectAll("circle")
.data(sites)
.enter()
.append("circle")
.attr("cx", function(d) { return d[0]; })
.attr("cy", function(d) { return d[1]; })
.attr("class", "site")
.attr("r", 5);
var mapColors = [];
var maxRed = 247, minRed = 116;
var maxGreen = 252, minGreen = 196;
var maxBlue = 245, minBlue = 118;
var numColors = 10;
for (var i = 0; i < numColors; i++) {
var red = String( Math.round( minRed + (maxRed - minRed)*i/numColors ) );
var green = String( Math.round( minGreen + (maxGreen - minGreen)*i/numColors) );
var blue = String( Math.round( minBlue + (maxBlue - minBlue)*i/numColors ) );
mapColors.push( "rgb(" + red + "," + green + "," + blue + ")");
}
var projection = d3.geo.albers()
.center([.2, 53.6])
.rotate([2.4, 0])
.parallels([50, 60])
.scale(1200 * 20)
.translate([width / 2, height / 2]);
var path = d3.geo.path()
.projection(projection);
var vertices;
d3.json("UKwithCounties.json", function(error, uk) {
d3.json("supermarket.json", function(supermarket) {
vertices = [];
// Set up map of UK
var places = topojson.feature(uk, uk.objects.placesUK);
var counties = topojson.feature(uk, uk.objects.ukcounties).features;
mapG.selectAll("path")
.data(counties)
.enter()
.append("path")
.attr("d", path)
.attr("fill", function(d,i) { return mapColors[i % 9]; })
.attr("stroke", "rgb(150,150,150)");
clickableG.selectAll("path")
.data(counties)
.enter()
.append("path")
.attr("d", path)
.attr("opacity", 0)
.on("mousedown", addPoint);
locationG.append("path")
.datum(places)
.attr("d", path)
.attr("class", "place");
locationG.selectAll(".place-label")
.data(places.features)
.enter().append("text")
.attr("class", "place-label")
.attr("transform", function(d) { return "translate(" + projection(d.geometry.coordinates) + ")"; })
.attr("x", function(d) { return d.geometry.coordinates[0] > -1 ? 6 : -6; })
.attr("dy", ".35em")
.style("text-anchor", function(d) { return d.geometry.coordinates[0] > -1 ? "start" : "end"; })
.text(function(d) { return d.properties.name; })
.attr("font-family", "Helvetica Neue")
.attr("font-size", 15)
.attr("font-weight", 400);
// Set up supermarket locations
// Add locations to vertices array
circleG.selectAll("circle")
.data(supermarket)
.enter()
.append("circle")
.filter(function(d) {
var loc = projection([d.LongWGS84, d.LatWGS84]);
return ( loc[0] >= 0 && loc[0] <= width && loc[1] >= 0 && loc[1] <= height);})
.attr("cx", function (d) { var loc = projection([d.LongWGS84, d.LatWGS84]);
vertices.push([parseFloat(loc[0]), parseFloat(loc[1])]);
return loc[0]; })
.attr("cy", function (d) { var loc = projection([d.LongWGS84, d.LatWGS84]);
return loc[1]; })
.attr("r", 2)
.attr("class", "client");
console.log('number of vertices', vertices.length);
});
});
function addPoint() {
facilitiesG.selectAll("circle").remove("circle");
animationG.selectAll("circle").remove("circle");
pathG.selectAll("line").remove("line");
var point = d3.mouse(this);
console.log('point', point);
sites.push(point);
sitesG.append("circle")
.attr("cx", point[0])
.attr("cy", point[1])
.attr("class", "site")
.attr("r", 5);
charge.push(cost);
// Add animation
animationG.selectAll("circle").remove("circle");
var anim = animationG.append("circle")
.attr("cx", point[0])
.attr("cy", point[1])
.attr("r", 0)
.attr("fill", "black");
anim.style("opacity", .5)
.transition()
.style("opacity", 0)
.attr("r", 20)
.duration(200)
.ease("out");
}
function compute() {
if (sites.length < 1) {
alert("Add potential sites to compute the optimal facility locations!");
return;
}
facilitiesG.selectAll("circle").remove("circle");
animationG.selectAll("circle").remove("circle");
pathG.selectAll("line").remove("line");
// Show loading screen
spinnerG.append("rect")
.attr("x", 0).attr("y", 0)
.attr("width", width).attr("height", height)
.attr("fill", "white")
.attr("opacity", .5);
spinnerG.append("text")
.attr("x", width/2)
.attr("y", .4*height)
.attr("font-size", 25)
.attr("font-family", "Helvetica Neue")
.attr("font-weight", 400)
.attr("text-anchor", "middle")
.text("Computing...");
var target = document.getElementById('demoarea');
spinner = new Spinner({left: '375%'}).spin(target);
cost = parseFloat(document.getElementById("cost").value)/alpha;
charge = [];
for (var i = 0; i < sites.length; i++) {
charge.push(cost);
}
d3.json('facility.py')
.header('Content-Type', 'application/json')
.post(JSON.stringify({'clients': vertices,
'facilities': sites,
'charge': charge }), serverResponse);
}
function serverResponse(error, data) {
console.log('serverResponse');
console.log('data', data);
// Remove loading screen
spinnerG.selectAll("rect").remove("rect");
spinnerG.selectAll("text").remove("text");
spinner.stop();
if (!error) {
if ('solution' in data) {
// Import solution and put it into correct format
var solution = data['solution'];
if (solution[0] === "error") {
alert("10 second solve time limit exceeded. Reduce the number of the candidate facilities.");
return;
}
var solution1 = solution[0]; // Facilities to open
var solution2 = solution[1]; // Edges to draw
var logMsg = solution[2]; // Log message to display
d3.select('#logfile').html(logMsg);
var facilities = [];
for (var i = 0; i < solution1.length; i++) {
facilities.push(sites[solution1[i]]);
}
facilitiesG.selectAll("circle").remove("circle");
animationG.selectAll("circle").remove("circle");
var faci = facilitiesG.selectAll("circle")
.data(facilities)
.enter()
.append("circle")
.attr("cx", function(d) { return d[0]; })
.attr("cy", function(d) { return d[1]; })
.attr("class", function(d, i) { return "c" + i % 5; })
.attr("r",60)
.attr("stroke-width", 2);
var anim = animationG.selectAll("circle")
.data(facilities)
.enter()
.append("circle")
.attr("cx", function(d) { return d[0]; })
.attr("cy", function(d) { return d[1]; })
.attr("class", function(d, i) { return "c" + i % 5; })
.attr("r",0)
.attr("stroke-width", 0);
faci.style("opacity", 0)
.style("stroke-opacity", 0)
.transition()
.style("opacity", 1)
.style("stroke-opacity", 1)
.attr("r", 6)
.duration(500)
.delay(function(d, i) { return i*100 });
anim.style("opacity", .5)
.style("stroke-opacity", .5)
.transition()
.style("opacity", 0)
.style("stroke-opacity", 0)
.attr("r", 30)
.duration(500)
.ease("out")
.delay(function(d, i) { return i*100 + 300});
pathG.selectAll("line").remove("line");
var connections = pathG.selectAll("line")
.data(solution2)
.enter()
.append("line")
.attr("x1", function(d) { var loc = vertices[d[0]]; return loc[0]; })
.attr("y1", function(d) { var loc = vertices[d[0]]; return loc[1]; })
.attr("x2", function(d) { var loc = sites[d[1]]; return loc[0]; })
.attr("y2", function(d) { var loc = sites[d[1]]; return loc[1]; })
.attr("class", function(d) { var n = solution1.indexOf(d[1]);
return "v" + n % 5; })
.attr("stroke-width", 0)
.attr("opacity", 0);
connections.transition()
.attr("opacity", .6)
.attr("stroke-width", 3)
.duration(500)
.delay(1000);
}
}
}
function restart() {
sitesG.selectAll("circle").remove("circle");
facilitiesG.selectAll("circle").remove("circle");
animationG.selectAll("circle").remove("circle");
pathG.selectAll("line").remove("line");
sites = [];
}
</script>