Skip to content
GitLab
Explore
Sign in
Primary navigation
Search or go to…
Project
M
MRIBuilder.jl
Manage
Activity
Members
Labels
Plan
Issues
Issue boards
Milestones
Wiki
Code
Merge requests
Repository
Branches
Commits
Tags
Repository graph
Compare revisions
Snippets
Build
Pipelines
Jobs
Pipeline schedules
Artifacts
Deploy
Releases
Package registry
Container Registry
Model registry
Operate
Environments
Terraform modules
Monitor
Incidents
Analyze
Value stream analytics
Contributor analytics
CI/CD analytics
Repository analytics
Model experiments
Help
Help
Support
GitLab documentation
Compare GitLab plans
Community forum
Contribute to GitLab
Provide feedback
Keyboard shortcuts
?
Snippets
Groups
Projects
Show more breadcrumbs
Michiel Cottaar
MRIBuilder.jl
Commits
e60211c3
Unverified
Commit
e60211c3
authored
6 months ago
by
Michiel Cottaar
Browse files
Options
Downloads
Patches
Plain Diff
ENH: allow for integer variables
parent
9f50197a
No related branches found
No related tags found
1 merge request
!9
Integer variables
Changes
1
Hide whitespace changes
Inline
Side-by-side
Showing
1 changed file
src/build_sequences.jl
+40
-10
40 additions, 10 deletions
src/build_sequences.jl
with
40 additions
and
10 deletions
src/build_sequences.jl
+
40
−
10
View file @
e60211c3
...
@@ -73,6 +73,7 @@ function build_sequence(f::Function, scanner::Union{Nothing, Scanner}, model::Tu
...
@@ -73,6 +73,7 @@ function build_sequence(f::Function, scanner::Union{Nothing, Scanner}, model::Tu
sequence
=
f
()
sequence
=
f
()
if
optimise
if
optimise
jump_model
=
GLOBAL_MODEL
[][
1
]
jump_model
=
GLOBAL_MODEL
[][
1
]
set_optimizer
(
jump_model
,
get_optimiser
(
jump_model
))
if
!
iszero
(
num_variables
(
jump_model
))
if
!
iszero
(
num_variables
(
jump_model
))
optimise_with_cost_func!
(
jump_model
,
total_cost_func
(),
n_attempts
)
optimise_with_cost_func!
(
jump_model
,
total_cost_func
(),
n_attempts
)
prev_cost_func
=
nothing
prev_cost_func
=
nothing
...
@@ -98,6 +99,37 @@ function number_equality_constraints(model::Model)
...
@@ -98,6 +99,37 @@ function number_equality_constraints(model::Model)
sum
([
num_constraints
(
model
,
expr
,
comp
)
for
(
expr
,
comp
)
in
JuMP
.
list_of_constraint_types
(
model
)
if
comp
<:
MOI
.
EqualTo
])
sum
([
num_constraints
(
model
,
expr
,
comp
)
for
(
expr
,
comp
)
in
JuMP
.
list_of_constraint_types
(
model
)
if
comp
<:
MOI
.
EqualTo
])
end
end
"""
model_has_integers(model)
Returns true if the model contains integer variables.
"""
model_has_integers
(
model
::
Model
)
=
(
VariableRef
,
MOI
.
Integer
)
in
list_of_constraint_types
(
model
)
"""
get_optimiser(model)
Returns a JuMP solver (https://jump.dev/JuMP.jl/stable/installation/#Supported-solvers) appropriate for this model.
"""
function
get_optimiser
(
model
::
Model
)
base_ipopt
=
optimizer_with_attributes
(
Ipopt
.
Optimizer
,
"print_level"
=>
0
,
"mu_strategy"
=>
"adaptive"
,
"max_iter"
=>
1000
,
)
if
model_has_integers
(
model
)
optimizer_with_attributes
(
Juniper
.
Optimizer
,
"nl_solver"
=>
base_ipopt
,
"log_levels"
=>
[],
)
else
return
base_ipopt
end
end
function
optimise_with_cost_func!
(
jump_model
::
Model
,
cost_func
,
n_attempts
)
function
optimise_with_cost_func!
(
jump_model
::
Model
,
cost_func
,
n_attempts
)
@objective
jump_model
Min
cost_func
@objective
jump_model
Min
cost_func
min_objective
=
Inf
min_objective
=
Inf
...
@@ -130,7 +162,7 @@ function optimise_with_cost_func!(jump_model::Model, cost_func, n_attempts)
...
@@ -130,7 +162,7 @@ function optimise_with_cost_func!(jump_model::Model, cost_func, n_attempts)
nsuccess
+=
1
nsuccess
+=
1
if
objective_value
(
jump_model
)
<
min_objective
if
objective_value
(
jump_model
)
<
min_objective
min_objective
=
objective_value
(
jump_model
)
min_objective
=
objective_value
(
jump_model
)
min_values
=
copy
(
backend
(
jump_model
)
.
optimizer
.
model
.
inner
.
x
)
min_values
=
copy
(
get_inner_state
(
jump_model
)
)
end
end
break
break
elseif
sub_attempt
==
3
elseif
sub_attempt
==
3
...
@@ -150,20 +182,18 @@ function optimise_with_cost_func!(jump_model::Model, cost_func, n_attempts)
...
@@ -150,20 +182,18 @@ function optimise_with_cost_func!(jump_model::Model, cost_func, n_attempts)
end
end
error
(
"Optimisation failed to converge. The following errors were raised:
$
err_string. Example errors for each type are printed above."
)
error
(
"Optimisation failed to converge. The following errors were raised:
$
err_string. Example errors for each type are printed above."
)
end
end
backend
(
jump_model
)
.
optimizer
.
model
.
inner
.
x
.=
min_values
get_inner_state
(
jump_model
)
.=
min_values
@assert
value
(
cost_func
)
≈
min_objective
eps
(
min_objective
)
@assert
value
(
cost_func
)
≈
min_objective
eps
(
min_objective
)
end
end
function
build_sequence
(
f
::
Function
,
scanner
::
Union
{
Nothing
,
Scanner
}
=
Default_Scanner
;
optimise
=
true
,
n_attempts
=
20
,
print_level
=
0
,
mu_strategy
=
"adaptive"
,
max_iter
=
1000
,
kwargs
...
)
get_inner_state
(
backend
::
Juniper
.
JuniperProblem
)
=
backend
.
solution
get_inner_state
(
backend
::
Ipopt
.
IpoptProblem
)
=
backend
.
x
get_inner_state
(
model
::
Model
)
=
get_inner_state
(
backend
(
model
)
.
optimizer
.
model
.
inner
)
function
build_sequence
(
f
::
Function
,
scanner
::
Union
{
Nothing
,
Scanner
}
=
Default_Scanner
;
optimise
=
true
,
n_attempts
=
20
)
if
optimise
||
GLOBAL_MODEL
[]
==
IGNORE_MODEL
if
optimise
||
GLOBAL_MODEL
[]
==
IGNORE_MODEL
full_kwargs
=
Dict
(
:
print_level
=>
print_level
,
:
mu_strategy
=>
mu_strategy
,
:
max_iter
=>
max_iter
,
kwargs
...
)
model
=
(
model
=
(
Model
(
optimizer_with_attributes
(
Ipopt
.
Optimizer
,
[
string
(
k
)
=>
v
for
(
k
,
v
)
in
full_kwargs
]
...
)
),
Model
(),
Tuple
{
Float64
,
AbstractJuMPScalar
}[]
Tuple
{
Float64
,
AbstractJuMPScalar
}[]
)
)
else
else
...
...
This diff is collapsed.
Click to expand it.
Preview
0%
Loading
Try again
or
attach a new file
.
Cancel
You are about to add
0
people
to the discussion. Proceed with caution.
Finish editing this message first!
Save comment
Cancel
Please
register
or
sign in
to comment