#NLConstraint with vectorized constraint JuMP/Julia - optimization

I am trying to solve a problem involving the equating of sums of exponentials.
This is how I would do it hardcoded:
#NLconstraint(m, exp(x[25])==exp(x[14])+exp(x[18]))
This works fine with the rest of the code. However, when I try to do it for an arbitrary set of equations like the above I get an error. Here's my code:
#NLconstraint(m,[k=1:length(LHSSum)],sum(exp.(LHSSum[k][i]) for i=1:length(LHSSum[k]))==sum(exp.(RHSSum[k][i]) for i=1:length(RHSSum[k])))
where LHSSum and RHSSum are arrays containing arrays of the elements that need to be exponentiated and then summed over. That is LHSSum[1]=[x[1],x[2],x[3],...,x[n]]. Where x[i] are variables of type JuMP.Variable. Note that length(LHSSum)=length(RHSSum).
The error returned is:
LoadError: exp is not defined for type Variable. Are you trying to build a nonlinear problem? Make sure you use #NLconstraint/#NLobjective.
So a simple solution would be to simply do all the exponentiating and summing outside of the #NLconstraint function, so the input would be a scalar. However, this too presents a problem since exp(x) is not defined since x is of type JuMP.variable, whereas exp expects something of type real. This is strange since I am able to calculate exponentials just fine when the function is called within an #NLconstraint(). I.e. when I code this line#NLconstraint(m,exp(x)==exp(z)+exp(y)) instead of the earlier line, no errors are thrown.
Another thing I thought to do would be a Taylor Series expansion, but this too presents a problem since it goes into #NLconstraint land for powers greater than 2, and then I get stuck with the same vectorization problem.
So I feel stuck, I feel like if JuMP would allow for the vectorized evaluation of #NLconstraint like it does for #constraint, this would not even be an issue. Another fix would be if JuMP implements it's own exp function to allow for the exponentiation of JuMP.Variable type. However, as it is I don't see a way to solve this problem in general using the JuMP framework. Do any of you have any solutions to this problem? Any clever workarounds that I am missing?

I'm confused why i isn't used in the expressions you wrote. Do you mean:
#NLconstraint(m, [k = 1:length(LHSSum)],
sum(exp(LHSSum[k][i]) for i in 1:length(LHSSum[k]))
==
sum(exp(RHSSum[k][i]) for i in 1:length(RHSSum[k])))

Related

TFAgents: how to take into account invalid actions

