2
$\begingroup$

I am trying to solve a large-scale nonlinear problem. Below is the objective function coded for pyscipopt. I have some loops over a list of tuples (r,p,s) in the list RouteTimeStop, and the only variable is headway[pat]. When I looped over RouteTimeStop, the solver stuck at the objective function creation and did not produce any progress for quite a long time. Then, I tried to see what is going on and considered first 200 tuples of the list.

### BEGIN DECLARING THE MODEL ###
m = Model('CTA')
### END DECLARING THE MODEL ###

### BEGIN ADDING VARIABLES INTO THE MODEL m ###
headway = {}
for pat in Pattern:
    headway[pat] = m.addVar(vtype = "C", lb = 1, ub = 30, name = "headway (%s)"%(pat))
print("Added the variables.")
### END ADDING VARIABLES INTO THE MODEL m ###

### BEGIN INTRODUCING THE OBJECTIVE FUNCTION INTO m ###
m.setObjective(quicksum(b/2/(1-el[r,p,s])*con2[r,p,s]*(30**(1-el[r,p,s])
             -(quicksum(pattern_ratio[r,p,s,pat]*30*headway[pat] for pat in RTSPdict[r,p,s])/30)
             **(el[r,p,s]-1)) for (r,p,s) in RouteTimeStop[:200])
             + quicksum(con[r,p,s]*quicksum(pattern_ratio[r,p,s,pat]*30*headway[pat]
             for pat in RTSPdict[r,p,s])**el[r,p,s] for (r,p,s) in RouteTimeStop[:200]),
             'maximize')
print("Added the objective function.")
### END INTRODUCING THE OBJECTIVE FUNCTION ###

The solver produced the following error.

Added the variables.
---------------------------------------------------------------------------
AssertionError                            Traceback (most recent call last)
<ipython-input-40-164bed181b42> in <module>
     11 
     12 ### BEGIN INTRODUCING THE OBJECTIVE FUNCTION INTO m ###
---> 13 m.setObjective(quicksum(b/2/(1-el[r,p,s])*con2[r,p,s]*(30**(1-el[r,p,s])
     14        -(quicksum(pattern_ratio[r,p,s,pat]*30*headway[pat] for pat in RTSPdict[r,p,s])/30)
     15                 **(el[r,p,s]-1)) for (r,p,s) in RouteTimeStop[:200])

src/pyscipopt/scip.pyx in pyscipopt.scip.Model.setObjective()

AssertionError: given coefficients are neither Expr or number but SumExpr

After playing a bit with the objective function, I realized number / variable and variable ^ number are not accepted in the objective function. How can I deal with this issue?

Below is a math formulation of the objective function (some notation names do differ between the code and the math formulation):

$$max \sum_{(r,p,s)\in RTS}e_{r,p,s}(h_{pat})+b w_{r,p,s}(h_{pat})\tag{1}$$

where

$$\tag{2}e_{r,p,s}(h_{pat})=C_{r,p,s}\left(\sum_{pat\in \pi_{r,p,s}}l_{pat}^{r,p,s}\frac{30}{h_{pat}}\right)^{\beta_{r,p,s}}$$

$$\tag{3}w_{r,p,s}(h_{pat})=\frac{C_{r,p,s}^\prime}{2\left(1-\beta_{r,p,s}\right)}\left[30^{\left(1-\beta_{r,p,s}\right)}-\left(\frac{\sum_{pat\in \pi_{r,p,s}}l_{pat}^{r,p,s}\frac{30}{h_{pat}}}{30}\right)^{\beta_{r,p,s}-1}\right]$$

Here, $h_{pat}\in\mathbb{R}^+$ and is the only variable. The parameter $\beta_{r,p,s}\in[0.01,0.95]$.

$\endgroup$
7
  • 2
    $\begingroup$ Without looking in detail in your specific function, it might help to introduce an auxiliary variable $z$ for your objective and then add the nonlinear constraint $z \le f(x)$ where $f$ is the objective function. SCIP supports nonlinear constraints but only linear objectives. $\endgroup$ Commented Jan 20, 2021 at 8:50
  • $\begingroup$ Well tried it, but starting to believe if Scip is a hoax. It shows running but nothing happening (no output logs) for hours. I have 10,000 variables. Maybe, it is because of that... $\endgroup$
    – tcokyasar
    Commented Jan 20, 2021 at 17:50
  • $\begingroup$ Is your model supposed to be convex if you ignore the integer constraints. $\endgroup$ Commented Jan 21, 2021 at 9:24
  • 1
    $\begingroup$ Since you use Scip I assumed some variables had to be integer constrained e.g. binary. If that is not case then Scip is may not be best option. Also the best option is very much dependent on whether your model is convex. $\endgroup$ Commented Jan 22, 2021 at 6:28
  • 1
    $\begingroup$ Ok. If there are no integer variables then using something like Ipopt or Knitro might be your best option. $\endgroup$ Commented Jan 22, 2021 at 14:38

1 Answer 1

2
$\begingroup$

As of April 1st 2024, it is now possible to use nonlinear objective functions directly on pyscipopt. To do this, you need to do the following:

from pyscipopt import Model, set_nonlinear_objective

m = Model()
x = m.addVar()
y = m.addVar()
m.set_nonlinear_objective(x*y)

We use the workaround with the set_nonlinear_objective because this is not a functionality in SCIP, and requires a(n epigraph) reformulation of the model. You can see the discussion that led to this decision in PR #781.

$\endgroup$
1
  • 1
    $\begingroup$ That's really handy! Thanks for mentioning that. $\endgroup$
    – A.Omidi
    Commented Jul 31 at 18:25

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.