Skip to content

Commit 1ad8f96

Browse files
blibliboeLaar
andauthored
32 refractor code for multiple method to generate alternatives (#38)
* Updated the generate parralel to return state aka all solutions from metaheuristics * Updated generate alternatives to make multiple MGA techniques possible * Update optimization specifically to also allow for multiple MGa techniques * Added the max-distance method to the MGA methods, this was already implemented * Added the max-distance method to the MGA methods, this was already implemented * Changed it so that max distance is not neccesarily the standard when testing * Exported core functions * Explicitly used JuMP when calling all variables * Changes based on the formatter * Capitalized the D in Max-Distance * Removed a link that does not work according to the pre-commit * Added test coverage * Moved the test file to the test directory and not in a subdirectory * Removed the unneccessary check for test in the run test file * Fixed a format error --------- Co-authored-by: Laar <luuk.vandelaar@tno.nl>
1 parent 220c3e0 commit 1ad8f96

File tree

9 files changed

+613
-219
lines changed

9 files changed

+613
-219
lines changed

Project.toml

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -6,11 +6,13 @@ version = "0.1.0"
66
[deps]
77
DataStructures = "864edb3b-99cc-5e75-8d2d-829cb0a9cfe8"
88
Distances = "b4f34e82-e78d-54a5-968a-f98e89d6e8f7"
9+
Ipopt = "b6b21f68-93f8-5de0-b562-5493be1d77c9"
910
JuMP = "4076af6c-e467-56ae-b986-b466b2749572"
1011
MathOptInterface = "b8f27783-ece8-5eb3-8dc8-9495eed66fee"
1112
Metaheuristics = "bcdb8e00-2c21-11e9-3065-2b553b22f898"
1213

1314
[compat]
15+
Ipopt = "1.7.1"
1416
DataStructures = "0.19"
1517
Distances = "0.10"
1618
JuMP = "1"

coverage/lcov.info

Lines changed: 247 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,247 @@
1+
SF:src\MGA-Methods\Max-Distance.jl
2+
DA:29,16
3+
DA:36,8
4+
DA:37,8
5+
DA:40,8
6+
DA:42,8
7+
DA:43,8
8+
DA:71,4
9+
DA:77,2
10+
DA:79,2
11+
DA:80,2
12+
DA:83,2
13+
DA:85,2
14+
LH:12
15+
LF:12
16+
end_of_record
17+
SF:src\NearOptimalAlternatives.jl
18+
LH:0
19+
LF:0
20+
end_of_record
21+
SF:src\alternative-metaheuristics.jl
22+
DA:5,7
23+
DA:26,8
24+
DA:32,8
25+
DA:33,8
26+
DA:34,28
27+
DA:35,21
28+
DA:37,1
29+
DA:39,14
30+
DA:40,8
31+
DA:59,91022
32+
DA:65,91022
33+
DA:67,91022
34+
DA:69,182044
35+
DA:71,364094
36+
DA:72,172044
37+
DA:74,10003
38+
DA:76,273072
39+
DA:77,91022
40+
DA:100,9
41+
DA:108,9
42+
DA:109,18
43+
DA:111,9
44+
DA:114,9
45+
DA:115,44
46+
DA:116,44
47+
DA:118,91015
48+
DA:120,91006
49+
DA:122,91006
50+
DA:124,91006
51+
DA:125,91006
52+
DA:131,0
53+
DA:138,91006
54+
DA:140,91006
55+
DA:141,364022
56+
DA:142,364022
57+
DA:145,364022
58+
DA:147,6
59+
DA:148,2
60+
DA:150,2
61+
DA:151,4
62+
DA:152,2
63+
DA:154,2
64+
DA:155,2
65+
DA:156,2
66+
DA:158,2
67+
DA:159,0
68+
DA:160,0
69+
DA:161,0
70+
DA:162,0
71+
DA:164,364016
72+
DA:166,728032
73+
DA:167,20000
74+
DA:170,344016
75+
DA:171,172008
76+
DA:172,172008
77+
DA:173,172008
78+
DA:174,0
79+
DA:175,0
80+
DA:176,0
81+
DA:177,0
82+
DA:178,0
83+
DA:181,0
84+
DA:183,637038
85+
DA:186,91006
86+
DA:187,0
87+
DA:190,91006
88+
DA:191,91004
89+
DA:194,91006
90+
DA:197,9
91+
DA:212,10
92+
DA:214,10
93+
DA:215,32
94+
DA:216,10
95+
DA:217,16
96+
DA:218,16
97+
DA:219,22
98+
DA:222,10
99+
DA:224,10
100+
DA:225,32
101+
DA:227,64
102+
DA:228,30
103+
DA:229,30
104+
DA:230,15
105+
DA:231,15
106+
DA:232,15
107+
DA:233,0
108+
DA:234,0
109+
DA:235,0
110+
DA:237,0
111+
DA:238,0
112+
DA:239,0
113+
DA:242,32
114+
DA:244,10
115+
DA:267,7
116+
DA:276,7
117+
DA:277,7
118+
DA:278,7
119+
DA:279,13
120+
DA:280,19
121+
DA:282,7
122+
DA:284,7
123+
DA:287,7
124+
DA:304,2
125+
DA:310,2
126+
DA:311,2
127+
DA:312,20003
128+
DA:313,20001
129+
DA:314,20001
130+
DA:315,20001
131+
DA:317,2
132+
DA:327,7
133+
DA:328,7
134+
DA:329,7
135+
LH:95
136+
LF:113
137+
end_of_record
138+
SF:src\alternative-optimisation.jl
139+
DA:47,16
140+
DA:56,8
141+
DA:57,8
142+
DA:58,8
143+
DA:61,8
144+
DA:63,8
145+
DA:64,0
146+
DA:67,8
147+
DA:69,8
148+
DA:71,8
149+
DA:72,7
150+
DA:78,1
151+
DA:105,4
152+
DA:112,2
153+
DA:113,0
154+
DA:115,2
155+
LH:14
156+
LF:16
157+
end_of_record
158+
SF:src\generate-alternatives.jl
159+
DA:24,16
160+
DA:34,16
161+
DA:35,1
162+
DA:36,7
163+
DA:37,1
164+
DA:38,6
165+
DA:39,1
166+
DA:42,5
167+
DA:44,5
168+
DA:45,5
169+
DA:54,5
170+
DA:55,5
171+
DA:56,5
172+
DA:57,5
173+
DA:60,5
174+
DA:61,1
175+
DA:62,1
176+
DA:68,1
177+
DA:69,1
178+
DA:70,1
179+
DA:71,1
180+
DA:72,1
181+
DA:74,5
182+
DA:97,14
183+
DA:105,14
184+
DA:106,1
185+
DA:107,6
186+
DA:108,1
187+
DA:109,5
188+
DA:110,1
189+
DA:113,4
190+
DA:115,4
191+
DA:117,4
192+
DA:118,4
193+
DA:119,4
194+
DA:120,9
195+
DA:121,1
196+
DA:123,7
197+
DA:125,8
198+
DA:127,4
199+
DA:135,4
200+
DA:136,4
201+
DA:137,8
202+
DA:138,4
203+
DA:140,4
204+
DA:141,1
205+
DA:142,1
206+
DA:143,1
207+
DA:144,1
208+
DA:145,2
209+
DA:146,1
210+
DA:147,1
211+
DA:149,4
212+
LH:53
213+
LF:53
214+
end_of_record
215+
SF:src\results.jl
216+
DA:5,12
217+
DA:14,9
218+
DA:15,18
219+
DA:16,1
220+
DA:20,8
221+
DA:21,8
222+
DA:22,16
223+
DA:23,16
224+
DA:24,8
225+
DA:27,8
226+
DA:28,15
227+
DA:29,1
228+
DA:31,7
229+
DA:46,5
230+
DA:53,5
231+
DA:54,0
232+
DA:57,5
233+
DA:58,5
234+
DA:59,5
235+
DA:61,10
236+
DA:62,9
237+
DA:63,9
238+
DA:64,13
239+
DA:66,6
240+
DA:67,1
241+
DA:68,1
242+
DA:70,5
243+
DA:73,5
244+
DA:75,5
245+
LH:28
246+
LF:29
247+
end_of_record

src/MGA-Methods/Max-Distance.jl

Lines changed: 90 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,90 @@
1+
export Dist_update!, Dist_initial!
2+
3+
"""
4+
Dist_initial!(
5+
model::JuMP.Model,
6+
variables::AbstractArray{T,N},
7+
fixed_variables::Vector{VariableRef};
8+
weights::Vector{Float64} = zeros(length(variables)),
9+
metric::Distances.SemiMetric = SqEuclidean(),
10+
) where {T<:Union{VariableRef,AffExpr},N}
11+
12+
Initialize a JuMP model's objective to maximize the distance between the current solution and a reference solution, based on a specified metric.
13+
14+
This function is typically used in the context of generating diverse solutions (alternatives) to an optimization problem by first defining a distance-based objective that measures how different a new solution is from an existing (optimal) one.
15+
16+
# Arguments
17+
- `model::JuMP.Model`: a JuMP model that has been previously solved.
18+
- `variables::AbstractArray{T,N}`: the variables of the model to consider in the distance computation.
19+
- `fixed_variables::Vector{VariableRef}`: a subset of all variables of `model` that are not allowed to be changed when seeking for alternatives.
20+
- `weights::Vector{Float64}`: optional weights to influence the distance calculation (currently not used directly but reserved for extensions).
21+
- `metric::Distances.SemiMetric`: the distance metric used to compute dissimilarity (default is squared Euclidean distance).
22+
23+
# Behavior
24+
- Extracts the current solution values of `variables`.
25+
- Sets the model's objective to maximize the distance between the current variable values and the solution.
26+
- Changes the model's objective sense to `Max`.
27+
28+
"""
29+
function Dist_initial!(
30+
model::JuMP.Model,
31+
variables::AbstractArray{T,N},
32+
fixed_variables::Vector{VariableRef};
33+
weights::Vector{Float64} = zeros(length(variables)),
34+
metric::Distances.SemiMetric = SqEuclidean(),
35+
) where {T<:Union{VariableRef,AffExpr},N}
36+
vars_vec = [v for v in variables]
37+
solution = value.(vars_vec)
38+
39+
# Fix the variables that are fixed
40+
fix.(fixed_variables, value.(fixed_variables), force = true)
41+
42+
set_objective_sense(model, FEASIBILITY_SENSE)
43+
@objective(model, Max, Distances.evaluate(metric, vars_vec, solution))
44+
end
45+
46+
"""
47+
Dist_update!(
48+
model::JuMP.Model,
49+
variables::AbstractArray{T,N};
50+
weights::Vector{Float64} = zeros(length(variables)),
51+
metric::Distances.SemiMetric = Cityblock(),
52+
) where {T<:Union{VariableRef,AffExpr},N}
53+
54+
Update a JuMP model's objective function to include an additional distance term for generating multiple diverse alternatives (as in Modeling to Generate Alternatives).
55+
56+
This function builds upon a previously defined objective by incrementally adding a distance term between the current solution and a new reference solution. It is typically used after `Dist_initial!` or a prior call to `MGA_Dist_update!`.
57+
58+
# Arguments
59+
- `model::JuMP.Model`: the JuMP model being updated to generate further alternatives.
60+
- `variables::AbstractArray{T,N}`: the variables to consider in the distance computation.
61+
- `weights::Vector{Float64}`: optional weights for the distance metric (currently not directly used).
62+
- `metric::Distances.SemiMetric`: the distance metric used to compute dissimilarity (default is Cityblock distance).
63+
64+
# Behavior
65+
- Evaluates the current objective function to retrieve the cumulative distance so far.
66+
- Computes the distance between the current variable values and their previous optimal values.
67+
- Updates the objective function to maximize the sum of the cumulative and new distances.
68+
- Resets the model's objective sense to `Max`.
69+
70+
"""
71+
function Dist_update!(
72+
model::JuMP.Model,
73+
variables::AbstractArray{T,N};
74+
weights::Vector{Float64} = zeros(length(variables)),
75+
metric::Distances.SemiMetric = Cityblock(),
76+
) where {T<:Union{VariableRef,AffExpr},N}
77+
cumulative_distance = objective_function(model)
78+
79+
vars_vec = [v for v in variables]
80+
solution = value.(vars_vec)
81+
82+
# Reset objective sense to be able to update objective function.
83+
set_objective_sense(model, FEASIBILITY_SENSE)
84+
# Update objective by adding the distance between variables and the previous optimal solution.
85+
@objective(
86+
model,
87+
Max,
88+
cumulative_distance + Distances.evaluate(metric, vars_vec, solution)
89+
)
90+
end

src/NearOptimalAlternatives.jl

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,8 @@ using MathOptInterface
88
using Metaheuristics
99
using DataStructures
1010

11+
include("MGA-Methods/Max-Distance.jl")
12+
1113
include("results.jl")
1214
include("alternative-optimisation.jl")
1315
include("generate-alternatives.jl")

0 commit comments

Comments
 (0)