Model info/remove constraints

182 views
Skip to first unread message

Eric

unread,
Feb 15, 2016, 12:31:11 PM2/15/16
to or-tools-discuss
Hi,

I am working with c++ API (not the command line flags) using the latest version of CP solver in windows. I have a few questions regarding my effort to use or-tools as follows:
1. How can I get some statistics (number of variables, constraints, ...) on the model itself? The only thing that I have found is Constraints() method from the solver class to retrieve the number of constraints in the model.
2. How can I export the model in a readable format just for debug purpose not re-use it again?
3. How can I remove a variable or constraint from the model?


Thank you so much.

Eric

Laurent Perron

unread,
Feb 15, 2016, 2:36:49 PM2/15/16
to or-tools-discuss
Hi

There are a few visitors on the model
Take one C++ example (golomb for example)

/bin/golomb --cp_model_stats --size=6 --log_prefix=false

Model has:

  - 11 constraints.

    * 1 AllDifferent

    * 10 CastExpressionIntoVariable

  - 16 integer variables.

  - 10 integer expressions.

    * 10 Difference

  - 10 expressions casted into variables.

  - 0 interval variables.

  - 0 sequence variables.

  - 2 model extensions.

    * 1 VariableGroup

    * 1 Objective

N = 6, optimal length = 17 (fails:8)



and


./bin/golomb --cp_print_model --size=6 --log_prefix=false

Model golomb {

  CastExpressionIntoVariable

    expression: Difference

        left: X02(1..217)

        right: X01(1..217)

    target_variable: Difference

        left: X02(1..217)

        right: X01(1..217)

  CastExpressionIntoVariable

    expression: Difference

        left: X03(1..217)

        right: X01(1..217)

    target_variable: Difference

        left: X03(1..217)

        right: X01(1..217)

  CastExpressionIntoVariable

    expression: Difference

        left: X04(1..217)

        right: X01(1..217)

    target_variable: Difference

        left: X04(1..217)

        right: X01(1..217)

  CastExpressionIntoVariable

    expression: Difference

        left: X05(1..217)

        right: X01(1..217)

    target_variable: Difference

        left: X05(1..217)

        right: X01(1..217)

  CastExpressionIntoVariable

    expression: Difference

        left: X03(1..217)

        right: X02(1..217)

    target_variable: Difference

        left: X03(1..217)

        right: X02(1..217)

  CastExpressionIntoVariable

    expression: Difference

        left: X04(1..217)

        right: X02(1..217)

    target_variable: Difference

        left: X04(1..217)

        right: X02(1..217)

  CastExpressionIntoVariable

    expression: Difference

        left: X05(1..217)

        right: X02(1..217)

    target_variable: Difference

        left: X05(1..217)

        right: X02(1..217)

  CastExpressionIntoVariable

    expression: Difference

        left: X04(1..217)

        right: X03(1..217)

    target_variable: Difference

        left: X04(1..217)

        right: X03(1..217)

  CastExpressionIntoVariable

    expression: Difference

        left: X05(1..217)

        right: X03(1..217)

    target_variable: Difference

        left: X05(1..217)

        right: X03(1..217)

  CastExpressionIntoVariable

    expression: Difference

        left: X05(1..217)

        right: X04(1..217)

    target_variable: Difference

        left: X05(1..217)

        right: X04(1..217)

  AllDifferent

    variables: [

      X01(1..217)

      X02(1..217)

      X03(1..217)

      X04(1..217)

      X05(1..217)

      Difference

        left: X02(1..217)

        right: X01(1..217)

      Difference

        left: X03(1..217)

        right: X01(1..217)

      Difference

        left: X04(1..217)

        right: X01(1..217)

      Difference

        left: X05(1..217)

        right: X01(1..217)

      Difference

        left: X03(1..217)

        right: X02(1..217)

      Difference

        left: X04(1..217)

        right: X02(1..217)

      Difference

        left: X05(1..217)

        right: X02(1..217)

      Difference

        left: X04(1..217)

        right: X03(1..217)

      Difference

        left: X05(1..217)

        right: X03(1..217)

      Difference

        left: X05(1..217)

        right: X04(1..217)

    ]

    range: 1

  Objective

    maximize: 0

    step: 1

    expression: X05(1..217)

  VariableGroup

    variables: [

      IntConst(0)

      X01(1..217)

      X02(1..217)

      X03(1..217)

      X04(1..217)

      X05(1..217)

    ]

}

N = 6, optimal length = 17 (fails:8)

You check the code behind these flags (src/constraint_solver/constraint_solver.cc).

You create the relevant visitor, and then run solver.Accept(visitor) after the model is created.


