13
$\begingroup$

I suspect that OR.SE will see a good number of questions that ask for help with an optimization model that's not working as desired. Here are a few examples of such questions, from other sites:

Stack Overflow:

math.SE:

We've also had one or two on OR.SE, if I'm not mistaken. Many of these questions are well crafted and insightful and make it easy for readers to understand the question and develop answers. Others are sloppy and non-intuitive and make me not want to invest time to answer them. (I'm not calling these specific questions out as good or bad examples, just as examples of the kinds of questions I'm referring to in this post.)

I'm sure many on this site don't really want to answer "help me with my model" questions, no matter how well written they are, and that's fine. Others (myself included) enjoy answering questions like that from time to time.

One way or another, I am sure that questions like this will drive a lot of traffic our way, and we should welcome them.

I also think that one of the selling points of OR.SE is that we can provide guidance on all aspects of the modeling process, from algebraic formulation through coding and analysis. Right now, theory questions tend to be asked on math.SE, coding/implementation questions on SO, modeling questions tend to get split between them, and there are many other exceptions of course. On OR.SE, one can ask questions both that include math models (MathJax!) and programming (code blocks!) in the same post.

I think we should provide some guidance to askers about how to ask a "help me with my model" question. This will make it easier on both the askers and the answerers, and will (I hope) help jump-start the quality of these questions. My idea is that at some point this guidance would be part of our help page, and we can also link to it when responding to questions that need some work before they are answerable.

I'll post my thoughts in an answer below. I hope others will comment and/or post their own answers.

$\endgroup$

1 Answer 1

13
$\begingroup$

How to ask for help with your 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 12.8.0.0: 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?

$\endgroup$
1
  • $\begingroup$ I appreciate specifically calling out "on all aspects of the modeling process". Model implementation has many pitfalls and the referenced communities aren't as experienced with our requirements. Not sure if a "help me with my implementation" post would be helpful, but i hope this community remains receptive to this side of the business as well. $\endgroup$
    – somedude
    Nov 23, 2021 at 14:19

You must log in to answer this question.

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