Now I have three 5 by 3 matrix, X, Y, Z. All elements in them are binary variables. I want to add the following constraints:
I know I should introduce an auxiliary binary variable here. But I really stuck in how to write these simple conditional equalities as linear constraints. Any advice, tricks, suggestions?
Many thanks in advance!
Actually you don't need extra binary variables for this.
x(i,j)=1 and x(i+1,j)=0 => z(i+1,j)=1
can be interpreted as:
z(i+1,j) >= x(i,j)*(1-x(i+1,j))
This can be written as a linear inequality:
z(i+1,j) >= x(i,j) - x(i+1,j)
Similarly,
x(i,j)=0 and x(i+1,j)=1 => y(i+1,j)=1
can be formulated as:
y(i+1,j) >= x(i+1,j) - x(i,j)
Related
In Mathematica, if I do the following
Roots[x^3 - 2 == 0, x]
I get
x=(-1)^(2/3) 2^(1/3) || x=(-2)^(1/3) || x = 2^(1/3)
I want to do something similar in Sagemath
sage: F1.<x> = PolynomialRing(CC)
sage: f=x^3 - 2
sage: f.roots()
[(1.25992104989487, 1),
(-0.629960524947437 - 1.09112363597172*I, 1),
(-0.629960524947437 + 1.09112363597172*I, 1)]
Is there a way in sagemath to see it either as radicals or as ^(1/n) or something similar?
Is there a reason you need this computation to take place within a complex polynomial ring? I'm not an expert in computer algebra and I'm sure I'm oversimplifying or something, but I believe that is the root of this behavior; Sage treats the complex numbers as an inexact field, meaning that it stores the coefficients a and b in a+b*I as (default 53-bit) floats rather than as symbolic constants. Basically, what you are asking for is a type error, any object defined over the ComplexField (or ComplexDoubleField, or presumably any inexact field) will have floats as its coefficients. On the other hand, the corresponding behavior in the symbolic ring (where the token x lives by default) seems to be exactly what you are looking for; more specifically, evaluating var("t"); solve(t^3-2==0,t) returns [t == 1/2*I*sqrt(3)*2^(1/3) - 1/2*2^(1/3), t == -1/2*I*sqrt(3)*2^(1/3) - 1/2*2^(1/3), t == 2^(1/3)].
I'm trying to formulate a constraint for a MIP problem that involves binary variable v and continuous variable i, such as:
if i = 0, v = 0, and
if i > 0, v = 1
I haven't been able to think of a solution to this and I'm not sure if there is a solution. Any suggestion is greatly appreciated. Thank you!
you can rely on logical constraints.
In OPL you can write
dvar boolean v;
dvar float+ i;
subject to
{
v==!(i==0);
}
And you can do the same with all CPLEX APIs
You can also model this using the 'traditional' Big-M formulation which is documented in many places on the internet and in many textbooks.
Usually this is done in a pair of constraints like this:
i <= M * v
This forces i to be zero if v is zero, and also if i is non-zero then v must be 1 which covers most of your requirement, but still allows i = 0 and v = 1. In many cases the objective is trying to minimise some expression including v and that may be sufficient to encourage v=0 when i=0. But don't fall into the silly error of using a really big value for M as that will adversely affect your linear relaxations and possibly overal performance.
Then you might also need to add a further constraint to force v to zero if i is zero such as:
v <= i
which would have the effect of directly forcing v to zero if i is zero.
I have two inequalities as below and while I can find the appropriate regions for which the inequalities hold true, I am looking for any one particular value rather than solving the inequality by hand. So one of the solution in this case would be when d=-4.5 and g=3. And I want any one correct solution. I am using sage.
Input: solve([-1/2*d + 1/2 < g, g < -d - 1],[d,g])
Output: [[-2*g + 1 < d, d < -g - 1, 2 < g]]
The closest I have got is by using sympy modules but I was not able to solve 2 equations, only 1 works:
Input: solve_univariate_inequality(x**2 >= 4, x, relational=False)
Output:(−∞,−2]∪[2,∞)
Is anyone aware of a technique that would lead me to a solution?
I'm confused as to why the Sage answer isn't acceptable. It says any g>2 so pick g=4, maybe, and then any d between -2*g+1 and -g-1, so that would be between -7 and -5, so -6 works. The point is to give you all answers.
You could do something like this, but you'd still have to look at the first solution to see what was appropriate for d, or find a way to programmatically extract that information - and that might be challenging, since in general your inequality may not have a nice solution like this, and the order of the inequalities in the solution might have a more random order.
var('d,g')
S = solve([-1/2*d + 1/2 < g, g < -d - 1],[d,g])[0]
[s.subs(g=4) for s in S]
I hope this at least helps you frame your question, if not perhaps even answer your actual query; good luck.
I'm converting a rather complicated set of code from Matlab to R. I have zero experience in Matlab and am a functioning novice in R.
I have a segment of code which reads (in matlab):
dSii=(sum(tao.*Sik,1))'-(sum(m'))'.*Sii-beta.*Sii./N.*(Iii+sum(Iik)');
Which I've simplified and will focus on the first segment (if I can solve the first segment I'm confident I can perform the rest):
J = (sum(A.*B,1))' - ...
tao (or A) and Sik (or B) are matrices. So my assumption is I'm performing matrix multiplication here (A * B)and summing the resultant column. The '1' is what is throwing me off in that statement. In R, that 1 would likely indicate we're talking about a sum of rows as opposed to columns(indicated by 2). But I can't find any supporting documentation for that kind of Matlab statement.
I was thinking of using a statement like this (but of course, too many '1's and ',')
J<- (apply(A*B, 1), 1, sum)
Thanks for all your help. I searched for other examples here and elsewhere and couldn't find an answer. I'm willing to work for it but this is akin to me studying French (which I don't know) to translate in Spanish (which I'm moderate in) while interpreting the whole process in English. :D
Because of the different conventions in R and Matlab, the idiosyncrasies have to be learned for each (just like your language analogy!). The Matlab command sum(A.*B,1) means multiply A and B element-wise, so they must be the same shape, and then sum along dimension 1, i.e. add each row together to get the column sums. Dimension 1 is the default so, sum(A.*B) would do the same thing as sum(A.*B,1). Because R treats * as element-wise for matrix multiplication, the following Matlab and R codes will produce the same column of numbers in J:
Matlab:
A=[[1,2,3];[4,5,6];[7,8,9]];
B=[[10,11,12];[13,14,15];[16,17,18]];
J=sum(A.*B,1)'; %the ' means to transpose the column sums to be a 3x1 matrix
R:
A<-matrix(c(1,2,3,4,5,6,7,8,9),3,byrow=T)
B<-matrix(c(10,11,12,13,14,15,16,17,18),3,byrow=T)
J<-matrix(colSums(A*B)) # no transpose needed here: nrow(J)==3
Integers can be used to store individual numbers, but not mathematical expressions. For example, lets say I have the expression:
6x^2 + 5x + 3
How would I store the polynomial? I could create my own object, but I don't see how I could represent the polynomial through member data. I do not want to create a function to evaluate a passed in argument because I do not only need to evaluate it, but also need to manipulate the expression.
Is a vector my only option or is there a more apt solution?
A simple yet inefficient way would be to store it as a list of coefficients. For example, the polynomial in the question would look like this:
[6, 5, 3]
If a term is missing, place a zero in its place. For instance, the polynomial 2x^3 - 4x + 7 would be represented like this:
[2, 0, -4, 7]
The degree of the polynomial is given by the length of the list minus one. This representation has one serious disadvantage: for sparse polynomials, the list will contain a lot of zeros.
A more reasonable representation of the term list of a sparse polynomial is as a list of the nonzero terms, where each term is a list containing the order of the term and the coefficient for that order; the degree of the polynomial is given by the order of the first term. For example, the polynomial x^100+2x^2+1 would be represented by this list:
[[100, 1], [2, 2], [0, 1]]
As an example of how useful this representation is, the book SICP builds a simple but very effective symbolic algebra system using the second representation for polynomials described above.
A list is not the only option.
You can use a map (dictionary) mapping the exponent to the corresponding coefficient.
Using a map, your example would be
{2: 6, 1: 5, 0: 3}
A list of (coefficient, exponent) pairs is quite standard. If you know your polynomial is dense, that is, all the exponent positions are small integers in the range 0 to some small maximum exponent, you can use the array, as I see Óscar Lopez just posted. :)
You can represent expressions as Expression Trees. See for example .NET Expression Trees.
This allows for much more complex expressions than simple polynomials and those expressions can also use multiple variables.
In .NET you can manipulate the expression tree as a tree AND you can evaluate it as a function.
Expression<Func<double,double>> polynomial = x => (x * x + 2 * x - 1);
double result = polynomial.Compile()(23.0);
An object-oriented approach would say that a Polynomial is a collection of Monomials, and a Monomial encapsulates a coefficient and exponent together.
This approach works when when you have a polynomial like this:
y(x) = x^1000 + 1
An approach that tied a data structure to a polynomial order would be terribly wasteful for this pathological case.
You need to store two things:
The degree of your polynomial (e.g. "3")
A list containing each coefficient (e.g. "{3, 0, 2}")
In standard C++, "std::vector<>" and "std::list<>" can do both.
Vector/array is obvious choice. Depending on type of expressions you may consider some sort of sparse vector type (custom made, i.e. based on dictionary or even linked list if you expressions have 2-3 non-zero coefficients 5x^100+x ).
In either case exposing through custom class/interface would be beneficial as you can replace implementation later. You would likely want to provide standard operations (+, -, *, equals) if you plan to write a lot of expression manipulation code.
Just store the coefficients in an array or vector. For example, in C++ if you are only using integer coefficients, you could use std::vector<int>, or for real numbers, std::vector<double>. Then you just push the coefficients in order and access them by variable exponent number.
For example (again in C++), to store 5*x^3 + 9*x - 2 you might do:
std::vector<int> poly;
poly.push_back(-2); // x^0, acceesed with poly[0]
poly.push_back(9); // x^1, accessed with poly[1]
poly.push_back(0); // x^2, etc
poly.push_back(5); // x^3, etc
If you have large, sparse, polynomials, then maybe you'd want to use a map instead of a vector. If you have fixed sized lengths, then you'd perhaps use an fixed length array instead of a vector.
I've used C++ for examples, but this same scheme can be used in any language.
You can also transform it into reverse Polish notation:
6x^2 + 5x + 3 -> x 2 ^ 6 * x 5 * + 3 +
Where x and numbers are "pushed" onto a stack and operations (^,*,+) take the two top-most values from the stack and replace them with the result of the operation. In the end you get the resultant value on the stack.
In this form it's easy to calculate arbitrarily complex expressions.
This representation is also close to tree representation of expressions where non-leaf tree nodes represent operations and functions and leaf nodes are for constants and variables.
What's good about trees is that you can also easily evaluate expressions and you can also do things like symbolic differentiation on them. Both have recursive nature.