Use IntVar as index to get data from Python Array?

1,423 views
Skip to first unread message

Jess TKD

unread,
Dec 1, 2019, 9:54:52 AM12/1/19
to or-tools-discuss
Hi everyone!

I have stumbled across an issue that I have not been able to solve. I am using the CP-SAT Solver for Python, and trying to access data stored in a Python array. However, the index of the data I am trying to get depends on an IntVar variable.

The C++ version of the solver seem to have the right function (MakeElement), as well as the routing solver (with var.IndexOf(array) ).

MakeElement :

IndexOf(array) : 


So if I am not mistaken, both take a list values and and IntVar index as argument, and return values[index].

Is there any way to get the same with the CP Sat solver? I have looked at every function but none of these seem to do the right job.

If you want more information about the problem or the Data Structure I can give more detail.

Thank you for reading, and for your answers!

Xiang Chen

unread,
Dec 1, 2019, 11:03:13 AM12/1/19
to or-tools-discuss

Jess TKD

unread,
Dec 1, 2019, 12:08:38 PM12/1/19
to or-tools-discuss
Unfortunately it doesn't, AddElement is about giving to an IntVar the value of a given data in a table, using an index that is an Int. I'm looking to find a value using an IntVar

Laurent Perron

unread,
Dec 1, 2019, 12:39:41 PM12/1/19
to or-tools-discuss
Add element takes an array of variables

--
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/65283ddd-c761-4135-81fc-9fc02f9d8689%40googlegroups.com.

Jess TKD

unread,
Dec 1, 2019, 3:10:05 PM12/1/19
to or-tools...@googlegroups.com
Tell me if I'm wrong : AddElement takes an array values, an index ind that is an Int and an IntVar target, and constrains so that values[ind] == target right?

But in that case ind is "just" an Int, so it cannot do what I want...

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/GgZq5ec9tzA/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/CAPyyUTukaeX9RG9Vbqogc-2JBGzNDmp4LV1nmmHP1vY9U%2BsQ%2BQ%40mail.gmail.com.

Xiang Chen

unread,
Dec 1, 2019, 3:43:58 PM12/1/19
to or-tools...@googlegroups.com

Laurent Perron

unread,
Dec 1, 2019, 7:14:39 PM12/1/19
to or-tools-discuss
The array can be an array of intvars.


If they are constants, fixed variables will be created on the fly. The constraint is always stored with an array of variables. 

Christopher Hamkins

unread,
Dec 2, 2019, 2:47:33 AM12/2/19
to or-tools-discuss
Hello Jess,

if I understand your requirement properly, you have a known 1-dimensional array of int data, and you want to pick a value from it depending on an IntVar index that is determined during the solution. 

If that is correct, another alternative would be to make a 2 x n array whose first column contains the values 0, 1, 2, 3, etc. and is to be attached to the IntVar index, and the second column with your data which will be attached to the IntVar receiving the data. Then you could use "AddAllowedAssignments" with the two IntVars in the vector and your data array as the tuples. That might introduce fewer variables in total.

Laurent Perron

unread,
Dec 2, 2019, 4:54:40 AM12/2/19
to or-tools-discuss
This works, but the AddElement(index_var, var_array, target_var) already exists.

The table part is useful to encode 2 dimensional elements though.
Laurent Perron | Operations Research | lpe...@google.com | (33) 1 42 68 53 00



--
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.

Jess TKD

unread,
Dec 2, 2019, 10:06:41 AM12/2/19
to or-tools-discuss
Ok so in the case I described it could solve the issue.

I have simplified the way I defined my problem, because my first query was to know whether an IntVar could be used as an index for an array in Python, and the answer is yes, which is already great.


Now, back to my actual problem: 

The goal is to schedule tasks on cores, with some fixed constrains (period, jitter, offset) and try to maximize the number of chains that can appear. find the period(j), that is itself a table of jobs (so period(j) = [job(j,1), job(j,2), job(j,3)] , that are defined as follows : job(j,i) = [OptionnalIntervalVar(start,size,end,is_present) , [start, size, end, is_present]] ;  my goal was therefore to store the period(j), a table, so it could not be an IntVar as it is the case with AddElement.

So according to what Christopher and Laurent answered, I have now changed the way some of my data is stored: 

Now, each task is linked to [job1, job2, job3] (the number of possible job/period is fixed), and job(i) is itself a table of [starts, ends, actives], where starts are all the IntVars representing the start of the jobs i (so start(i,j) will be the start of the job(i) for the jth period), and so on. That way, I can use AddElement() to link a period j to all the data of the jobs contained by this period, by creating (3 times the number of jobs) vars start, size and end, and linking these to the IntVar stored in the desribed table, using AddElement. (Hope this is understandable, I did my best here).

Thank you a lot for your answers, you helped me a lot by explaining which function can handle what type!
 
Maybe the way I solved this problem is not the simplest one (probably because of the data structure I used in the beginning, so I think I will have to simplify that), but I'll leave it here, in case someone else is trying to do some task scheduling with preemption and chains of tasks.



Le dimanche 1 décembre 2019 21:43:58 UTC+1, Xiang Chen a écrit :
ind can also be a Intvar

On Sun, 1 Dec 2019 at 21:10, Jess TKD <jss...@gmail.com> wrote:
Tell me if I'm wrong : AddElement takes an array values, an index ind that is an Int and an IntVar target, and constrains so that values[ind] == target right?

But in that case ind is "just" an Int, so it cannot do what I want...

Le dim. 1 déc. 2019 à 18:39, Laurent Perron <lauren...@gmail.com> a écrit :
Add element takes an array of variables

Le dim. 1 déc. 2019 à 18:08, Jess TKD <jss...@gmail.com> a écrit :
Unfortunately it doesn't, AddElement is about giving to an IntVar the value of a given data in a table, using an index that is an Int. I'm looking to find a value using an IntVar

Le dimanche 1 décembre 2019 17:03:13 UTC+1, Xiang Chen a écrit :
Does AddElement achieve what you want?
https://github.com/google/or-tools/blob/master/ortools/sat/doc/reference.md#addelement

On Sunday, 1 December 2019 15:54:52 UTC+1, Jess TKD wrote:
Hi everyone!

I have stumbled across an issue that I have not been able to solve. I am using the CP-SAT Solver for Python, and trying to access data stored in a Python array. However, the index of the data I am trying to get depends on an IntVar variable.

The C++ version of the solver seem to have the right function (MakeElement), as well as the routing solver (with var.IndexOf(array) ).

MakeElement :

IndexOf(array) : 


So if I am not mistaken, both take a list values and and IntVar index as argument, and return values[index].

Is there any way to get the same with the CP Sat solver? I have looked at every function but none of these seem to do the right job.

If you want more information about the problem or the Data Structure I can give more detail.

Thank you for reading, and for your answers!

--
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...@googlegroups.com.

--
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/GgZq5ec9tzA/unsubscribe.
To unsubscribe from this group and all its topics, send an email to or-tools...@googlegroups.com.

--
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...@googlegroups.com.
Reply all
Reply to author
Forward
0 new messages