Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

removed impl and use dispatch instead #273

Merged
merged 3 commits into from
Aug 21, 2021
Merged
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 0 additions & 3 deletions src/ConstraintSolver.jl
Original file line number Diff line number Diff line change
Expand Up @@ -725,8 +725,6 @@ function solve!(com::CS.CoM)
com.branch_strategy = get_branch_strategy(; options = options)
com.branch_split = get_branch_split(; options = options)

set_impl_functions!(com)

if :Info in com.options.logging
print_info(com)
end
Expand All @@ -749,7 +747,6 @@ function solve!(com::CS.CoM)
added_con_idxs = simplify!(com)
if length(added_con_idxs) > 0
set_in_all_different!(com; constraints = com.constraints[added_con_idxs])
set_impl_functions!(com; constraints = com.constraints[added_con_idxs])
!init_constraints!(com; constraints = com.constraints[added_con_idxs]) &&
return :Infeasible
!update_init_constraints!(com; constraints = com.constraints[added_con_idxs]) &&
Expand Down
268 changes: 91 additions & 177 deletions src/constraints.jl
Original file line number Diff line number Diff line change
Expand Up @@ -43,15 +43,12 @@ function init_constraints!(com::CS.CoM; constraints = com.constraints)
feasible = true
for constraint in constraints
constraint.is_deactivated && continue
if constraint.impl.init
feasible = init_constraint!(com, constraint, constraint.fct, constraint.set)
!feasible && break
end
feasible = init_constraint!(com, constraint, constraint.fct, constraint.set)
!feasible && break
constraint.is_initialized = true
if constraint.impl.activate
feasible = activate_constraint!(com, constraint, constraint.fct, constraint.set)
!feasible && break
end

feasible = activate_constraint!(com, constraint, constraint.fct, constraint.set)
!feasible && break
constraint.is_activated = true
end
return feasible
Expand All @@ -66,177 +63,26 @@ Return if feasible after the update of the initalization
function update_init_constraints!(com::CS.CoM; constraints = com.constraints)
feasible = true
for constraint in com.constraints
if constraint.impl.update_init
feasible = update_init_constraint!(
com,
constraint,
constraint.fct,
constraint.set,
constraints,
)
!feasible && break
end
feasible = update_init_constraint!(
com,
constraint,
constraint.fct,
constraint.set,
constraints,
)
!feasible && break
end
return feasible
end

"""
set_impl_functions!(com, constraint::Constraint)

Set std.impl.[] for each constraint
"""
function set_impl_functions!(com, constraint::Constraint)
if com.sense != MOI.FEASIBILITY_SENSE
set_impl_update_best_bound!(constraint)
end
set_impl_init!(constraint)
set_impl_activate!(constraint)
set_impl_update_init!(constraint)
set_impl_finished_pruning!(constraint)
set_impl_restore_pruning!(constraint)
set_impl_reverse_pruning!(constraint)
end

"""
set_impl_functions!(com::CS.CoM)

Set std.impl.[] for each constraint
"""
function set_impl_functions!(com::CS.CoM; constraints = com.constraints)
for constraint in constraints
set_impl_functions!(com, constraint)
end
end

"""
set_impl_init!(constraint::Constraint)
Sets `std.impl.init` if the constraint type has a `init_constraint!` method
"""
function set_impl_init!(constraint::Constraint)
c_type = typeof(constraint)
c_fct_type = typeof(constraint.fct)
c_set_type = typeof(constraint.set)
if hasmethod(init_constraint!, (CS.CoM, c_type, c_fct_type, c_set_type))
constraint.impl.init = true
end
end

"""
set_impl_activate!(constraint::Constraint)
Sets `std.activate.init` if the constraint type has a `activate_constraint!` method
"""
function set_impl_activate!(constraint::Constraint)
c_type = typeof(constraint)
c_fct_type = typeof(constraint.fct)
c_set_type = typeof(constraint.set)
if hasmethod(activate_constraint!, (CS.CoM, c_type, c_fct_type, c_set_type))
constraint.impl.activate = true
end
end

"""
set_impl_update_init!(constraint::Constraint)
Sets `std.impl.update_init` if the constraint type has a `update_init_constraint!` method
"""
function set_impl_update_init!(constraint::Constraint)
c_type = typeof(constraint)
c_fct_type = typeof(constraint.fct)
c_set_type = typeof(constraint.set)
if hasmethod(
update_init_constraint!,
(CS.CoM, c_type, c_fct_type, c_set_type, Vector{<:Constraint}),
)
constraint.impl.update_init = true
end
end

