Skip to content

Commit

Permalink
Misc fixes
Browse files Browse the repository at this point in the history
  • Loading branch information
bokner committed Nov 28, 2024
1 parent edf9ee2 commit 20c90d1
Show file tree
Hide file tree
Showing 5 changed files with 25 additions and 8 deletions.
2 changes: 1 addition & 1 deletion lib/solver/core/propagator/constraint_graph.ex
Original file line number Diff line number Diff line change
Expand Up @@ -44,7 +44,7 @@ defmodule CPSolver.Propagator.ConstraintGraph do
constraint_graph,
variable_id,
domain_change
) do
) when is_atom(domain_change) do
propagators_by_variable(constraint_graph, variable_id, fn p_id, propagator_variable_edge ->
domain_change in propagator_variable_edge.propagate_on &&
get_propagator_data(
Expand Down
4 changes: 4 additions & 0 deletions lib/solver/core/propagator/propagator.ex
Original file line number Diff line number Diff line change
Expand Up @@ -288,6 +288,10 @@ defmodule CPSolver.Propagator do
Arrays.get(args, pos)
end

def arg_map(%{args: args} = _propagator, mapper) do
arg_map(args, mapper)
end

def arg_map(args, mapper) when is_function(mapper) and is_list(args) do
Enum.map(args, mapper)
end
Expand Down
2 changes: 2 additions & 0 deletions lib/solver/search/strategy/variable/variable_selector.ex
Original file line number Diff line number Diff line change
Expand Up @@ -202,6 +202,8 @@ defmodule CPSolver.Search.VariableSelector do
break_even_fun.(selection, data)
end

def variable_choice(strategy_fun, break_even_fun \\ &Enum.random/1)

def variable_choice(strategy_fun, break_even_fun) when is_function(strategy_fun) do
fn vars, data ->
vars
Expand Down
12 changes: 12 additions & 0 deletions test/constraints/or_test.exs
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ defmodule CPSolverTest.Constraint.Or do
alias CPSolver.BooleanVariable
alias CPSolver.Model
alias CPSolver.Constraint.Or
import CPSolver.Variable.View.Factory

describe "Or constraint" do
test "`or` functionality" do
Expand All @@ -30,6 +31,17 @@ defmodule CPSolverTest.Constraint.Or do
assert result.status == :unsatisfiable
end

test "with negation vars" do
x = BooleanVariable.new(name: "x")
not_y = negation(BooleanVariable.new(name: "y"))
vars = [x, not_y]
or_constraint = Or.new(vars)
model = Model.new(vars, [or_constraint])

{:ok, result} = CPSolver.solve(model)
assert [0, 0] in result.solutions
end

test "peformance" do
n = 1000
bool_vars = Enum.map(1..n, fn i -> BooleanVariable.new(name: "b#{i}") end)
Expand Down
13 changes: 6 additions & 7 deletions test/space/space_propagation_test.exs
Original file line number Diff line number Diff line change
Expand Up @@ -10,11 +10,10 @@ defmodule CPSolverTest.SpacePropagation do
%{
propagators: _propagators,
variables: [x, y, z] = _variables,
constraint_graph: graph,
store: store
constraint_graph: graph
} = stable_setup()

:solved = Propagation.run(graph, store)
:solved = Propagation.run(graph, %{})

assert Variable.fixed?(x) && Variable.fixed?(z)
## Check not_equal(x, z)
Expand All @@ -28,17 +27,17 @@ defmodule CPSolverTest.SpacePropagation do
end

test "Propagation on solvable space" do
%{variables: variables, constraint_graph: graph, store: store} =
%{variables: variables, constraint_graph: graph} =
solved_setup()

refute Enum.all?(variables, fn var -> Variable.fixed?(var) end)
assert :solved == Propagation.run(graph, store)
assert :solved == Propagation.run(graph, %{})
assert Enum.all?(variables, fn var -> Variable.fixed?(var) end)
end

test "Propagation on failed space" do
%{constraint_graph: graph, store: store} = fail_setup()
assert {:fail, _propagator_id} = Propagation.run(graph, store)
%{constraint_graph: graph} = fail_setup()
assert {:fail, _propagator_id} = Propagation.run(graph, %{})
end

defp stable_setup() do
Expand Down

0 comments on commit 20c90d1

Please sign in to comment.