Either or constraint in linear programming PULP - linear-programming

I am new to LP and also to using pulp. I'm trying to add an either/or constraint of the below form:
x ==0 or x >=35
I understand introducing a binary decision variable might help, but the model won't be a linear problem anymore. Are there other python packages I can use in that case?

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.

How to add a new constraint using ConstraintList() and "rule" in Pyomo

I am working in a optimization problem that has many iterations, and for each iteration the size of problem increases.
I am trying to use model.c = ConstraintList() to create my set of constraints.
And later I have to add new constraints.
Today I am using model.c.add(expr). However, I would like to use model.c.add(rule=my_rule)
But this provide the error add() got an unexpected keyword argument 'rule'
Using .add(expr) works fine, but using rule would improve the performance of the algorithm.
I can't use model.c = Constraint(rule=my_rule) because I don't have the exactly size of the problem in the first iteration.
Could someone help me please?

Differentiating in pyomo

How can we differentiate constraints defined in pyomo with respect to particular variables, and multiply those expressions with another pyomo model component. I want to generate a constraint that involves the derivative of other constraints, ie: l1*dg1/dz + l2*dg2/dz = 0, where l1 and l2 are pyomo variables, g1 and g2 are other constraints in the model. Kindly help me out. Thank you.
Take a look at the differentiate function in pyomo.core.base.symbolic. There are a few faster implementations in the pipeline, but this should give you what you need. For a usage example, you can take a look at the GDPopt solver code.

Implementing a solver in c++ for problems described in some scripting syntax

I have a generic question on how to write a C++ code to solve a general class of problems. The class of problems are described in some scripting language that will be read dynamically by the c++ program. E.g. the problem can be described like the following:
syms a b c x
sol = solve(a*x^2 + b*x + c == 0)
sola = solve(a*x^2 + b*x + c == 0, a)
Here I am just using MATLAB for illustration purposes, and I am not trying to build anything like MATLAB. What I am really after is to find out how, in general, does one go about designing a C++ program that will take in a script, which describes some calculation instructions, and then read/interpret the logic described in the script and then perform the calculations as described.
The general architecture of your program will look as follows:
(from the parsing article on Wikipedia)
There are plenty of tutorials covering lexical analysis, parsing and building parse trees or, more often, abstract syntax trees (AST). See, for example, the Kaleidoscope tutorial from LLVM.
Once you constructed the AST, you'll need to translate it into some internal representation such as byte code and pass it to an interpreter or a virtual machine. In some cases it is possible to skip this step and work directly with AST.
Interpreter will take the input generated on the previous step, construct the runtime representation of the algebraic problem (which is also a tree-like data structure) and pass it to the actual solver.
The solver will analyze the structure of the problem and apply relevant methods to find the solution for the equation. For example, if x is a variable and a, b and c are parameters in your example, it can detect that it is a quadratic equation and apply well known formulas to find the solution.

Can I check whether a variable has deterministic value by C++ API

I noticed that Z3 can do allsmt from some paper. In my project, I have to search for deterministic variables in a SMT formula. By deterministic I mean the variable can only take one int value to make the formula satisfiable. Is there a c++/c API function which can do this task?
The best I can do so far is to call the solver.check() function many times for the negation of each variable I am interested in. Is there a faster way to do this by using the API?
Basically, I want to do allsmt and predicate abstraction/projection.
There is no specialized API for checking if all models of a given variable have to agree on the same value. You can implement more or less efficient algorithms on top of Z3 to solve this question.
Here is a possible algorithm:
Get a model M from Z3.
For the variables you are interested in assert: Not (And([(M.eval(x) == x) for x in Vars]))
Recheck satisfiability. If the new state is unsatisfiable, then the remaining variales in Vars must have the same value. Otherwise, remove variables from Vars that evaluate to a new value different from the old M.eval(x), and repeat (2) until Vars is either empty or the context is unsatisfiable.