Conditional contraints in linear programming for non boolean variables - linear-programming

Question 1 : x , y are integer non negative variables
i have a condition - if x > 0 then y=0
Question 2 : a , b , c , d , e are integer non negative variables
if any one of above > 0 then other 4 will be zero.
How to formulate above constraint as linear equations.I know how to handle above for Boolean variables but could not do for integer variable. I tried searching in other forums and WEB but could not get the solution.

As a starting point try creating some additional boolean variables, one for each of your non boolean variables. Then you can use a typical 'big m' modelling for each. Once you have something that works you can try reworking it to get a better model.

You can use logical implication in CPLEX (if-then). Please check out the following page: Logical constraints for CPLEX

Related

Is it possible for an MIP to have if then (either or)

I'm making a term project, I need something like this
x,te,ts decision variables,
if x[m,i]+x[m,j]-1 > 0
then
either te[i]+d-ts[j]<=0 or te[j]+d-ts[i]<=0;
If this was only either or I can make it with big M method, or I can convert an if then to either or, but I have a nested situtiation
is it possible to express this in a linear programming model? Or maybe my decision variables are wrong.

Is there a way to generate a new variable only if it meets certain criteria?

I am trying to replicate the following Stata code in R:
gen UAPDL_1=sqrt((((Sanchez_1-Iglesias_1)^2)+((Casado_1-Iglesias_1)^2)+((Rivera_1-Iglesias_1)^2))/3) if maxIglesias_1==1
replace UAPDL_1=sqrt((((Sanchez_1-Rivera_1)^2)+((Casado_1-Rivera_1)^2)+((Iglesias_1-Rivera_1)^2))/3) if maxRivera_1==1
In other words, I am trying to make different calculations and generate a new variable with different values depending on certain conditions (in this case, they have value 1 in an another variable. I managed to create the variables to be met for making the calculation (maxIglesias==1 and maxRivera==1), but I am stuck in the generation of the UAPDL variable. I tried with case_when and ifelse, but in these cases these commands only let you define a certain value. Is there a way with mutate or dplyr (or any other package) to achieve this goal?
Welcome to SO!
Let me try to 'parse' your question for the sake of clarity.
You want to generate a variable UAPDL depending on the value of two distinct variables (maxIglesias_1 and maxRivera_1, which let's say correspond to values f(I) and f(R), respectively). Here I note that, according to the snippet of the code you posted, there is no guarantee that the two variables are mutually exclusive - i.e., you may have records with maxIglesias_1 == 1 AND maxRivera_1 == 1. In those cases, the order in which you run the commands matters, as they all end up valued f(R), or f(I) if you twist them.
However, in order to replicate the Stata commands that you posted (issue with the ordering included!) you should run
UAPDL_1 <- numeric(length(maxIglesias_1)) # generate the vector
UAPDL_1[maxIglesias_1 == 1] <- f(I)
UAPDL_1[maxRivera_1 == 1] <- f(R)
where I assume that maxIglesias_1 and maxIglesias_1 are two R objects of the same length as the original Stata matrix.
Good luck!

Is there a #between? for numbers in Crystal?

I was wondering if I simply cannot find a between method for numbers in Crystal.
In Ruby, there's the Comparable#between? method which can (among others) compare two numeric values (my specific case).
Background: I want to achieve a not-between solution without using
variable < 2 || variable > 5
I tried 5.between(2,5) and 5.between?(2,5) but all I got was a compilation error:
Error in line 1: undefined method 'between?' for Int32
I ended up with extending the number structure:
struct Number
def between?(a, b)
self <=> a >= 0 && self <=> b <= 0
end
end
Question 2: Is my solution above a feasible one? If not, suggestions are welcomed.
In crystal you can write 2 <= variable <= 5, which is easier to read and gives you greater control over inclusivity/exclusivity at each end of the range.
From a deleted answer but I still like it:
You can use a similar method Range#includes? (or #covers).

Check whether variable has a value equal to some value in a fixed set

I am currently modelling a linear program for which I am using a formulation which I do not know if it is good.
I have some variable X, and in some constraint I want to check if X takes on a value from some list or set (if this is the case, then the this value is forbidden, otherwise the constraint disabled via the BigM method etc etc, but I think this is not so important).
So as an example, SET = {1, 2, 10}, and now if in the solution X would be 2 this is not allowed, X has to take on a different value.
Right now I am creating a constraint for every possible value x in SET and in the constraint use |X - x|, then do something like 0 <= |X - x| * BigM for every x.
Is there any "standard" way to model this, do you know a better solution? I did not find anything regarding this topic. Thanks a lot!
in OPL you could write
{int} SET={1,2,10};
dvar int x;
subject to
{
forall(i in SET) x!=i;
}

Sympy symbol and the cls parameter

I have seen f = sympy.symbols('f', cls=Function) but not any documentation. Python does not like x = sympy.symbols('x', cls=FF(8)), it complains about
raise CoercionFailed("expected an integer, got %s" % a) CoercionFailed: expected an integer, got x
Whan is the purpose of the cls parameters and what must I do so that cls=FF(8) is meaning full?
With x = sympy.symbols('x', cls=FF(8)) I want x to be a symbol in the field FF(8), i.e x^(2^8-1) must give me 1.
There are a few issues here:
The FF object does not allow Symbols. It only works for exact numerical entries, like FF(3)(2).
Therefore, the cls parameter of symbols will not work. That just changes what object is used to create the symbol, so it must take a string as an input (the default is Symbol).
SymPy does not currently support Symbols over finite fields. The best bet you can get is to use the Poly object with the modulus flag.
FF currently only supports finite fields of prime cardinality. FF(8) has actually created the ring Z_8, not the finite field with 8 elements.
You probably know this, but ^ does not do exponentiation in SymPy/Python. Use **.