Constraints comparator - constraints

Do you know a tool I could use to compare constraints (not only mathematical)? It's easier to explain with examples:
A) Simple example
C1: x < 0 && y * y < x
C2: x < 0 && y * y < x - 1
I want to know if C2 is stronger then C1, and it is. That means, (x,y) of C2 are also in C1.
B) Complicated example
C1: x > 0 && y > 0
C2: x > 0
C2 is weaker then C1 because it doesn't contain constraints on y.
I can try to write something by hand, but I don't think this is a solution. I know that the problem of solving constraints is undecidable, but I would like to know what work has been done in this direction.
Thanks,

Are you trying to decide if a constraint C1 subsumes C2 (meaning that it is not possible to find a set of values that satisfy C1 and not satisfy C2 -> C2 is true in all scenarios in which C1 is true but maybe not the other way around)?. When C1 subsumes C2 then C2 can be removed since it does not add any semantics to the problem.
If this is the case, I´d recommend you to use a CSP solver for that. It is easier than it seems. For instance, the Eclipse CSP solver (here Eclipse does not referes to the IDE platform, the solver has the same name) can be easily called from your java program through a well-defined API so you could just sent the problem to the solver and see which constraint is weaker

It's an interesting question! You could write a script that parses each constraint and checks the number of separate comparisons in each one although I'm not sure if that is the best way to measure them.

Related

Sagemath: Is there a simple way to factor a polynomial over C & have the roots appear in radical instead of decimal form?

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)].

How to add if(A and B) then C constraints in Gurobi

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)

Finding any one solution to two inequalities in 2 variables (Python)

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.

Decision variable in rhs of a constraint, implemented in R with lpSolveAPI or lpsolve

This is my first question on Stack Overflow.
I'm working on a optimization problem with R and the package lpSolveAPI which, to this point, works perfectly well for me.
My problem is very simple: How do I add a decision variable to the right hand side of a constraint?
Let C1,C2,...,C50 be the decision variables. How do I formulate a constraint like C1 > C34?
This seems to be so easy, that I have the feeling that I'm missing the point somewhere, but every post I've read and every example I've seen works with constant numeric constraints like C1 > 1000.
I don't have much experience with LPs or MILPs, so maybe there is a clever way to reformulate the constraint so it works with a numeric value on the rhs?
An example constraint from my application looks like this:
10 C1 + 2 C11 <= 200 C51
right after posting this question, i've had an idea how to solve it and it is really easy as well:
10 C1 + 2 C11 <= 200 C51 ---> 10 C1 + 2 C11 -200 C51 <= 0
This way it fits into the solver API!

Set Theory & Geometry: Two arcs on the same circle overlap with wrapping values

As a background, I'm a computer programmer and I'm working on a software library that allows a computer to quickly search through all dates to find a set of dates that satisfies a criteria. For example:
I want a list of every possible time that has ever occurred that has occurred on a friday or a saturday that is in April or May during the first week of the month.
My library uses numerical sets to efficiently represent ranges of dates that satisfy a criteria.
I've been thinking about ways to improve the performance of some parts of the app and I think that by combining sets and some geometry, I can really improve my results. However, my geometry is a bit rusty and I was hoping you might could help.
Here's my thought:
Certain elements of time can be represented as a circular dial. For example, Minutes can be positioned on a clock with values between 0...59. We could store valid ranges as a list of arcs. For example, If we wanted all times that ended with 05..10, we could store [5,10]. If we wanted all times that end with :45-59 or :00-15, we could store [45, 15]. Notice how this last arc "loops around" the dial. Here's a mockup showing different ranges intersecting on a dial
My question is this:
Given a set of whole numbers between N...M arranged into a circle.
Given Arc1 which is representing by [A, B] and Arc2 which is represented by [C, D] where A, B, C, and D are all within in range N...M
How do I determine:
A. Whether the arcs intersect.
B. If they do, what their intersection is.
C. If they do, what their union is.
Thank you so much for your help. If you're not able to help, if you can point me in the right direction, that would be great.
Thanks!
A simple and safe approach is to split the intervals that straddle 0. Then you perform pairwise interval intersection/union (for instance if A < D and C < B then [max(A,C), min(B,D)] for the intersection), and merge them if they meet at 0.
It seems the primitive operation to implement would be something like 'is the number X contained in the arch [A,B]'. Once you have that, you could implement an [A,B]/[C,D] arch-intersection predicate by something like -
Arch intersection means exactly that at least one of the following conditions is met:
C is contained in [A,B]
D is contained in [A,B]
A is contained in [C,D]
B is contained in [C,D]
One way to implement this contained-in-arch test without any branches is with some trigonometry and vector cross product. Not sure it would be faster (the math/branches performance tradeoff is entirely empiric), but it might be worth a try.
Denote Xa = sin(X/N * 2PI), Ya = cos(X/N * 2PI) and similarly for Xb,Yb etc.
C is contained in [A,B] is equivalent to:
Xa * Yc - Ya * Xc > 0
AND
Xc * Yb - Yc * Xb > 0
You can complete the other 3 conditions in an identical manner.
Hope this turns out useful.

Resources