How to implement a circuit constraint ?

2,064 views
Skip to first unread message

Claudio Cesar de Sa

unread,
Apr 12, 2020, 8:36:35 PM4/12/20
to or-tools-discuss
Hi Everyone:

I am new here, as well as, I am learning Python using the OR-TOOLS as a nice motivation. I did some models
using the OR-TOOLS translating some models from Minizinc or Picat (languages that I am used to work).
Well, at moment I am stopped in constraint such the circuit for a vector. I am trying to write this constraint from scratch,
but without success.

The circuit constraint returns true for a sequence as :  [1, 3, 0, 2] from any index, I will be returning at the starting point
after n steps. This constraint is the kernel for my idea of TSP.

My codes are in: https://github.com/claudiosa/CCS/tree/master/python/or-tools

Another help:

To build another constraint such X <==> Y (Equivalent Relation) I tried something such:

def equivalence_constraint(model , x, y ):
    model.AddImplication(x,y)
    model.AddImplication(y,x)
It does not work ....

Can I use some user functions in models under of OR-TOOLS?

Thanks indeed

I am looking forward to hearing from you


Claudio


Xiang Chen

unread,
Apr 12, 2020, 9:02:26 PM4/12/20
to or-tools...@googlegroups.com
I didn't quite understand what you meant but there is already a circuit constraint:

--
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.
To view this discussion on the web visit https://groups.google.com/d/msgid/or-tools-discuss/2136629c-4bfc-474d-944e-fb82acd99e64%40googlegroups.com.

Hakan Kjellerstrand

unread,
Apr 13, 2020, 7:06:59 AM4/13/20
to or-tools-discuss
Hi Claudio.

For inspiration you can also see my decomposition of circuit which I implemented long time ago: https://github.com/google/or-tools/blob/stable/examples/contrib/circuit.py
It's fairly easy to extend this to also show the path, i.e. not only the circuit.

/Hakan

--
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.
To view this discussion on the web visit https://groups.google.com/d/msgid/or-tools-discuss/2136629c-4bfc-474d-944e-fb82acd99e64%40googlegroups.com.


--

Claudio Cesar de Sa

unread,
Apr 13, 2020, 10:37:18 AM4/13/20
to or-tools-discuss
Thanks Xiang for your ready answer

Really I would like learn to build these constraints such "circuit". Your indication was great, but the code presented
inside of AddCircuit is very incomprehensible (it is in a high-level for me at moment).
In addition this constraints does not work for my list numbers.

I was looking for a solution as presented by Hakan (see below... ), but also I did some customization for CP-SAT.
Anyway I will keep in my studies.

About the equivalence, a <=> b, works if one side is constant, but when the  both side are constraints variable, maybe I need some reyfing approach.
A case like this model.Add( (x[i][j] == 1) == (tour[i] == j)) does not work once, "x" and "tour" are variables ....
It means  (x[i][j] == 1) is true only if (tour[i] == j) is true ... both side are variable.

Well, Xiang, once indeed thanks for your help

Claudio
To unsubscribe from this group and stop receiving emails from it, send an email to or-tools...@googlegroups.com.

Claudio Cesar de Sa

unread,
Apr 13, 2020, 10:48:47 AM4/13/20
to or-tools-discuss

Thanks Hakan

After some days trying, any imprecise progress. I did a circuit verifier/cheker in Python  and tried to convert it to CP-SAT. In this second part I lost again.

I got your code and did minors changes for CP-SAT syntax. It is a function inside of my tsp.py.
For instance:

            # solution: use Element instead
            # solver.Add(z[i] == solver.Element(x, z[i - 1])) 
            ## MODIFIED here by CCS
            solver.AddElement(z[i-1] , x, z[i])      ### NEW

just adapted for CP-SAT syntax.

Anyway, now I have an idea how to implement this constraint. I resume my approach considering yours ideas.

Thanks indeed again

Claudio
To unsubscribe from this group and stop receiving emails from it, send an email to or-tools...@googlegroups.com.

