I want to ask for some help with my OR model. What guidance is available to me for doing so?


How to ask for help with your OR model

We love mathematical models and are happy to help you with yours. Before you ask your question, please read these suggestions about how to ask a good question. Doing so will make it easier for us to provide good answers.

  1. Use a descriptive title. Include (some of) the relevant details and be specific. Avoid vague, general titles like "Why isn't my model working?" Good titles will catch the reader's eye and make us want to read the question.

  2. Include your algebraic model. Include your entire algebraic formulation, or at least the relevant portion of it. Write your formulation using MathJax. Explain all of the notation and what the objective function and constraints are doing, if it's not obvious.

  3. Include minimal, reproducible code. If you are including code (AMPL, GAMS, Python, etc.), use a minimal, complete, verifiable example. That is, give us enough code so that we can run it, reproduce your problem/error, and try to debug it. Don't post very long code listings that contain a lot of stuff that's irrelevant to the problem.

  4. Be specific about what's not working. Tell us exactly what's not working the way you want it to. Are you getting error messages? Include them. Are you getting results that don't seem logical or numerically correct? Include them, and tell us what you think is wrong with them.

  5. Keep it self-contained. Your question should be as self-contained as possible. Avoid posts that require us to click on external images just to see your formulation, or to read a journal article to understand what you want your model to do. External links are good if they provide additional context, but the question should be able to stand on its own even if we don't click on the links.

  6. Use the tag. This tag will let users know that your question is one that needs help with a modeling problem.

A good example

Title: Why does AMPL/CPLEX give me "nonlinear" error for a knapsack-type problem?

Question: I am trying to modify a 0–1 knapsack problem to require that at least half of the items chosen are "priority" items. But I'm getting an error message in AMPL that I don't understand.

Here is my model. We have $n$ items, each with a weight $w_i$ and a value $v_i$. For item $i$, $p_i$ = 1 if the item is a "priority" item and 0 otherwise. The knapsack has a capacity of $W$. (These are all parameters.) The decision variables are $x_i$, which equals 1 if we choose the item, and 0 otherwise. The integer programming formulation is:

$$\begin{alignat}{2} \text{maximize} \quad & \sum_{i=1}^n v_ix_i && \\ \text{subject to} \quad & \sum_{i=1}^n w_ix_i \le W &\quad& \forall i=1,\ldots,n \\ & \frac{\sum_{i=1}^n p_ix_i}{\sum_{i=1}^n x_i} \ge 0.5 \\ & x_i \in \{0,1\} && \forall i=1,\ldots,n \end{alignat}$$

The first and third constraints are standard knapsack constraints. The second constraint says at least half of the items chosen have to be priority items.

I implemented my model in AMPL. Here is my *.mod file.

param n;                            # number of items
set ITEMS = 1..n;                   # set of items

param weight{ITEMS};                # weight of each item
param value{ITEMS};                 # value of each item
param is_priority{ITEMS} binary;    # 1/0 if item is priority/not

param capacity;                     # knapsack capacity

var x{ITEMS} binary;                # do we choose the item?

maximize TotalValue:
    sum {i in ITEMS} value[i] * x[i];

subj to Capacity:
    sum {i in ITEMS} weight[i] * x[i] <= capacity;

subj to HalfPriority:
    (sum {i in ITEMS} is_priority[i] * x[i]) / (sum {i in ITEMS} x[i]) >= 0.5;

Here is a simplified version of my *.dat file. (My real *.dat file has n > 500.)

param n := 4;

param: weight   value   is_priority :=
1       20      50      1
2       15      40      0
3       15      55      0
4       20      30      1 ;

param capacity := 40;

I am using CPLEX as the solver. When I solve the model, I get the following error message:

CPLEX Constraint _scon[1] is a nonquadratic nonlinear constraint.

What am I doing wrong?

(Spoiler: The "priority" constraint is nonlinear. Just multiply both sides by the denominator.)

A bad example

Title: Question about solver

Question: I want the knapsack problem to include a constraint that half the items are priority items.

Here is the constraint in AMPL:

subj to HalfPriority:
    (sum {i in ITEMS} is_priority[i] * x[i]) / (sum {i in ITEMS} x[i]) >= 0.5;

But AMPL gives me an error. What am I doing wrong?


You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .