ac-tuwien / MHLib.jl
Showing 4 of 20 files from the diff.

@@ -86,7 +86,7 @@
Loading
86 86
    apply_two_opt_move(permutation_solution, p1, p2)
87 87
88 88
Perform two-opt move on given solution defined as inversion of subsequence starting from 
89 -
    position p1 up to and including position p2.
89 +
    position `p1` up to and including position `p2`.
90 90
"""
91 91
function apply_two_opt_move!(s::PermutationSolution, p1::Integer, p2::Integer)
92 92
    @assert 1 <= p1 <= p2 <= length(s)
@@ -96,7 +96,7 @@
Loading
96 96
"""
97 97
    two_opt_move_delta_eval(permutation_solution, p1, p2)
98 98
99 -
Return the delta in the objective value when inverting s.x from position p1 to position p2.
99 +
Return the delta in the objective value when inverting `s.x` from pos. `p1` to pos. `p2`.
100 100
101 101
The function returns the difference in the objective function if the move would be performed,
102 102
the solution, however, is not changed.

@@ -41,18 +41,18 @@
Loading
41 41
        default = -1
42 42
    "--mh_ttime"
43 43
        help = "time limit [s] (<0: turned off)"
44 -
        arg_type = Int
45 -
        default = -1
44 +
        arg_type = Float64
45 +
        default = -1.0
46 46
    "--mh_tctime"
47 47
        help = "maximum time [s] without improvement (<0: turned off)"
48 -
        arg_type = Int
49 -
        default = -1
48 +
        arg_type = Float64
49 +
        default = -1.0
50 50
    "--mh_tobj"
51 51
        help = "objective value at which should be terminated when reached (<0: turned off)"
52 52
        arg_type = Float64
53 53
        default = -1.0
54 54
    "--mh_checkit"
55 -
        help = "call check() for each solution after each method application"
55 +
        help = "call `check` for each solution after each method application"
56 56
        arg_type = Bool
57 57
        default = false
58 58
end
@@ -64,8 +64,8 @@
Loading
64 64
Data in conjunction with a method application's result.
65 65
66 66
Attributes
67 -
- `changed`: if false, the solution has not been changed by the method application
68 -
- `terminate`: if true, a termination condition has been fulfilled
67 +
- `changed`: if `false`, the solution has not been changed by the method application
68 +
- `terminate`: if `true`, a termination condition has been fulfilled
69 69
- `log_info`: customized log info
70 70
"""
71 71
mutable struct Result
@@ -127,7 +127,7 @@
Loading
127 127
Attributes
128 128
- `incumbent`: incumbent solution, i.e., initial solution and always best solution so far
129 129
    encountered
130 -
- `incumbent_valid`: true if incumbent is a valid solution to be considered
130 +
- `incumbent_valid`: `true` if incumbent is a valid solution to be considered
131 131
- `incumbent_iteration`: iteration in which incumbent was found
132 132
- `incumbent_time`: time at which incumbent was found
133 133
- `methods`: vector of all `MHMethods`
@@ -159,7 +159,7 @@
Loading
159 159
Create a `MHMethod` scheduler.
160 160
161 161
Create a Scheduler for the given solution with the given methods provides as
162 -
`Vector{MHMethod}`. If `consider_initial_sol` is true, consider the given solution as
162 +
`Vector{MHMethod}`. If `consider_initial_sol` is `true`, consider the given solution as
163 163
valid initial solution; otherwise it is assumed to be uninitialized.
164 164
165 165
"""
@@ -167,7 +167,7 @@
Loading
167 167
        consider_initial_sol::Bool=false)
168 168
    method_stats = Dict([(m.name, MHMethodStatistics()) for m in methods])
169 169
    s = Scheduler(sol, consider_initial_sol, 0, 0.0, methods, method_stats, 0,
170 -
        time(), missing, settings[:mh_checkit])
170 +
        time(), missing, settings[:mh_checkit]::Bool)
171 171
    log_iteration_header(s)
172 172
    if s.incumbent_valid
173 173
        log_iteration(s, "-", NaN, sol, true, true, "")
@@ -268,15 +268,16 @@
Loading
268 268
"""
269 269
    check_termination(scheduler)
270 270
271 -
Check termination conditions and return true when to terminate.
271 +
Check termination conditions and return `true` when to terminate.
272 272
"""
273 273
function check_termination(s::Scheduler)::Bool
274 274
    t = time()
275 -
    if 0 <= settings[:mh_titer] <= s.iteration ||
276 -
        0 <= settings[:mh_tciter] <= s.iteration - s.incumbent_iteration ||
