operand preceding > has invalid type - ampl

I've this variable:
var produced{L, M} >= 0;
I'm trying to find how many values inside producedare larger than 0:
subject to fee_calc {m in M} :
fee[m] = sum {l in L} (if produced[l,m] > 0 then 1 else 0);
But it throws this error: "operand preceding > has invalid type"...
How do I solve this?
Thanks

AMPL supports variables in if-then-else statements and in expressions involving strict relational operators like > and you can solve such problems with a constraint programming solver. See “LOGIC” AND CONSTRAINT PROGRAMMING EXTENSIONS for more details.
As for the error, it is likely to be caused by limitations of GLPK, which only supports a subset of AMPL features.

Related

How to write constraint which says if p or q then r in pyomo?

Hi I am learning optimization using pyomo and i have a problem where one variable should get value only when one of the other two variables or both get a value.
it's like (P V Q) => R in tautology. can someone please help how to write it as a constraint in pyomo.
example : if i am using 3 ingredients to make a product, 3rd one should always be used if any of 1,2 are used or both 1,2 are used.
I don't know what "get a value" is in optimization. All variables in the model "get a value". Look at it as a system of equations + an objective.
But, of course,
(P V Q) => R
is equivalent to
R >= P
R >= Q
where R,P and Q are binary variables.
What you are describing is disjunctive programming. Pyomo provides support for that through Pyomo.GDP: https://pyomo.readthedocs.io/en/latest/modeling_extensions/gdp.html, with more logical expression support upcoming.
For simple problems, the direct algebraic formulation suggested by Erwin is probably easiest.

Conditional Statement with Decision variables and Multiple relations

This is from CPLEX.
I tried doing this but getting no results. Basically my model need a forall statement with these two conditions using decision variables and multiple relations under that. All the equality constraints. Can anyone explain what is the problem in my syntax.
Error : Function operator<(dvar float+,float) not available in context CPLEX.
Some of the screenshots and actual equation from the document is provided alongwith the problem.
Regards,
Debtirthaenter image description here
// code from the model.
enter image description here
forall (a in A, j in Ji[a], n in N: j==jbreak)
{Ts[a][j][n] < tbreak && Tf[a][j][n] > tbreak} => (yvr1[j][n] == yv[j][n]);// && wvr1[a][n] == wv[a][n] && Balr1[a][j][n] == Bal[a][j][n] && Tsr1[a][j][n] == Ts[a][j][n] &&Tfr1[a][j][n] == Tf[a][j][n]);
forall (b in B: b==jbreak,i in Ij[b], n in N) ctTBRD[i][b][n]:
Tsr1[i][b][n] >= tbreak + tmaint;
}
strict inequality is not allowed so can you change
Tf[a][j][n] > tbreak
into
Tf[a][j][n] >= tbreak+1
?
Expanding on Alex's answer: the problem is indeed that strict inequality is not supported. However, Alex's solution will only work if tbreak is an integer variable. According to your error message, tbreak is a float+ variable, though. So the fix should be something like this:
Ts[a][j][n] <= tbreak - eps
where eps is a small constant, like 1e-6.
However, working with these tolerances is always a bit shaky, so you may want to double-check whether you can get around this. For example, by making tbreak an integer variable or by reverting the condition so that a strict less-than becomes a greater-than-or-equal (not sure this can be done but it is worth thinking about).

Cannot understand the reason of the problem being infeasible

I have a simple linear programming problem written in OSiL format, which is carved out from a complicated non-linear problem that reported as infeasible by SCIP. This simple problem is the minimal lines to reproduce this infeasible problem, however it confuses me. Below is the content of the OSiL:
<instanceData>
<variables numberOfVariables="1">
<var name="F"/>
</variables>
<objectives numberOfObjectives="1">
<obj maxOrMin="min" numberOfObjCoef="1" >
<coef idx="0">1</coef>
</obj>
</objectives>
<constraints numberOfConstraints="1">
<con lb="10"/>
</constraints>
</instanceData>
Isn't the OSiL saying:
Minimize: F
Subject to: F >= 0
? Why should this problem be infeasible? Looks to me, the <con lb="10"/> is useless because no one is referencing it. But in fact this constraint does influence the original problem in a way that I failed to notice, because the problem can be solved if the lower bound is changed to 0 or smaller, or change it to upper bound.
Can someone explain this to me? I'm a newbie in numerical optimization and the OSiL format, so thanks in advance for your time.
There is no F in your constraint, you only added the variable to the objective.
The constraint that is formulated there is 10 <= 0, which is infeasible.
If you look at the problem in SCIP, this may become more apparent:
original problem has 1 variables (0 bin, 0 int, 0 impl, 1 cont) and 1 constraints
SCIP> disp prob
STATISTICS
Problem name : a.osil
Variables : 1 (0 binary, 0 integer, 0 implicit integer, 1 continuous)
Constraints : 0 initial, 1 maximal
OBJECTIVE
Sense : minimize
VARIABLES
[continuous] <F>: obj=1, original bounds=[0,+inf]
CONSTRAINTS
[linear] <cons0>: 0 >= 10;
END