"""
set_impl_update_best_bound!(constraint::Constraint)

Sets `update_best_bound` if the constraint type has a `update_best_bound_constraint!` method
"""
function set_impl_update_best_bound!(constraint::Constraint)
c_type = typeof(constraint)
c_fct_type = typeof(constraint.fct)
c_set_type = typeof(constraint.set)
if hasmethod(
update_best_bound_constraint!,
(CS.CoM, c_type, c_fct_type, c_set_type, Int, Int, Int),
)
constraint.impl.update_best_bound = true
else # just to be sure => set it to false otherwise
constraint.impl.update_best_bound = false
end
end

"""
set_impl_reverse_pruning!(constraint::Constraint)
Sets `std.impl.single_reverse_pruning` and `std.impl.reverse_pruning`
if `single_reverse_pruning_constraint!`, `reverse_pruning_constraint!` are implemented for `constraint`.
"""
function set_impl_reverse_pruning!(constraint::Constraint)
c_type = typeof(constraint)
c_fct_type = typeof(constraint.fct)
c_set_type = typeof(constraint.set)
if hasmethod(
single_reverse_pruning_constraint!,
(CS.CoM, c_type, c_fct_type, c_set_type, CS.Variable, Int),
)
constraint.impl.single_reverse_pruning = true
else # just to be sure => set it to false otherwise
constraint.impl.single_reverse_pruning = false
end

if hasmethod(reverse_pruning_constraint!, (CS.CoM, c_type, c_fct_type, c_set_type, Int))
constraint.impl.reverse_pruning = true
else # just to be sure => set it to false otherwise
constraint.impl.reverse_pruning = false
end
end

"""
set_impl_finished_pruning!(constraint::Constraint)
Sets `std.impl.finished_pruning` if `finished_pruning_constraint!` is implemented for `constraint`.
"""
function set_impl_finished_pruning!(constraint::Constraint)
c_type = typeof(constraint)
c_fct_type = typeof(constraint.fct)
c_set_type = typeof(constraint.set)
if hasmethod(finished_pruning_constraint!, (CS.CoM, c_type, c_fct_type, c_set_type))
constraint.impl.finished_pruning = true
else # just to be sure => set it to false otherwise
constraint.impl.finished_pruning = false
end
end

"""
set_impl_restore_pruning!(constraint::Constraint)
Sets `std.impl.restore_pruning` if `restore_pruning_constraint!` is implemented for the `constraint`.
"""
function set_impl_restore_pruning!(constraint::Constraint)
c_type = typeof(constraint)
c_fct_type = typeof(constraint.fct)
c_set_type = typeof(constraint.set)
if hasmethod(
restore_pruning_constraint!,
(CS.CoM, c_type, c_fct_type, c_set_type, Union{Int,Vector{Int}}),
)
constraint.impl.restore_pruning = true
else # just to be sure => set it to false otherwise
constraint.impl.restore_pruning = false
end
end

"""
call_finished_pruning!(com)

Call `finished_pruning_constraint!` for every constraint which implements that function as saved in `constraint.impl.finished_pruning`
"""
function call_finished_pruning!(com)
for constraint in com.constraints
if constraint.impl.finished_pruning
finished_pruning_constraint!(com, constraint, constraint.fct, constraint.set)
end
finished_pruning_constraint!(com, constraint, constraint.fct, constraint.set)
end
end

Expand All @@ -247,15 +93,13 @@ Call `call_restore_pruning!` for every constraint which implements that function
"""
function call_restore_pruning!(com, prune_steps)
for constraint in com.constraints
if constraint.impl.restore_pruning
restore_pruning_constraint!(
com,
constraint,
constraint.fct,
constraint.set,
prune_steps,
)
end
restore_pruning_constraint!(
com,
constraint,
constraint.fct,
constraint.set,
prune_steps,
)
end
end

Expand Down Expand Up @@ -294,3 +138,73 @@ function get_two_unfixed(com::CS.CoM, constraint::Constraint)
end
return local_vidx_1, vidx_1, local_vidx_2, vidx_2
end

function init_constraint!(
com::CS.CoM,
constraint::Constraint,
fct,
set
)
return true
end

function activate_constraint!(::CS.CoM, ::Constraint, fct, set)
return true
end

function finished_pruning_constraint!(::CS.CoM, ::Constraint, fct, set)
nothing
end

function reverse_pruning_constraint!(
com::CoM,
constraint::Constraint,
fct,
set,
backtrack_id,
)
nothing
end

function single_reverse_pruning_constraint!(
::CoM,
::Constraint,
fct,
set,
variable,
backtrack_idx,
)
nothing
end

function restore_pruning_constraint!(
::CoM,
::Constraint,
fct,
set,
prune_steps,
)
nothing
end

function update_best_bound_constraint!(
com::CS.CoM,
constraint::Constraint,
fct,
set,
vidx,
lb,
ub,
)
return true
end

function update_init_constraint!(
com::CS.CoM,
constraint::Constraint,
fct,
set,
constraints,
)
return true
end
Loading