jump-dev / MiniZinc.jl

A Julia interface to the MiniZinc constraint modeling language
https://www.minizinc.org/
MIT License
18 stars 4 forks source link

Add more support for logical operators #22

Closed odow closed 1 year ago

odow commented 1 year ago

The demo is

julia> using JuMP

julia> import MiniZinc

julia> function register_symbol(model, sym, n)
           function f(args...)
               error(
                   "Unable to evaluate operator $(sym) with $(n) arguments because " *
                   "it was registered as a symbolic function only. Use `register` " * 
                   "to pass callbacks to evaluate the function and its derivatives.",
               )
           end
           register(model, sym, n, f, f, f)
           return
       end
register_symbol (generic function with 1 method)

julia> model = Model(() -> MiniZinc.Optimizer{Int}(MiniZinc.Chuffed()))
A JuMP Model
Feasibility problem with:
Variables: 0
Model mode: AUTOMATIC
CachingOptimizer state: EMPTY_OPTIMIZER
Solver name: MiniZinc

julia> @variable(model, x, Bin)
x

julia> @variable(model, y, Bin)
y

julia> @variable(model, z, Bin)
z

julia> register_symbol(model, :(=>), 2)

julia> register_symbol(model, :(!), 1)

julia> @NLconstraint(model, (z => !(x || y)) == true)
(z => !(x || y)) - 1.0 = 0

julia> optimize!(model)
Warning: included file "count.mzn" overrides a global constraint file from the standard library. This is deprecated. For a solver-specific redefinition of a global constraint, override "fzn_<global>.mzn" instead.

julia> value.([x, y, z])
3-element Vector{Float64}:
 1.0
 1.0
 0.0

x-ref: https://github.com/jump-dev/JuMP.jl/issues/2227#issuecomment-1405993222

codecov[bot] commented 1 year ago

Codecov Report

Base: 95.15% // Head: 95.19% // Increases project coverage by +0.04% :tada:

Coverage data is based on head (953a2d0) compared to base (56f1743). Patch coverage: 100.00% of modified lines in pull request are covered.

Additional details and impacted files ```diff @@ Coverage Diff @@ ## master #22 +/- ## ========================================== + Coverage 95.15% 95.19% +0.04% ========================================== Files 3 3 Lines 454 458 +4 ========================================== + Hits 432 436 +4 Misses 22 22 ``` | [Impacted Files](https://codecov.io/gh/jump-dev/MiniZinc.jl/pull/22?src=pr&el=tree&utm_medium=referral&utm_source=github&utm_content=comment&utm_campaign=pr+comments&utm_term=jump-dev) | Coverage Δ | | |---|---|---| | [src/write.jl](https://codecov.io/gh/jump-dev/MiniZinc.jl/pull/22?src=pr&el=tree&utm_medium=referral&utm_source=github&utm_content=comment&utm_campaign=pr+comments&utm_term=jump-dev#diff-c3JjL3dyaXRlLmps) | `98.60% <100.00%> (+0.01%)` | :arrow_up: | Help us with your feedback. Take ten seconds to tell us [how you rate us](https://about.codecov.io/nps?utm_medium=referral&utm_source=github&utm_content=comment&utm_campaign=pr+comments&utm_term=jump-dev). Have a feature suggestion? [Share it here.](https://app.codecov.io/gh/feedback/?utm_medium=referral&utm_source=github&utm_content=comment&utm_campaign=pr+comments&utm_term=jump-dev)

:umbrella: View full report at Codecov.
:loudspeaker: Do you have feedback about the report comment? Let us know in this issue.

chriscoey commented 1 year ago

As I commented on over here, the register_symbol in your demo above is not so convenient.

odow commented 1 year ago

I think we could have it so that MiniZinc automatically registers those

chriscoey commented 1 year ago

Yes, but I mean solver wrappers shouldn't have to write things like

f(args...) = error("evaluation not supported")
MOI.Nonlinear.register_operator(nlp, :!, 1, f, f, f)

rather it should be possible to write just something simple like

MOI.Nonlinear.register_operator(nlp, :!, 1)