Idiomatic way of listing elements of a sum type in Idris

I have a sum type representing arithmetic operators:
data Operator = Add | Substract | Multiply | Divide
and I'm trying to write a parser for it. For that, I would need an exhaustive list of all the operators.
In Haskell I would use deriving (Enum, Bounded) like suggested in the following StackOverflow question: Getting a list of all possible data type values in Haskell
Unfortunately, there doesn't seem to be such a mechanism in Idris as suggested by Issue #19. There is some ongoing work by David Christiansen on the question so hopefully the situation will improve in the future : david-christiansen/derive-all-the-instances
Coming from Scala, I am used to listing the elements manually, so I pretty naturally came up with the following:
Operators : Vect 4 Operator
Operators = [Add, Substract, Multiply, Divide]
To make sure that Operators contains all the elements, I added the following proof:
total
opInOps : Elem op Operators
opInOps {op = Add} = Here
opInOps {op = Substract} = There Here
opInOps {op = Multiply} = There (There Here)
opInOps {op = Divide} = There (There (There Here))
so that if I add an element to Operator without adding it to Operators, the totality checker complains:
Parsers.opInOps is not total as there are missing cases
It does the job but it is a lot of boilerplate.
Did I miss something? Is there a better way of doing it?
There is an option of using such feature of the language as elaborator reflection to get the list of all constructors.
Here is a pretty dumb approach to solving this particular problem (I'm posting this because the documentation at the moment is very scarce):
%language ElabReflection
data Operator = Add | Subtract | Multiply | Divide
constrsOfOperator : Elab ()
constrsOfOperator =
do (MkDatatype _ _ _ constrs) <- lookupDatatypeExact `{Operator}
loop $ map fst constrs
where loop : List TTName -> Elab ()
loop [] =
do fill `([] : List Operator); solve
loop (c :: cs) =
do [x, xs] <- apply `(List.(::) : Operator -> List Operator -> List Operator) [False, False]
solve
focus x; fill (Var c); solve
focus xs
loop cs
allOperators : List Operator
allOperators = %runElab constrsOfOperator
A couple comments:
It seems that to solve this problem for any inductive datatype of a similar structure one would need to work through the Elaborator Reflection: Extending Idris in Idris paper.
Maybe the pruviloj library has something that might make solving this problem for a more general case easier.

Using conditional statements for CPLEX resolver

Is it possible to use in AMPL conditional statements such as "if (...) then..."? Below is shown as I tried to do.
subject to c1a {k in K, o in O, n in N: n!=t[k,o]}:
sum{e in E}
(a[n,e]*x[e,k,o]) -
sum{e in E}
(b[n,e]*x[e,k,o]) =
(if (r[n,k]==1 and f[n,o]==1)
then d[k,o]*(1-f[k,o])
else 0);
My ampl returning to me bug as follows:
CPLEX 11.2.0: Constraint _scon[1] is not convex quadratic since it is an equality constraint.
Do you have any idea ho to resolve this problem?
It is possible to use if-then-else expression with CPLEX if the condition (the expression between if and then) doesn't contain variables. CPLEX also supports so called "indicator constraints" (see here for more details) which use implication operator (==>) and are somewhat similar to if-then-else, but allow variables in the condition.
Regarding your example, it is not clear which names correspond to variables and which to constraints but the error suggests that the problem is not due to if-then-else, but because you have a quadratic constraint in the form not supported by CPLEX (see the section Quadratic Constraints on page 33 of ILOG AMPL CPLEX System User's Guide for the information about the accepted form).
you may change your solver cplex only deal with convex and quadratic constraints and it's used byt default in Ampl resolution, so you can try to repload your mod and dat files and then choose another solver as follow:
ampl: option solver " ipopt";
ampl: solve;
or
ampl: option solver " couenne";
ampl: solve;