Skip to content

Commit a458ef1

Browse files
matbesanconWikunia
authored andcommitted
wording improvement, style (#55)
* deprecate `init` use constructor instead
1 parent d173321 commit a458ef1

24 files changed

+178
-177
lines changed

benchmark/graph_color/cs.jl

+1-1
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@ using ConstraintSolver
22
CS = ConstraintSolver
33

44
function main(filename; benchmark=false)
5-
com = CS.init()
5+
com = CS.ConstraintSolverModel()
66

77
lines = readlines(filename)
88
num_colors = 0

benchmark/sudoku/cs.jl

+1-1
Original file line numberDiff line numberDiff line change
@@ -60,7 +60,7 @@ function solve_all(grids; benchmark=false, single_times=true)
6060
end
6161

6262
function solve_one(grid)
63-
com = CS.init()
63+
com = CS.ConstraintSolverModel()
6464

6565
CS.build_search_space!(com, grid,[1,2,3,4,5,6,7,8,9],0)
6666
add_sudoku_constr!(com, grid)

src/ConstraintSolver.jl

+66-63
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,7 @@ const MOIU = MOI.Utilities
1111

1212
include("options.jl")
1313

14-
CS = ConstraintSolver
14+
const CS = ConstraintSolver
1515

1616
mutable struct Variable
1717
idx :: Int
@@ -21,15 +21,14 @@ mutable struct Variable
2121
last_ptr :: Int
2222
values :: Vector{Int}
2323
indices :: Vector{Int}
24-
offset :: Int
25-
min :: Int # the minimum value during the solving process
24+
offset :: Int
25+
min :: Int # the minimum value during the solving process
2626
max :: Int # for initial see lower/upper_bound
2727
changes :: Vector{Vector{Tuple{Symbol,Int64,Int64,Int64}}}
28-
has_upper_bound :: Bool # must be true to work
29-
has_lower_bound :: Bool # must be true to work
28+
has_upper_bound :: Bool # must be true to work
29+
has_lower_bound :: Bool # must be true to work
3030
is_fixed :: Bool
31-
is_integer :: Bool # must be true to work
32-
31+
is_integer :: Bool # must be true to work
3332
end
3433

3534
Variable(idx) = Variable(idx,0,0,0,0,[],[],0,0,0,Vector{Vector{Tuple{Symbol,Int64,Int64,Int64}}}(), false, false, false, false)
@@ -42,18 +41,19 @@ mutable struct CSInfo
4241
backtrack_reverses :: Int
4342
end
4443

45-
abstract type Constraint
46-
end
44+
abstract type Constraint end
4745

48-
abstract type ObjectiveFunction
49-
end
46+
abstract type ObjectiveFunction end
5047

5148
mutable struct SingleVariableObjective <: ObjectiveFunction
5249
fct :: Function
5350
index :: Int # index of the variable
5451
indices :: Vector{Int}
5552
end
5653

54+
# used to designate a feasibility sense
55+
struct NoObjective <: ObjectiveFunction end
56+
5757
mutable struct BasicConstraint <: Constraint
5858
idx :: Int
5959
fct :: Function
@@ -79,7 +79,7 @@ struct AllDifferentSet <: MOI.AbstractVectorSet
7979
dimension :: Int64
8080
end
8181

82-
struct NotEqualSet{T} <: MOI.AbstractScalarSet
82+
struct NotEqualSet{T} <: MOI.AbstractScalarSet
8383
value :: T
8484
end
8585

@@ -134,21 +134,19 @@ end
134134
mutable struct ConstraintSolverModel
135135
init_search_space :: Vector{Variable}
136136
search_space :: Vector{Variable}
137-
subscription :: Vector{Vector{Int}}
137+
subscription :: Vector{Vector{Int}}
138138
constraints :: Vector{Constraint}
139139
bt_infeasible :: Vector{Int}
140140
c_backtrack_idx :: Int
141141
backtrack_vec :: Vector{BacktrackObj}
142142
sense :: MOI.OptimizationSense
143143
objective :: ObjectiveFunction
144144
best_sol :: Int # Objective of the best solution
145-
best_bound :: Int # Overall best bound
145+
best_bound :: Int # Overall best bound
146146
solutions :: Vector{Int} # saves only the id to the BacktrackObj
147147
info :: CSInfo
148148
input :: Dict{Symbol,Any}
149149
logs :: Vector{TreeLogNode}
150-
151-
ConstraintSolverModel() = new()
152150
end
153151

154152
const CoM = ConstraintSolverModel
@@ -167,27 +165,32 @@ include("equal.jl")
167165
include("not_equal.jl")
168166

169167
"""
170-
init()
168+
ConstraintSolverModel()
171169
172-
Initialize the constraint model object
170+
Create the constraint model object.
173171
"""
174-
function init()
175-
com = CoM()
176-
com.constraints = Vector{Constraint}()
177-
com.subscription = Vector{Vector{Int}}()
178-
com.init_search_space = Vector{Variable}()
179-
com.search_space = Vector{Variable}()
180-
com.bt_infeasible = Vector{Int}()
181-
com.c_backtrack_idx = 1
182-
com.sense = MOI.FEASIBILITY_SENSE
183-
com.backtrack_vec = Vector{BacktrackObj}()
184-
com.solutions = Vector{Int}()
185-
com.info = CSInfo(0, false, 0, 0, 0)
186-
com.input = Dict{Symbol, Bool}()
187-
com.logs = Vector{TreeLogNode}()
188-
return com
172+
function ConstraintSolverModel()
173+
ConstraintSolverModel(
174+
Vector{Variable}(), # init_search_space
175+
Vector{Variable}(), # search_space
176+
Vector{Vector{Int}}(), # subscription
177+
Vector{Constraint}(), # constraints
178+
Vector{Int}(), # bt_infeasible
179+
1, # c_backtrack_idx
180+
Vector{BacktrackObj}(), # backtrack_vec
181+
MOI.FEASIBILITY_SENSE, #
182+
NoObjective(), #
183+
0, # best_sol,
184+
0, # best_bound
185+
Vector{Int}(), # solutions
186+
CSInfo(0, false, 0, 0, 0), # info
187+
Dict{Symbol,Any}(), # input
188+
Vector{TreeLogNode}() # logs
189+
)
189190
end
190191

192+
@deprecate init() ConstraintSolverModel()
193+
191194
"""
192195
add_var!(com::CS.CoM, from::Int, to::Int; fix=nothing)
193196
@@ -246,7 +249,7 @@ function fixed_vs_unfixed(search_space, indices)
246249
push!(unfixed_indices, i)
247250
end
248251
end
249-
return fixed_vals, unfixed_indices
252+
return (fixed_vals, unfixed_indices)
250253
end
251254

252255
"""
@@ -438,7 +441,7 @@ function prune!(com::CS.CoM; pre_backtrack=false, all=false, only_once=false, in
438441
else
439442
com.info.pre_backtrack_calls += 1
440443
end
441-
444+
442445
if !feasible
443446
break
444447
end
@@ -535,26 +538,26 @@ function checkout_from_to!(com::CS.CoM, from_idx::Int, to_idx::Int)
535538
from = backtrack_vec[from_idx]
536539
to = backtrack_vec[to_idx]
537540
if to.parent_idx == from.idx
538-
return
541+
return
539542
end
540543
reverse_pruning!(com, from.idx)
541-
544+
542545
prune_steps = Vector{Int}()
543546
# first go to same level if new is higher in the tree
544547
if to.depth < from.depth
545548
depth = from.depth
546549
parent_idx = from.parent_idx
547550
parent = backtrack_vec[parent_idx]
548551
while to.depth < depth
549-
reverse_pruning!(com, parent_idx)
552+
reverse_pruning!(com, parent_idx)
550553
parent = backtrack_vec[parent_idx]
551554
parent_idx = parent.parent_idx
552555
depth -= 1
553556
end
554557
if parent_idx == to.parent_idx
555558
return
556559
else
557-
from = parent
560+
from = parent
558561
end
559562
elseif from.depth < to.depth
560563
depth = to.depth
@@ -567,7 +570,7 @@ function checkout_from_to!(com::CS.CoM, from_idx::Int, to_idx::Int)
567570
depth -= 1
568571
end
569572

570-
573+
571574
to = parent
572575
if backtrack_vec[prune_steps[1]].parent_idx == from.parent_idx
573576
prune!(com, prune_steps)
@@ -578,7 +581,7 @@ function checkout_from_to!(com::CS.CoM, from_idx::Int, to_idx::Int)
578581
# same diff but different parent
579582
# => level up until same parent
580583
while from.parent_idx != to.parent_idx
581-
reverse_pruning!(com, from.parent_idx)
584+
reverse_pruning!(com, from.parent_idx)
582585
from = backtrack_vec[from.parent_idx]
583586

584587
pushfirst!(prune_steps, to.parent_idx)
@@ -617,10 +620,10 @@ function update_best_bound!(backtrack_obj::BacktrackObj, com::CS.CoM, constraint
617620
com.info.backtrack_reverses += 1
618621
return false, false
619622
end
620-
623+
621624
# check best_bound again
622-
# if best bound unchanged => continue pruning
623-
# otherwise try another path but don't close the current
625+
# if best bound unchanged => continue pruning
626+
# otherwise try another path but don't close the current
624627
# -> means open new paths from here even if not pruned til the end
625628
new_bb = get_best_bound(com)
626629
if backtrack_obj.best_bound != new_bb
@@ -639,7 +642,7 @@ Return :Solved or :Infeasible if proven or `:NotSolved` if interrupted by `max_b
639642
function backtrack!(com::CS.CoM, max_bt_steps; sorting=true)
640643
found, ind = get_weak_ind(com)
641644
com.info.backtrack_fixes = 1
642-
645+
643646
pvals = reverse!(values(com.search_space[ind]))
644647
dummy_backtrack_obj = BacktrackObj()
645648
dummy_backtrack_obj.status = :Close
@@ -649,7 +652,7 @@ function backtrack!(com::CS.CoM, max_bt_steps; sorting=true)
649652
backtrack_vec = com.backtrack_vec
650653
push!(backtrack_vec, dummy_backtrack_obj)
651654

652-
# the first solve (before backtrack) has idx 1
655+
# the first solve (before backtrack) has idx 1
653656
num_backtrack_objs = 1
654657
step_nr = 1
655658
for pval in pvals
@@ -679,9 +682,9 @@ function backtrack!(com::CS.CoM, max_bt_steps; sorting=true)
679682
step_nr += 1
680683
# get next open backtrack object
681684
l = 1
682-
685+
683686
# if there is no objective or sorting is set to false
684-
if com.sense == MOI.FEASIBILITY_SENSE || !sorting
687+
if com.sense == MOI.FEASIBILITY_SENSE || !sorting
685688
l = length(backtrack_vec)
686689
backtrack_obj = backtrack_vec[l]
687690
while backtrack_obj.status != :Open
@@ -741,7 +744,7 @@ function backtrack!(com::CS.CoM, max_bt_steps; sorting=true)
741744
ind = backtrack_obj.variable_idx
742745

743746
com.c_backtrack_idx = backtrack_obj.idx
744-
747+
745748
if !started
746749
com.c_backtrack_idx = 0
747750
checkout_from_to!(com, last_backtrack_id, backtrack_obj.idx)
@@ -777,7 +780,7 @@ function backtrack!(com::CS.CoM, max_bt_steps; sorting=true)
777780

778781
further_pruning = true
779782
# first update the best bound (only constraints which have an index in the objective function)
780-
if com.sense != MOI.FEASIBILITY_SENSE
783+
if com.sense != MOI.FEASIBILITY_SENSE
781784
feasible, further_pruning = update_best_bound!(backtrack_obj, com, constraints)
782785

783786
if !feasible
@@ -789,17 +792,17 @@ function backtrack!(com::CS.CoM, max_bt_steps; sorting=true)
789792
if further_pruning
790793
# prune completely start with all that changed by the fix or by updating best bound
791794
feasible = prune!(com)
792-
795+
793796
if !feasible
794797
com.info.backtrack_reverses += 1
795798
continue
796799
end
797800
end
798-
801+
799802

800803
found, ind = get_weak_ind(com)
801804
# no index found => solution found
802-
if !found
805+
if !found
803806
new_sol = get_best_bound(com)
804807
if length(com.solutions) == 0
805808
com.best_sol = new_sol
@@ -809,7 +812,7 @@ function backtrack!(com::CS.CoM, max_bt_steps; sorting=true)
809812
push!(com.solutions, backtrack_obj.idx)
810813
if com.best_sol == com.best_bound
811814
return :Solved
812-
else
815+
else
813816
# set all nodes to :Worse if they can't achieve a better solution
814817
for bo in backtrack_vec
815818
if bo.status == :Open && obj_factor*bo.best_bound >= com.best_sol
@@ -824,12 +827,12 @@ function backtrack!(com::CS.CoM, max_bt_steps; sorting=true)
824827
return :NotSolved
825828
end
826829

827-
830+
828831

829832
if com.input[:logs]
830833
com.logs[backtrack_obj.idx] = log_one_node(com, length(com.search_space), backtrack_obj.idx, step_nr)
831834
end
832-
835+
833836
pvals = reverse!(values(com.search_space[ind]))
834837
last_backtrack_obj = backtrack_vec[last_backtrack_id]
835838
for pval in pvals
@@ -858,7 +861,7 @@ function backtrack!(com::CS.CoM, max_bt_steps; sorting=true)
858861
end
859862
end
860863
if length(com.solutions) > 0
861-
# find one of the best solutions
864+
# find one of the best solutions
862865
sol, sol_id = findmin([backtrack_vec[sol_id].best_bound*obj_factor for sol_id in com.solutions])
863866
backtrack_id = com.solutions[sol_id]
864867
checkout_from_to!(com, last_backtrack_id, backtrack_id)
@@ -901,7 +904,7 @@ function simplify!(com)
901904
end
902905
if b_all_different_sum && b_sum
903906
# for each all_different constraint
904-
# which can be formulated as a sum constraint
907+
# which can be formulated as a sum constraint
905908
# check which sum constraints are completely inside all different
906909
# which are partially inside
907910
# compute inside sum and total sum
@@ -934,7 +937,7 @@ function simplify!(com)
934937
if !haskey(cons_indices_dict, sub_variable_idx)
935938
all_inside = false
936939
push!(outside_indices, sub_variable_idx)
937-
else
940+
else
938941
delete!(cons_indices_dict, sub_variable_idx)
939942
end
940943
end
@@ -949,7 +952,7 @@ function simplify!(com)
949952
break
950953
end
951954
end
952-
955+
953956
# make sure that there are not too many outside indices
954957
if add_sum_constraint && length(outside_indices) < 3
955958
add_constraint!(com, sum(com.search_space[outside_indices]) == total_sum - all_diff_sum)
@@ -993,7 +996,7 @@ function solve!(com::CS.CoM, options::SolverOptions)
993996
if length(com.constraints) == 0
994997
return :Solved
995998
end
996-
999+
9971000
backtrack = options.backtrack
9981001
max_bt_steps = options.max_bt_steps
9991002
backtrack_sorting = options.backtrack_sorting
@@ -1015,7 +1018,7 @@ function solve!(com::CS.CoM, options::SolverOptions)
10151018
if !feasible
10161019
return :Infeasible
10171020
end
1018-
1021+
10191022
if all(v->isfixed(v), com.search_space)
10201023
com.best_bound = get_best_bound(com)
10211024
com.best_sol = com.best_bound
@@ -1028,7 +1031,7 @@ function solve!(com::CS.CoM, options::SolverOptions)
10281031
push!(com.logs, log_one_node(com, length(com.search_space), 1, 1))
10291032
end
10301033

1031-
if !feasible
1034+
if !feasible
10321035
return :Infeasible
10331036
end
10341037

0 commit comments

Comments
 (0)