From 982a9c831df61aa43e74130f76786bfb5c5b3a1e Mon Sep 17 00:00:00 2001 From: azzaare Date: Mon, 15 Nov 2021 18:11:34 +0900 Subject: [PATCH] Compat depenendices --- .gitignore | 2 +- Project.toml | 8 ++-- test/Project.toml | 1 - test/internal.jl | 4 +- test/raw_solver.jl | 108 ++++++++++++++++++++++++++++++++++++++++++--- test/runtests.jl | 5 +-- 6 files changed, 112 insertions(+), 16 deletions(-) diff --git a/.gitignore b/.gitignore index bba0d34..a205db7 100644 --- a/.gitignore +++ b/.gitignore @@ -1,6 +1,6 @@ *.jl.*.cov *.jl.cov -*.jl.mem +*.mem /docs/build/ .gitignore _git2_* diff --git a/Project.toml b/Project.toml index 8b3090c..7f6fb69 100644 --- a/Project.toml +++ b/Project.toml @@ -1,7 +1,7 @@ name = "LocalSearchSolvers" uuid = "2b10edaa-728d-4283-ac71-07e312d6ccf3" authors = ["Jean-Francois Baffier"] -version = "0.3.12" +version = "0.3.13" [deps] CompositionalNetworks = "4b67e4b5-442d-4ef5-b760-3f5df3a57537" @@ -15,9 +15,9 @@ Lazy = "50d2b5c4-7a5e-59d5-8109-a42b560f39c0" OrderedCollections = "bac558e1-5e72-5ebc-8fee-abe8a469f55d" [compat] -CompositionalNetworks = "0.2" -ConstraintDomains = "0.2" -Constraints = "0.2" +CompositionalNetworks = "0.3" +ConstraintDomains = "0.3" +Constraints = "0.3" Dictionaries = "0.3" JSON = "0.21" Lazy = "0.15" diff --git a/test/Project.toml b/test/Project.toml index 56f2d31..a01aebb 100644 --- a/test/Project.toml +++ b/test/Project.toml @@ -1,7 +1,6 @@ [deps] CompositionalNetworks = "4b67e4b5-442d-4ef5-b760-3f5df3a57537" ConstraintDomains = "5800fd60-8556-4464-8d61-84ebf7a0bedb" -ConstraintModels = "841a6ec5-cac3-4c42-9a0a-4b21c9553698" Constraints = "30f324ab-b02d-43f0-b619-e131c61659f7" Dictionaries = "85a47980-9c8c-11e8-2b9f-f7ca1fa99fb4" Distributed = "8ba89e20-285c-5b6f-9357-94700520ee1b" diff --git a/test/internal.jl b/test/internal.jl index 0411b86..b02b26b 100644 --- a/test/internal.jl +++ b/test/internal.jl @@ -73,7 +73,7 @@ end m = model() -describe(m) +# LocalSearchSolvers.describe(m) x1 = variable([4,3,2,1]) x2 = variable(d2) @@ -116,7 +116,7 @@ objs = Dictionary(1:2, [o1, o2]) add_value!(m, 1, 1) delete_var_from_cons!(m, 1, 1) add_var_to_cons!(m, 1, 1) - describe(m) + # describe(m) end ## Test Solver diff --git a/test/raw_solver.jl b/test/raw_solver.jl index 0939ee3..4c8e00e 100644 --- a/test/raw_solver.jl +++ b/test/raw_solver.jl @@ -1,10 +1,108 @@ +function mincut(graph; source, sink, interdiction =0) + m = model(; kind=:cut) + n = size(graph, 1) + + d = domain(0:n) + + separator = n + 1 # value that separate the two sides of the cut + + # Add variables: + foreach(_ -> variable!(m, d), 0:n) + + # Extract error function from usual_constraint + e1 = (x; param=nothing, dom_size=n + 1) -> error_f( + usual_constraints[:ordered])(x; param, dom_size + ) + e2 = (x; param=nothing, dom_size=n + 1) -> error_f( + usual_constraints[:all_different])(x; param, dom_size + ) + + # Add constraint + constraint!(m, e1, [source, separator, sink]) + constraint!(m, e2, 1:(n + 1)) + + # Add objective + objective!(m, (x...) -> o_mincut(graph, x...; interdiction)) + + return m +end + +function golomb(n, L = n^2) + m = model(; kind=:golomb) + + # Add variables + d = domain(0:L) + foreach(_ -> variable!(m, d), 1:n) + + # Extract error function from usual_constraint + e1 = (x; param=nothing, dom_size=n) -> error_f( + usual_constraints[:all_different])(x; param, dom_size + ) + e2 = (x; param=nothing, dom_size=n) -> error_f( + usual_constraints[:all_equal_param])(x; param, dom_size + ) + e3 = (x; param=nothing, dom_size=n) -> error_f( + usual_constraints[:dist_different])(x; param, dom_size + ) + + # # Add constraints + constraint!(m, e1, 1:n) + constraint!(m, x -> e2(x; param=0), 1:1) + for i in 1:(n - 1), j in (i + 1):n, k in i:(n - 1), l in (k + 1):n + (i, j) < (k, l) || continue + constraint!(m, e3, [i, j, k, l]) + end + + # Add objective + objective!(m, o_dist_extrema) + + return m +end + +function sudoku(n; start=nothing) + N = n^2 + d = domain(1:N) + + m = model(;kind=:sudoku) + + # Add variables + if isnothing(start) + foreach(_ -> variable!(m, d), 1:(N^2)) + else + foreach(((x, v),) -> variable!(m, 1 ≤ v ≤ N ? domain(v) : d), pairs(start)) + end + + + e = (x; param=nothing, dom_size=N) -> error_f( + usual_constraints[:all_different])(x; param=param, dom_size=dom_size + ) + + # Add constraints: line, columns; blocks + foreach(i -> constraint!(m, e, (i * N + 1):((i + 1) * N)), 0:(N - 1)) + foreach(i -> constraint!(m, e, [j * N + i for j in 0:(N - 1)]), 1:N) + + for i in 0:(n - 1) + for j in 0:(n - 1) + vars = Vector{Int}() + for k in 1:n + for l in 0:(n - 1) + push!(vars, (j * n + l) * N + i * n + k) + end + end + constraint!(m, e, vars) + end + end + + return m +end + @testset "Raw solver: internals" begin models = [ - sudoku(2; modeler=:raw), + sudoku(2), ] for m in models - @info describe(m) + # @info describe(m) s = solver(m; options=Options(print_level=:verbose, time_limit = Inf, iteration=Inf, info_path="info.json")) for x in keys(get_variables(s)) @test get_name(s, x) == "x$x" @@ -60,14 +158,14 @@ end 0 7 0 0 0 0 0 5 3 ])) - s = solver(sudoku(3; start = sudoku_instance, modeler = :raw); options = Options(print_level = :minimal, iteration = Inf, time_limit = 10)) + s = solver(sudoku(3; start = sudoku_instance); options = Options(print_level = :minimal, iteration = Inf, time_limit = 10)) display(Dictionary(1:length(sudoku_instance), sudoku_instance)) solve!(s) display(solution(s)) end @testset "Raw solver: golomb" begin - s = solver(golomb(5, modeler = :raw); options = Options(print_level = :minimal, iteration = 1000)) + s = solver(golomb(5); options = Options(print_level = :minimal, iteration = 1000)) solve!(s) @info "Results golomb!" @@ -99,7 +197,7 @@ end @info "Sol (vals): $(!isnothing(best_value(s)) ? best_values(s) : nothing)" s = solver(mincut(graph, source=1, sink=5, interdiction=2); options = Options(print_level=:minimal, time_limit = 15, iteration=Inf)) - @info describe(s) + # @info describe(s) solve!(s) @info "Results 2-mincut!" @info "Values: $(get_values(s))" diff --git a/test/runtests.jl b/test/runtests.jl index 4841a3c..fda6569 100644 --- a/test/runtests.jl +++ b/test/runtests.jl @@ -1,13 +1,12 @@ using Distributed # Add a process with two threads -addprocs(1; exeflags = "-t 2") +# addprocs(1; exeflags = ["-t 2", "--project"]) # addprocs(1) import ConstraintDomains import CompositionalNetworks -using ConstraintModels -using Constraints +@everywhere using Constraints using Dictionaries @everywhere using LocalSearchSolvers using Test