Laurent Perron

unread,
Apr 13, 2020, 4:45:03 PM4/13/20
to or-tools-discuss
Note that it will be 100s of time slower that way.

To unsubscribe from this group and stop receiving emails from it, send an email to or-tools-discu...@googlegroups.com.
To view this discussion on the web visit https://groups.google.com/d/msgid/or-tools-discuss/024bfb2d-9bae-4ca5-add3-16553f3915d2%40googlegroups.com.

Claudio Cesar de Sa

unread,
Apr 15, 2020, 11:53:44 AM4/15/20
to or-tools...@googlegroups.com


Laurent

I agree with your performance view ... but there are some points:

a) In another moment, other specific constraints should be built, and some I want to know, this "how to do it....."
b) In addition, it is frustrating for a beginner (like me) tries something such simple as:

....................................................
    n  = 7  #  number of nodes ( n x n matrix)
    the_model = cp_model.CpModel()
    tour = [ the_model.NewIntVar(0, (n-1), 'tour' ) 
             for i in range(n)
            ]
   the_model.AddCircuit( tour )
.................................................... 
runs it and an answer like:
.................................................... 
 File "study_circuit.py", line 28, in model_CIRCUIT
    the_model.AddCircuit( tour )
  File "/home/ccs/.local/lib/python3.8/site-packages/ortools/sat/python/cp_model.py", line 855, in AddCircuit
    cp_model_helper.AssertIsInt32(arc[0])
TypeError: 'IntVar' object is not subscriptable
.................................................... 
A bad feeling with this answer for a beginner ...

In another CP languages with this constraint is direct, for instance:
....................................
> L =[X1,X2], circuit(L). 
L = [2,1]
...................................

Really, maybe it is fault also ....  but the constraint done by-hand by Hakan works as expected.

Maybe in the manual of OR-TOOLS are missing some small examples to use these constraints.

Anyway, thanks very much

cc







You received this message because you are subscribed to a topic in the Google Groups "or-tools-discuss" group.
To unsubscribe from this topic, visit https://groups.google.com/d/topic/or-tools-discuss/Tv9TSXVSpLg/unsubscribe.
To unsubscribe from this group and all its topics, send an email to or-tools-discu...@googlegroups.com.
To view this discussion on the web visit https://groups.google.com/d/msgid/or-tools-discuss/CABcmEeaEqFBOx4j33uvf-bfNXUTnCLZEBUR%3DJK2NO%3DqXCw_8Kw%40mail.gmail.com.


--
claudio

**********************************************************************
***********************************************************************

Xiang Chen

unread,
Apr 15, 2020, 1:59:06 PM4/15/20
to or-tools...@googlegroups.com
I might be wrong but your examples look more like an AddAllDiferent than a circuit.

Laurent Perron

unread,
Apr 15, 2020, 5:34:02 PM4/15/20
to or-tools-discuss

Claudio Cesar de Sa

unread,
Apr 15, 2020, 6:58:37 PM4/15/20
to or-tools...@googlegroups.com
Laurent

I will be checking these examples ...
Thank you

Claudio

Xiang:

Consider the index starts in 1 up to 5. See some examples:
.....
[2,5,4,1,3]
...............
[3,1,5,2,4]
.......
[5,4,2,1,3]
.............
In N steps you will back to the origin, seems the allDifferent ... it does a cycle....
In Picat it runs directly such: 
Picat> L =[X1,X2,X3,X4,X5], circuit(L), solve(L), writeln(L),  fail.


Claudio

alireza...@gmail.com

unread,
Jul 12, 2023, 6:23:03 AM7/12/23
to or-tools-discuss
Dear Laurent, 
Would it be possible to provide a super basic and easy to understand example of circuit constraint ? 
I have difficulty to understand the concept and application of circuit constraint ?
Thanks in advance 
Alireza

Laurent Perron

unread,
Jul 12, 2023, 12:19:47 PM7/12/23
to or-tools...@googlegroups.com
Reply all
Reply to author
Forward
0 new messages