277 -
        0 <= settings[:mh_ttime] <= t - s.time_start ||
278 -
        0 <= settings[:mh_tctime] <= t - s.incumbent_time ||
279 -
        0 <= settings[:mh_tobj] && !is_worse_obj(s.incumbent, obj(s.incumbent), settings[:mh_tobj])
275 +
    if 0 <= settings[:mh_titer]::Int <= s.iteration ||
276 +
        0 <= settings[:mh_tciter]::Int <= s.iteration - s.incumbent_iteration ||
277 +
        0 <= settings[:mh_ttime]::Float64 <= t - s.time_start ||
278 +
        0 <= settings[:mh_tctime]::Float64 <= t - s.incumbent_time ||
279 +
        0 <= settings[:mh_tobj]::Float64 && !is_worse_obj(s.incumbent, obj(s.incumbent), 
280 +
            settings[:mh_tobj]::Float64)
280 281
        return true
281 282
    end
282 283
    false
@@ -286,8 +287,7 @@
Loading
286 287
"""
287 288
    perform_sequentially!(scheduler, solution, methods)
288 289
289 -
Applies the given methods sequentially, finally keeping the best solution as
290 -
incumbent.
290 +
Applies the given methods sequentially, finally keeping the best solution as incumbent.
291 291
"""
292 292
function perform_sequentially!(s::Scheduler, sol::Solution, meths::Vector{MHMethod})
293 293
    for m in next_method(meths)
@@ -377,9 +377,9 @@
Loading
377 377
"""
378 378
function log_iteration(sched::Scheduler, method_name::String, obj_old, new_sol::Solution,
379 379
        new_incumbent::Bool, in_any_case::Bool, log_info::String="")
380 -
    log = in_any_case || new_incumbent && settings[:mh_lnewinc]
380 +
    log = in_any_case || new_incumbent && settings[:mh_lnewinc]::Bool
381 381
    if !log
382 -
        lfreq = settings[:mh_lfreq]
382 +
        lfreq = settings[:mh_lfreq]::Int
383 383
        if lfreq > 0 && sched.iteration % lfreq == 0
384 384
            log = true
385 385
        elseif lfreq < 0 && is_logarithmic_number(sched.iteration)

@@ -1,5 +1,5 @@
Loading
1 1
#=
2 -
Managing global settings via decentralized specifications.
2 +
Managing global parameters/settings via decentralized specifications.
3 3
=#
4 4
5 5
using ArgParse
@@ -16,7 +16,18 @@
Loading
16 16
17 17
18 18
"""
19 -
Dictionary with all parameters and their values.
19 +
    settings::Dict{Symbol,Any}
20 +
21 +
Global settings, i.e., dictionary with all parameters and their values.
22 +
23 +
24 +
Settings can be given by command line arguments, in a configuration file or in code, e.g.,
25 +
provided in the main program.
26 +
27 +
For using module-specific settings, define them via `ArgParseSettings` and `@add_arg_table!`
28 +
and call `parse_settings!`, values can then be accessed via `settings[:<name>]::<Type>`.
29 +
The type-cast is optional but may frequently help to achieve type-stability.
30 +
20 31
"""
21 32
const settings = Dict{Symbol,Any}()
22 33

@@ -40,7 +40,7 @@
Loading
40 40
Return true if the optimization goal is to maximize the objective function
41 41
in the given type of solutions.
42 42
43 -
This default implementation returns true.
43 +
This default implementation returns `true`.
44 44
"""
45 45
to_maximize(::Type) = true
46 46
to_maximize(s::Solution) = to_maximize(typeof(s))
@@ -56,9 +56,9 @@
Loading
56 56
57 57
    
58 58
"""
59 -
    obj(::Solution)
59 +
    obj(s::Solution)
60 60
61 -
Return obj_val if obj_val_valid or calculate it via objective(::Solution).
61 +
Return `s.obj_val` if `obj_val_valid` or calculate it via `objective(::Solution)`.
62 62
"""
63 63
function obj(s::Solution)
64 64
    if !s.obj_val_valid
