Re: [Gurobi] How to implement following two constraints in Python and Gurobi?

4,353 views
Skip to first unread message
Message has been deleted
Message has been deleted

Renan Garcia

unread,
Jun 15, 2017, 3:56:45 PM6/15/17
to gur...@googlegroups.com
One option would be to store your variables in a tupledict (see http://www.gurobi.com/documentation/7.0/refman/py_tupledict.html) with something like:


# define list of indices [0,...,I]
N = range(I + 1)

# define list of tuples for valid index pairs
pairs = [(i,j) for i in N for j in N if i != j]

# add variables for valid index pairs
x = model.addVars(pairs, vtype=GRB.BINARY, name='x')

# add constraints
model.addConstrs(x.sum(i, '*') == 1 for i in N)
model.addConstrs(x.sum('*', j) == 1 for j in N)


For more possibilities, have a look at the examples at http://www.gurobi.com/resources/examples/example-models-overview

On Jun 15, 2017, at 2:06 PM, Nitesh Sood <nites...@gmail.com> wrote:


Can anybody help me with the following constraint:



scenarios = [1, 2, 3, 4, 5]   this is "I"

scenarios_2 = len(scenarios) + 1 this is "I + 1"



#Currently the decision variables looks like this for the x

    x_decisions = {}
    x_matrix = []
    for i in range(scenarios_2):
        i_column = []
        for j in range(scenarios_2):
            if i != j:
                x_decisions[i] = model.addVar(vtype = GRB.BINARY, name = str(i))
                x_column.append(x_decisions[i])
            else:
                x_decisions[i] = 0
                x_column.append(x_decisions[i])
        x_matrix.append(y_column)





#Currently the first constraint looks like this:

    for j in range(scenarios_2):
        for i in range(scenarios_2):
            if j != i:
                model.addConstr(
                    quicksum(y_matrix[j][i] for s in range(scenarios)) == 1, name = ("first_constraint")

I have difficulties to understand how to implement the above mentioned constraint.

--

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

Edgar Alan Avila Gomez

unread,
Jun 15, 2017, 4:04:35 PM6/15/17
to gur...@googlegroups.com
For i in....
M.addconstr(quicksum([x[I,j] For j in ... If i != j]) == 1) 

For j in....
...............


El jun. 15, 2017 1:52 PM, "Nitesh Sood" <nites...@gmail.com> escribió:

Decision variable matrix should look like the picture above. Other option is not to add zeros, thus then matrix would be I*(I+1), right? Would that work? I am really confused how to implement the decisions variables and constraints in this case. This is the last step in my model.


--

---
You received this message because you are subscribed to the Google Groups "Gurobi Optimization" group.
To unsubscribe from this group and stop receiving emails from it, send an email to gurobi+unsubscribe@googlegroups.com.
Message has been deleted

Edgar Alan Avila Gomez

unread,
Jun 16, 2017, 10:41:30 AM6/16/17
to gur...@googlegroups.com
You miss the symbol [] and yes it's work and only sum when the condition is True

El jun. 16, 2017 8:38 AM, "Nitesh Sood" <nites...@gmail.com> escribió:
Hello,

Thank you for the help.
I'd like to ask does this work?
for i in range(scenarios_2):
     model.addConstr(
           quicksum(x_decisions_variables[i][j] for j in range(scenarios_2) if i != j ) == 1)
I mean that i != j thing in the end?



torstai 15. kesäkuuta 2017 23.04.35 UTC+3 Edgar Alan Avila Gomez kirjoitti:
For i in....
M.addconstr(quicksum([x[I,j] For j in ... If i != j]) == 1) 

For j in....
...............

El jun. 15, 2017 1:52 PM, "Nitesh Sood" <nites...@gmail.com> escribió:

Decision variable matrix should look like the picture above. Other option is not to add zeros, thus then matrix would be I*(I+1), right? Would that work? I am really confused how to implement the decisions variables and constraints in this case. This is the last step in my model.


--

---
You received this message because you are subscribed to the Google Groups "Gurobi Optimization" group.
To unsubscribe from this group and stop receiving emails from it, send an email to gurobi+un...@googlegroups.com.

For more options, visit https://groups.google.com/d/optout.
Message has been deleted

Edgar Alan Avila Gomez

unread,
Jun 16, 2017, 11:22:57 AM6/16/17
to gur...@googlegroups.com
Is True, I use [] when there are a set of list...

m.setObjective(quicksum(y[s] * u for s in S), GRB.Maximize) 



El jun. 16, 2017 9:11 AM, "Nitesh Sood" <nites...@gmail.com> escribió:
Where and why do I need [] symbol? Apparently gurobi accepts the constraints when I loaded them into model (didn't give any error but I might have errors when I run the model)

I have actually additional question
How do I implement following objective function:

Message has been deleted

Edgar Alan Avila Gomez

unread,
Jun 18, 2017, 2:00:52 PM6/18/17
to gur...@googlegroups.com
The traditional way to do this is to use penalties on the i = j, then it simply iterates over all.

9999 x x
x 9999 x
x x 9999

Everything depends on how the problem is formulated, the best would be a literal interpretation.

I do not know the model and what restrictions it has, however the last constraints and objective corresponds to the TSP model ... if I previously made decoding variables for i = j equal to zero, I should actually let them take any value, and everything will depend Of the penalties.

2017-06-17 3:12 GMT-06:00 Nitesh Sood <nites...@gmail.com>:


Hello,

Correct me if I am wrong but I don't even need the i != j constraint since they already are 0 in my decision_variables matrix?

- Nitesh

torstai 15. kesäkuuta 2017 22.52.59 UTC+3 Nitesh Sood kirjoitti:



--

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



--
MII. Edgar Alan Ávila Gómez 
6671678109

Message has been deleted
Message has been deleted

Edgar Alan Avila Gomez

unread,
Jun 26, 2017, 4:10:50 AM6/26/17
to gur...@googlegroups.com
Imágenes integradas 3
Imágenes integradas 1
Imágenes integradas 2
 
Fisrt i see that the matrix its S * S+1,  but in (8)(9) start on 0 and finish in S, and in (10) start in 1 and finish in S.. so i think that you need a (S+1)*(S+1). 

y = {}
for r in S+1:
	for s in S+1:
		if r != s:
			y[r,s] = m.addVars(vtype = GRB.BINARY)
the next, r and s start on cero value and finish in S
for r in S:
	m.addConstr(quicksum([y[r,s] for s in S+1 if r != s]) == 1)

for s in S:
	m.addConstr(quicksum([y[r,s] for r in S if r != s]) == 1)

in the next series of restrictions, r and s, start in one value "1", and finish in S+1 so if you use a lists ... variables[s+1] its correct, or you can use a range.

for r in range(1,S+1):
	for s in range(1,S+1):
		if r != s:
			m.addConstr((v_Var[r] - v_Var[s] + (S+1) * y[r,s]) <= S) 

2017-06-23 6:28 GMT-06:00 Nitesh Sood <nites...@gmail.com>:
Link to my whole code:
https://github.com/NiteshNSood/SSD_efficient/blob/master/SSD-testi.py

And I have difficulties to understand constraint #15, why I need [r+1][s+1] according to my instructor?
I get same results with and without +1 (coincidence?). But since I am analyzing a lot of data and for looping 70-80 rounds (approximately this will take over 24 hours, about one to 30minutes for each round) and I will do this to different portfolio sets (5/10/12) I'd like to be sure that my model is flawless.

        # Longarela article, constraint (15)
        for r in scenarios:
            for s in scenarios:
                if r != s:
                    model.addConstr(
                            v_decision[r] - v_decision[s] + (S + 1) * y_decision[r+1][s+1]
                            <= S, name = "constraint (15)")
Link to article:
See, (13) - (17). 



perjantai 23. kesäkuuta 2017 10.41.47 UTC+3 Nitesh Sood kirjoitti:
Hi,

Whoah, it indeed resembles TSP model (I looked it up) (constraint 13, 14 Longarela's article).

We did some test and the model gave expected values. But there is one thing where I am confused. 
If you can please (I am not sure am I allowed to copy article here), see the Longarela's article "A characterization of the SSD-Efficient Frontier of Portfolio Weights by Means of a Set of Mixed-Integer Linear Constraints"


And my code looks like this:

y_decision:
        y_decision = []
        for r in scenarios_2:
            #Creating empty list for the row
            y_decision.append([])
            for s in scenarios_2:
                if r != s:
                    y_decision[r].append(model.addVar(vtype = GRB.BINARY))
                else:
                    y_decision[r].append(0)

        # Longarela article, constraint(13)
        for r in scenarios_2:
            model.addConstr(
                    quicksum(y_decision[r][s] for s in scenarios_2 if r != s) == 1,
                    name = "constraint (13)")
            
        # Longarela article, constraint(14)
        for s in scenarios_2:
            model.addConstr(
                    quicksum(y_decision[r][s] for r in scenarios_2 if s != r) == 1,
                    name = "constraint (14)")
            
        # Longarela article, constraint (15)
        for r in scenarios:
            for s in scenarios:
                if r != s:
                    model.addConstr(
                            v_decision[r] - v_decision[s] + (S + 1) * y_decision[r+1][s+1]
                            <= S, name = "constraint (15)")

I have one question regarding to that red text. My instructor asked me to put +1. But I don't understand why.
I also can upload the whole code if it is needed. 


sunnuntai 18. kesäkuuta 2017 21.00.52 UTC+3 Edgar Alan Avila Gomez kirjoitti:
The traditional way to do this is to use penalties on the i = j, then it simply iterates over all.

9999 x x
x 9999 x
x x 9999

Everything depends on how the problem is formulated, the best would be a literal interpretation.

I do not know the model and what restrictions it has, however the last constraints and objective corresponds to the TSP model ... if I previously made decoding variables for i = j equal to zero, I should actually let them take any value, and everything will depend Of the penalties.
2017-06-17 3:12 GMT-06:00 Nitesh Sood <nites...@gmail.com>:


Hello,

Correct me if I am wrong but I don't even need the i != j constraint since they already are 0 in my decision_variables matrix?

- Nitesh

torstai 15. kesäkuuta 2017 22.52.59 UTC+3 Nitesh Sood kirjoitti:



--

---
You received this message because you are subscribed to the Google Groups "Gurobi Optimization" group.
To unsubscribe from this group and stop receiving emails from it, send an email to gurobi+un...@googlegroups.com.

For more options, visit https://groups.google.com/d/optout.



--
MII. Edgar Alan Ávila Gómez 
6671678109

--

---
You received this message because you are subscribed to the Google Groups "Gurobi Optimization" group.
To unsubscribe from this group and stop receiving emails from it, send an email to gurobi+unsubscribe@googlegroups.com.
For more options, visit https://groups.google.com/d/optout.
Reply all
Reply to author
Forward
0 new messages