-
Notifications
You must be signed in to change notification settings - Fork 7
Expand file tree
/
Copy pathQap.java
More file actions
105 lines (75 loc) · 2.26 KB
/
Qap.java
File metadata and controls
105 lines (75 loc) · 2.26 KB
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
package de.mh4j.examples.qap.model;
import java.util.ArrayList;
import java.util.List;
import de.mh4j.solver.Solution;
/**
*
* The quadratic assignment problem (QAP) is one of fundamental combinatorial
* optimization problems in the branch of optimization or operations research in
* mathematics, from the category of the facilities-location problems.
*
*
* **/
public class Qap implements Solution<Qap> {
public List<String> solution = new ArrayList<>();;
public int costs = 0;
public Qap(List<String> solution) {
for (int i = 0; i < solution.size(); i++) {
this.solution.add(solution.get(i));
}
calculateCosts(this.solution);
}
public Qap(Qap original) {
this.solution = new ArrayList<>(original.solution);
this.costs = original.costs;
}
/**
* __n__ __n__
* \ \
* \ \ Fij*Dp(i)p(j) for minimizing or maximizing the costs
* / / where F are the facilities' flows and D the
* /____ /____ the distances
* i=1 j=1
*
*
* for example in case of 3 facilities(1,2,3) and 3 locations(A,B,C) this
* method would calculate: f12*dAB+f13*dAC+f21*dBA+f23*dBC+ f31*dCA+f32*dCB
*
*
* */
public void calculateCosts(List<String> solution) {
costs = 0;
int k = 0;
int m;
for (int i = 0; i < solution.size(); i++) {
for (int j = 0; j < Facility.facilities.size(); j++) {
if (solution.get(i).equals(Facility.facilities.get(j).name)) {
m = 0;
for (int l = 0; l < solution.size(); l++) {
for (int o = 0; o < Facility.facilities.get(j).facilitiesNames
.size(); o++) {
if (solution.get(l).equals(
Facility.facilities.get(j).facilitiesNames
.get(o))) {
k = Facility.facilities.get(j).facilitiesCosts
.get(o);
costs = costs
+ k
* Location.locations.get(i).distances
.get(m);
m++;
}
}
}
}
}
}
}
public int getCosts() {
return costs;
}
@Override
public boolean isBetterThan(Qap otherSolution) {
return otherSolution.getCosts() < costs;
}
}