How to constraint at most one element of variable vector could be non-zero in mosek? - optimization

I'm solving a problem like this:
Variable X = {x0, x1}
maximize AX + B
subject to 0 <= x0 <= c0,
0 <= x1 <= c1,
x0 * x1 <= 0
It's to say that, I want to get a solution of X with at most one non-zero element.
I'm not sure if problems like this could be solved with mosek.
Could someone give me some help?
Thanks a lot!

Related

Var Already defined in AMPL

I'm brand new to AMPL and can't seem to get past this issue.
The code I've written is very basic (again, just starting out) but I keep getting this error message "AMPLPrac.mod, line 2 (offset 6):
x1 is already defined
context: var >>> x1> <<< =0;"
Here is the code:
var x1>=0; var x2>=0;
maximize z: 2*x1 + 3*x2;
subject to c1: 2*x1 + x2 <=4; c2: x1 + 2*x2 <=5;
solve;
Any help would be appreciated. Thanks!
I think the problem may be that you are defining to variables on the same line i AMPL. I tried to run this code (who is the same as yours, only with one argument at every line:
var x1 >= 0;
var x2 >= 0;
maximize z: 2*x1 + 3*x2;
subject to c1: 2 * x1 + x2 <= 4;
subject to c2: x1 + 2 * x2 <= 5;
And I got no errors, and the following output:
x1 = 1
x2 = 2
So I think the problem must be that you have defines mulitply variables on the same line. So to next time, just one variables in every line.
The solution proposed by eriksen1110 is correct, but the problem with your model is that you omited the multiplication operator * from your math expressions, which is not possible in AMPL. It should be:
maximize z: 2*x1 + 3*x2;
# ^ ^
subject to c1: 2*x1 + x2 <= 4;
# ^
subject to c2: x1 + 2*x2 <= 5;
# ^
See it on PIFOP
However, you said that AMPL reports that x1 is already defined, which makes me suspect that you are reading the model file twice, and thus declaring the variable twice, which cannot be done.
Try reseting all declarations by putting this at the beggining of your model file:
reset;
Thanks for all your help! It turns out that the other issues I was having was that I had downloaded not the full version of AMPL and didn't have the correct solvers installed too so I was also getting error messages about not having Minos installed etc but it seems to be resolved now.

Constraint saying all elements of vector has to be >= 0 in scipy optimization

The input vector is x, which contains 5 elements and I want to optimize (minimize) a function with a constraint that all elements of x should be greater than or equal to 0.
ie., x[i] >= 0 for 1 <= i <= 5
In one of the answer I saw this and used in my code, but the answer is returning negative values also
def constraint2(x):
"""constrain all elements of a to be >= 0"""
return x
cons2 = {'type': 'ineq', 'fun': constraint2}
Where am I going wrong? How to enforce the constraint?
all(ele >= 0 for ele in x)
Try this

Conditional Graphing Plot?

I am trying to graph two functions, but i want to graph one function for a condition but graph using another function if another condition is met.
A simple example would be:
if x > 0
then sin(x)
else cos(x)
It would then graph cos and sin depending on the x value, there being an obvious gap at x = 0, as cos(0) = 1 and sin(0) = 0.
EDIT: There is a built-in way. I'll leave my original answer below for posterity, but try using the piecewise() function:
plot(piecewise(((cos(x),x<0), (sin(x), 0<x))))
See it here.
I would guess that there's a built-in way to do this, but I don't know it. You can multiply your functions by the Heaviside Step Function to accomplish this task. The step function is 1 if x > 0 and 0 if x < 0, so multiplying this into your functions and then summing them together will select only one of them based on the sign of x, that is to say:
f(x) := heaviside(x) * sin(x) + heaviside(-x) * cos(x)
If x > 0, heaviside(x) = 1 and heaviside(-x) = 0, so f(x) = sin(x).
If x < 0, heaviside(x) = 0 and heaviside(-x) = 1, so f(x) = cos(x).
See it in action here. In general, note that if you want the transition to be at x = a, then you could do heaviside(x-a) and heaviside(-x+a), respectively. If you want N functions, you'll have to have (N-1) multiplied step functions on each term, each with their own (x-a_i) argument. I hope someone else can contribute a cleaner solution.

Prolog Program to Find Square of Natural Numbers

My code below is meant to generate the square of natural numbers in order
(i.e sq(X). -> X=0; X=1; X=4; X=9; X=16; ...)
nat(0).
nat(X) :- nat(Y), Z is Y+1, X is Z*Z.
but the answer I am getting is:
1
0 ?- nat(X).
X = 0 ;
X = 1 ;
X = 4 ;
X = 25 ;
X = 676
Should be a quick fix, but I've spent longer on this than I'd like to say. Any help is greatly appreciated!
your nat/1 really seems to return a different sequence. Should be
nat(0).
nat(X) :- nat(Y), X is Y+1.
and then, a different predicate for square
sq(X) :- % call nat/1, square it...
please complete the code

Deriving equations for finite domain constraint system

The following inequality system is solved for x1 and x2 over the integers.
x1 + x2 = l
x1 >= y1
x2 >= y2
x1 <= z1
x2 <= z2
l - z1 <= x2
l - z2 <= x1
l,y1,y2,z1,z2 are arbitrary but fixed and >= 0.
With the example values
l = 8
y1 = 1
y2 = 2
z1 = z2 = 6
I solve the system and get the following equations:
2 <= x1 <= 6
x2 = 8 - x1
When I tell WolframAlpha that it should solve it over the integers, it only outputs all possible values.
My question is whether I can derive equations/ranges for x1 and x2 for any given l,y1,y2,z1,z2 programmatically. This problem is related to constraint programming and I found an old paper about this problem: "Compiling Constraint Solving using Projection" by Harvey et al.
Is this approach used in any modern constraint solving libraries?
The reason I ask this is that I need to solve systems like the above several thousand times with different parameters and this takes a long time if the whole system is read/optimized/solved over and over again. Therefore, if I could compile my parameterized systems once and then just use the compiled versions I expect a massive speed gain.