You can also see 


./bin/golomb --cp_print_added_constraints --size=6 --log_prefix=false

cast((X02(1..217) - X01(1..217)), CastVar<0>(-216..216))

cast((X03(1..217) - X01(1..217)), CastVar<1>(-216..216))

cast((X04(1..217) - X01(1..217)), CastVar<2>(-216..216))

cast((X05(1..217) - X01(1..217)), CastVar<3>(-216..216))

cast((X03(1..217) - X02(1..217)), CastVar<4>(-216..216))

cast((X04(1..217) - X02(1..217)), CastVar<5>(-216..216))

cast((X05(1..217) - X02(1..217)), CastVar<6>(-216..216))

cast((X04(1..217) - X03(1..217)), CastVar<7>(-216..216))

cast((X05(1..217) - X03(1..217)), CastVar<8>(-216..216))

cast((X05(1..217) - X04(1..217)), CastVar<9>(-216..216))

BoundsAllDifferent(X01(1..217), X02(1..217), X03(1..217), X04(1..217), X05(1..217), CastVar<0>(1..216), CastVar<1>(1..216), CastVar<2>(1..216), CastVar<3>(1..216), CastVar<4>(1..216), CastVar<5>(1..216), CastVar<6>(1..216), CastVar<7>(1..216), CastVar<8>(1..216), CastVar<9>(1..216))

N = 6, optimal length = 17 (fails:8)


which is often more useful with the --cp_name_cast_variables


./bin/golomb --cp_print_added_constraints --size=6 --log_prefix=false --cp_name_cast_variables

cast((X02(1..217) - X01(1..217)), Var<(X02(1..217) - X01(1..217))>(-216..216))

cast((X03(1..217) - X01(1..217)), Var<(X03(1..217) - X01(1..217))>(-216..216))

cast((X04(1..217) - X01(1..217)), Var<(X04(1..217) - X01(1..217))>(-216..216))

cast((X05(1..217) - X01(1..217)), Var<(X05(1..217) - X01(1..217))>(-216..216))

cast((X03(1..217) - X02(1..217)), Var<(X03(1..217) - X02(1..217))>(-216..216))

cast((X04(1..217) - X02(1..217)), Var<(X04(1..217) - X02(1..217))>(-216..216))

cast((X05(1..217) - X02(1..217)), Var<(X05(1..217) - X02(1..217))>(-216..216))

cast((X04(1..217) - X03(1..217)), Var<(X04(1..217) - X03(1..217))>(-216..216))

cast((X05(1..217) - X03(1..217)), Var<(X05(1..217) - X03(1..217))>(-216..216))

cast((X05(1..217) - X04(1..217)), Var<(X05(1..217) - X04(1..217))>(-216..216))

BoundsAllDifferent(X01(1..217), X02(1..217), X03(1..217), X04(1..217), X05(1..217), Var<(X02(1..217) - X01(1..217))>(1..216), Var<(X03(1..217) - X01(1..217))>(1..216), Var<(X04(1..217) - X01(1..217))>(1..216), Var<(X05(1..217) - X01(1..217))>(1..216), Var<(X03(1..217) - X02(1..217))>(1..216), Var<(X04(1..217) - X02(1..217))>(1..216), Var<(X05(1..217) - X02(1..217))>(1..216), Var<(X04(1..217) - X03(1..217))>(1..216), Var<(X05(1..217) - X03(1..217))>(1..216), Var<(X05(1..217) - X04(1..217))>(1..216))

N = 6, optimal length = 17 (fails:8)


All these flags are also available as parameters to the solver (see src/constraint_solver/solver_parameters.proto

I hope this helps.


--
You received this message because you are subscribed to the Google Groups "or-tools-discuss" group.
To unsubscribe from this group and stop receiving emails from it, send an email to or-tools-discu...@googlegroups.com.
For more options, visit https://groups.google.com/d/optout.

Eric

unread,
Feb 16, 2016, 3:26:37 AM2/16/16
to or-tools-discuss
Thanks Laurent for your prompt answer.

I have got how to get the model stats, but I am still do not know how to remove a variable or constraint from the model?

Cheers,
Eric

louis Ucheya

unread,
Mar 2, 2016, 4:25:24 PM3/2/16
to or-tools-discuss
Hi Eric,

Did you ever figure out how to remove a variable or constraint the model?

Thanks,
Louis

Eric

unread,
Mar 2, 2016, 5:35:45 PM3/2/16
to or-tools-discuss
Hi Louis,

No. I haven't got any reply back so far regrading removing a variable or constraint.

Thanks,
Eric
Reply all
Reply to author
Forward
0 new messages