I'm using TF-Agents library for reinforcement learning,
and I would like to take into account that, for a given state,
some actions are invalid.
How can this be implemented?
Should I define a "observation_and_action_constraint_splitter" function when
creating the DqnAgent?
If yes: do you know any tutorial on this?
Yes you need to define the function, pass it to the agent and also appropriately change the environment output so that the function can work with it. I am not aware on any tutorials on this, however you can look at this repo I have been working on.
Note that it is very messy and a lot of the files in there actually are not being used and the docstrings are terrible and often wrong (I forked this and didn't bother to sort everything out). However it is definetly working correctly. The parts that are relevant to your question are:
rl_env.py in the HanabiEnv.__init__ where the _observation_spec is defined as a dictionary of ArraySpecs (here). You can ignore game_obs, hand_obs and knowledge_obs which are used to run the environment verbosely, they are not fed to the agent.
rl_env.py in the HanabiEnv._reset at line 110 gives an idea of how the timestep observations are constructed and returned from the environment. legal_moves are passed through a np.logical_not since my specific environment marks legal_moves with 0 and illegal ones with -inf; whilst TF-Agents expects a 1/True for a legal move. My vector when cast to bool would therefore result in the exact opposite of what it should be for TF-agents.
These observations will then be fed to the observation_and_action_constraint_splitter in utility.py (here) where a tuple containing the observations and the action constraints is returned. Note that game_obs, hand_obs and knowledge_obs are implicitly thrown away (and not fed to the agent as previosuly mentioned.
Finally this observation_and_action_constraint_splitter is fed to the agent in utility.py in the create_agent function at line 198 for example.

How to solve systems with TI-nspire automatically

is there any solution to solve a large system of equations in a program with the TI-nspire without typing all the variable names by hand?
For example, the function solve([1 2; 3 4]* [x; y] = 2,{x,y}) requires typing x,y manually. How is this done if the dimensions of the matrix can change?
I tried to use the function constructMat(x[i], i, j, 5, 1) which partly works but solve doesn't accept this function as variable as second argument.
Thanks for the help!!
I had this problem a while ago, i couldnt find any solution but to manually type the list of variables. Ive found the way to construct the list of unkowns (B_n):
i:=3:j:=4:B_n:=newlist(j+1) For i1,i,i+j:B_n[i1-i+1]:=expr("b"&string(i1)):endfor
with result: {b3,b4,b5,b6,b7}.
I dont remember exactly why this didnt work, i guess its the same reason (solve or zeros function will not accept this list as an argument)

Octave: quadprog index issue?

I am trying to run several files of code for an assignment. I am trying to solve an optimization problem using the "quadprog" function from the "optim" package.
quadprog is supposed to solve the optimization problem in a certain format and takes inputs H,f, A,b, Aeq, Beq, lb, ub.
The issue I am having involves my f which is a column vector of constants. To clarify, f looks like c*[1,1,1,1,1,1] where c is a constant. Quadprog seems to run my code just fine for certain values of c, but gives me the error:
error: index (_,49): but object has size 2x2
error: called from
quadprog at line 351 column 32
for other values of c. So, for example, 1/3 works, but 1/2 doesn't. Does anyone have any experience with this?
Sorry for not providing a working example. My code runs over several files and I seem to only be having problems with a specific value set that is very big. Thanks!
You should try the qp native Octave function.
You mention f is: c*[1,1,1,1,1,1] but, if c is a scalar, that is not a column vector. It seems very odd that a scalar value might produce a dimensions error...

Mathematica: Commands return no output, but itself. Bug?

I am working with Wolfram Mathematica 8 and have the following problem. I have an optimization problem under certain constraints and want to have an analytical (symbolical solution). I am maximizing function piA. My input is:
piA[a_, WA1_, WA0_] =
a/(1 + a)*(X - (y*WA1 + 1)^(1/y)) - 1/(1 + a) ((y*WA0 + 1)^(1/y));
Maximize[{piA[a, WA1, WA0], WA0 >= -1/y, WA1 >= -1/y}, WA0]
What I get most of the times is:
Maximize[{-((1 + WA0 y)^((1/y))/(1 + a)) + (
a (X - (1 + WA1 y)^(1/y)))/(1 + a), WA0 >= -(1/y), WA1 >= -(1/y)},a]
Basically, the command does nothing, but outputs itself. Only once I have managed to get the proper output (too long to paste here). I have tested it with simpler functions and it works. Unfortunately, I cannot understand what causes the problem. It is not a syntax problem, since it has worked like that several times. Any help would be very much appreciated.
P.S. Just checked again and my input ALWAYS generates the wrong output. The time it generated the solution was when I accidentally set parameters X and y to certain numbers.
The most likely reason is that given the function and constraints, Mathematica doesn't know how to maximize your function with respect to WA0. Note you also have a free variables X and a in there, and it might not have enough information about the domain of X and a to be able to properly form a solution to your equation.
I've had instances where I tried feeding in some equations and constraints and Mathematica simply couldn't do anything with them because they were too general. This may be the case here as well. Is there a specific problem you're trying to solve, and is there any way you could give Mathematica more context?
I don't think this is a bug at all, but it's unfortunate that sometimes Mathematica will just spit back your input when it doesn't have any rules for solving what you gave it.
The usual reason these things happens seems to be when the expressions given are too general for Mathematica to handle, or when it it's faced with a set of expressions that are ill formed.
Just as an example, I tried passing in fractions into a function I wrote that specifically looked for rational expressions, thinking it would work. It turned out that it needed to handle both Rational[a, b] and Times[a, Power[b, -1]]. It could be the case that Mathematica is not expecting a constraint to be of the form GreaterEqual[a, b].
Mathematica returns an answer if you assign the variable a some value. Maybe you could build your strategy on that? In fact it does provide an answer if you assign a value to any of the variables.
( I would need more background of the problem to go from there... )

can a variable have multiple values

In algebra if I make the statement x + y = 3, the variables I used will hold the values either 2 and 1 or 1 and 2. I know that assignment in programming is not the same thing, but I got to wondering. If I wanted to represent the value of, say, a quantumly weird particle, I would want my variable to have two values at the same time and to have it resolve into one or the other later. Or maybe I'm just dreaming?
Is it possible to say something like i = 3 or 2;?
This is one of the features planned for Perl 6 (junctions), with syntax that should look like my $a = 1|2|3;
If ever implemented, it would work intuitively, like $a==1 being true at the same time as $a==2. Also, for example, $a+1 would give you a value of 2|3|4.
This feature is actually available in Perl5 as well through Perl6::Junction and Quantum::Superpositions modules, but without the syntax sugar (through 'functions' all and any).
At least for comparison (b < any(1,2,3)) it was also available in Microsoft Cω experimental language, however it was not documented anywhere (I just tried it when I was looking at Cω and it just worked).
You can't do this with native types, but there's nothing stopping you from creating a variable object (presuming you are using an OO language) which has a range of values or even a probability density function rather than an actual value.
You will also need to define all the mathematical operators between your variables and your variables and native scalars. Same goes for the equality and assignment operators.
numpy arrays do something similar for vectors and matrices.
That's also the kind of thing you can do in Prolog. You define rules that constraint your variables and then let Prolog resolve them ...
It takes some time to get used to it, but it is wonderful for certain problems once you know how to use it ...
Damien Conways Quantum::Superpositions might do what you want,
https://metacpan.org/pod/Quantum::Superpositions
You might need your crack-pipe however.
What you're asking seems to be how to implement a Fuzzy Logic system. These have been around for some time and you can undoubtedly pick up a library for the common programming languages quite easily.
You could use a struct and handle the operations manualy. Otherwise, no a variable only has 1 value at a time.
A variable is nothing more than an address into memory. That means a variable describes exactly one place in memory (length depending on the type). So as long as we have no "quantum memory" (and we dont have it, and it doesnt look like we will have it in near future), the answer is a NO.
If you want to program and to modell this behaviour, your way would be to use a an array (with length equal to the number of max. multiple values). With this comes the increased runtime, hence the computations must be done on each of the values (e.g. x+y, must compute with 2 different values x1+y1, x2+y2, x1+y2 and x2+y1).
In Perl , you can .
If you use Scalar::Util , you can have a var take 2 values . One if it's used in string context , and another if it's used in a numerical context .