Welcome to ShenZhenJia Knowledge Sharing Community for programmer and developer-Open, Learning and Share
menu search
person
Welcome To Ask or Share your Answers For Others

Categories

I am having issues summing over an exponential equation and using this as is the objective function.

I have also tried writing the exponential equation in as a constraint as I thought that may be another method to solve this issue, but this did not work for me as well.

Any help on this would be appreciated.

import pandas as pd
from pyscipopt import Model, quicksum, multidict, exp

num_fac_to_open = 1
order_to_open = []
opened_fac = []
closed_fac = [0, 1, 2]
S = [0, 1, 2]
R = [10, 11, 12]
distance_dict = {(0, 10): 300.8, (1, 10): 150.6, (2, 10): 1567.8, (0, 11): 1241.0, (1, 11): 2012.1, (2, 11): 789.2, (0, 12): 563.2, (1, 12): 1798.3, (2, 12): 946.3}
population_dict = {10:54, 11:46, 12:22}

# n is the desired number of facilities to open
n = len(opened_fac) + num_fac_to_open
# create a model
model = Model()

z, y= {}, {}
for s in S:
    # x_i is binary, 1 if service facility i is opened, 0 otherwise
    z[s] = model.addVar(vtype="B")
    for r in R:
        # y_i,j is binary, 1 if service facility i is assigned to residential area j, 0 otherwise
        y[s, r] = model.addVar(vtype="B")

for r in R:
    want_list = (distance_dict[s, r]*y[s, r] for s in S)
    want_list_quicksum = quicksum(want_list)
    exp_power = population_dict[r]*want_list_quicksum
    w = exp(exp_power)

model.setObjective(quicksum([w]), 'minimize')
model.optimize()

The error that occurs from this code is:

Traceback (most recent call last):
  File "stack_overflow_code.py", line 38, in <module>
    model.setObjective(quicksum([w]), 'minimize')
  File "src/pyscipopt/scip.pyx", line 1246, in pyscipopt.scip.Model.setObjective
AssertionError: given coefficients are neither Expr or number but SumExpr

It is my understanding that the format of the objective function should be (which is the result I get from printing exp_power):

Expr({Term(): 0.0, Term(x4): 12390.400000000001, Term(x8): 39562.6, Term(x12): 20818.6})

However, once the exponential term is added (w), the format becomes:

exp(sum(0.0,prod(12390.400000000001,x4),prod(39562.6,x8),prod(20818.6,x12)))

Further, when adding the quicksum[w], the format becomes:

sum(0.0,exp(sum(0.0,prod(12390.400000000001,x4),prod(39562.6,x8),prod(20818.6,x12))))

The aim is to minimize sum_{r=1}^N W_r

Where W_r = exp(population_dict[r]*sum_{s∈S} d_r,s * y_r,s) ?r ∈ R

question from:https://stackoverflow.com/questions/65661458/scip-optimization-of-an-exponential-function

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
thumb_up_alt 0 like thumb_down_alt 0 dislike
213 views
Welcome To Ask or Share your Answers For Others

1 Answer

quicksum takes an iterable, but distance_dict[s, r]*y[s, r] for s in S is not returning an iterable as you are probably expecting.

Try simplifying that line into multiple lines and making sure you have a list to work with before feeding it into quicksum.

In order to use list comprehension in Python we must wrap our statement in square brackets. [distance_dict[s, r]*y[s, r] for s in S].


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
thumb_up_alt 0 like thumb_down_alt 0 dislike
Welcome to ShenZhenJia Knowledge Sharing Community for programmer and developer-Open, Learning and Share
...