@@ -75,7 +75,7 @@
Loading
75 75
(Re-)calculate the objective value of the given solution and return it.
76 76
"""
77 77
calc_objective(::Solution) =
78 -
    error("Abstract calc_objective(solution) called")
78 +
    error("Abstract calc_objective(::Solution) called")
79 79
80 80
81 81
"""
@@ -89,7 +89,7 @@
Loading
89 89
"""
90 90
    is_equal(::Solution, ::Solution)
91 91
92 -
Return true if the two solutions are considered equal and false otherwise.
92 +
Return `true` if the two solutions are considered equal and false otherwise.
93 93
94 94
The default implementation just checks if the objective values are the same.
95 95
"""
@@ -99,7 +99,7 @@
Loading
99 99
"""
100 100
    is_better(::Solution, ::Solution)
101 101
102 -
Return true if the first solution is better than the second.
102 +
Return `true` if the first solution is better than the second.
103 103
"""
104 104
function is_better(s1::S, s2::S) where {S <: Solution}
105 105
    to_maximize(s1) ? obj(s1) > obj(s2) : obj(s1) < obj(s2)
@@ -109,7 +109,7 @@
Loading
109 109
"""
110 110
    is_worse(::Solution, ::Solution)
111 111
112 -
Return true if the first solution is worse than the second.
112 +
Return `true` if the first solution is worse than the second.
113 113
"""
114 114
function is_worse(s1::S, s2::S) where {S <: Solution}
115 115
    to_maximize(s1) ? obj(s1) < obj(s2) : obj(s1) > obj(s2)
@@ -119,7 +119,7 @@
Loading
119 119
"""
120 120
    is_better_obj(::Solution, obj1, obj2)
121 121
122 -
Return true if obj1 is a better objective value than obj2 in
122 +
Return `true` if `obj1` is a better objective value than `obj2` in
123 123
the given solution type.
124 124
"""
125 125
function is_better_obj(s::Solution, obj1, obj2)
@@ -130,7 +130,7 @@
Loading
130 130
"""
131 131
    is_worse_obj(::Solution, obj1, obj2)
132 132
133 -
Return true if obj1 is a worse objective value than obj2 in
133 +
Return `true` if `obj1` is a worse objective value than `obj2` in
134 134
the given solution type.
135 135
"""
136 136
function is_worse_obj(s::Solution, obj1, obj2)
@@ -155,7 +155,7 @@
Loading
155 155
156 156
Check validity of solution.
157 157
158 -
If a problem is encountered, raise an exception.
158 +
If a problem is encountered, terminate with an error.
159 159
The default implementation just re-calculates the objective value.
160 160
"""
161 161
function check(s::Solution)::Nothing
@@ -176,7 +176,7 @@
Loading
176 176
"""
177 177
    VectorSolution
178 178
179 -
An abstract solution encoded by a vector of type `T`.
179 +
An abstract solution encoded by a vector of elements with type `T`.
180 180
181 181
Concrete subtypes need to implement:
182 182
- all requirements of the supertype `Solution`
@@ -230,7 +230,7 @@
Loading
230 230
"""
231 231
    k_random_flips!(::BoolVectorSolution, k)
232 232
233 -
Perform k random flips (i.e. exactly k bits are flipped) and call invalidate.
233 +
Perform `k` random flips, i.e., exactly `k` bits are flipped, and call `invalidate`.
234 234
"""
235 235
function k_random_flips!(s::BoolVectorSolution, k::Int)
236 236
    p = sample(1:length(s), k, replace = false)
@@ -242,7 +242,7 @@
Loading
242 242
"""
243 243
    k_flip_neighborhood_search!(::BoolVectorSolution, k::Int, best_improvement::Bool)
244 244
245 -
Perform one major iteration of a k-flip local search, i.e., search one neighborhood.
245 +
Perform one major iteration of a `k`-flip local search, i.e., search one neighborhood.
246 246
247 247
If `best_improvement` is set, the neighborhood is completely searched and a best neighbor is
248 248
kept; otherwise the search terminates in a first-improvement manner, i.e., keeping a first
@@ -266,7 +266,7 @@
Loading
266 266
                if !best_improvement
267 267
                    return true
268 268
                end
269 -
                best_sol[:] = s.x
269 +
                copy!(best_sol, s.x)
270 270
                best_obj = obj_val
271 271
                better_found = true
272 272
            end
@@ -292,7 +292,7 @@
Loading
292 292
        end
293 293
    end
294 294
    if better_found
295 -
        s.x[:] = best_sol
295 +
        copy!(s.x, best_sol)
296 296
        obj_val = best_obj
297 297
    end
298 298
    obj_val
Files Coverage
src 82.94%
Project Totals (7 files) 82.94%
Sunburst
The inner-most circle is the entire project, moving away from the center are folders then, finally, a single file. The size and color of each slice is representing the number of statements and the coverage, respectively.
Icicle
The top section represents the entire project. Proceeding with folders and finally individual files. The size and color of each slice is representing the number of statements and the coverage, respectively.
Grid
Each block represents a single file in the project. The size and color of each block is represented by the number of statements and the coverage, respectively.
Loading