How to declare dynamic variables inside FOR loop in OR-TOOLS

rapha123

I'm getting started with OR-TOOLS from Google and I couldn't figure out how to declare variables dynamically.

A very simple example in OR-TOOLS websites in given as follows

x = solver.NumVar(0, 10, 'x')
y = solver.NumVar(0, 10, 'y')
solver.Add(x + 7 * y <= 17.5)
solver.Maximize(x + 10 * y)`

Because I am modelling a Benders decomposition, I'm trying to declare some variables inside a loop. Thus, I could dynamically create its variables.

I've tried something like:

for i in range(3):
    x[i] = solver.NumVar(0, 10, 'x[i]')
    y[i] = solver.IntVar(0, 10, 'y[i]')
    solver.Add(x[i] + 7 * y[i] <= 17.5)

However, that clearly doesn't work.

Could someone help me please? Thanks!

sascha

This approach surely works and is probably shown in lots of examples.

Without testing, your idea could look like:

# prepare some data-structure to hold variables returned
x = [None] * 3
y = [None] * 3

for i in range(3):
    x[i] = solver.NumVar(0, 10, 'x[{}]'.format(i))
    y[i] = solver.IntVar(0, 10, 'y[{}]'.format(i))
    solver.Add(x[i] + 7 * y[i] <= 17.5)  # invariant is important!
                                         # we only access available indices

Nothing wrong with that conceptionally.

Your variables will now be ready to be used / consumed by access your lists.

Collected from the Internet

Please contact [email protected] to delete if infringement.

edited at
0

Comments

0 comments
Login to comment

Related

JavaScript variables declare outside or inside loop?

Jmeter - How to assign multiple dynamic values to a variables inside a loop

How to declare multiple variables inside IF statement blocks?

Create dynamic scope variables in AngularJs inside loop

Dynamic created variables inside for loop, Javascript

How to declare variables based on a struct in a for-loop

How to declare variable types for loop variables in Go?

How to declare multiple variables using a loop?

Javascript Set Dynamic Variables inside a For Loop Within addEventListener

How to assign dynamic variables in a for loop in python

How to declare unlimited variables in a loop depending on user input?

How can I declare constant variables in a loop using the increment in the variable?

Declare an object inside or outside a loop?

how to assign multiple localstorage variables inside a loop?

how to save a value inside a loop and add on it variables?

Dynamically declare and assign variables in a loop

How to declare variables in dataclasses

Dynamic Variables in loop

Initialising variables inside for loop

Variables counting inside Loop

MySQL How to use dynamic PHP variables inside INSERT INTO SELECT query?

Declare variables and increase that inside Django templates

Declare bash variables inside sql EOF

declare variables with $this keyword inside mouseover/mouseout events

SQL Server : declare variables inside FROM clause

How do I declare a two dimensional dynamic array inside of a Class in C++

How do I create dynamic variable names inside a loop?

how to create dynamic variable name inside a for loop in sql

How to add draggable() with dynamic div being created inside loop?