I am looking at solving optimizaton problems using MATLAB to model the problem and CPLEX as the solver. Everything works and I am able to get my solution. However lets say I have 3 optimization variables all of different sizes, when I obtain the solution, I get the values of all the variables in 1 variable which I now have to sought through to get the values for each inidivual variable. Now if I were doing this in python and one of my variables is x, I would just use value.x to get the value of the x variable. Is there a similar way to get the values for the individual optimization variables without have to manually sought it out myself. This is what I use to obtain my solution:
options = cplexoptimset('cplex');
options.timelimit = 300;
[sol3,fval3, exitflag3, output3] = cplexmiqp(PP.H, PP.f, PP.Aineq, PP.bineq, ...
PP.Aeq, PP.beq,[], [], [], PP.lb, PP.ub, ctype, [], options);
If I had 3 optim. variables x1, x2 and x3 which are not scalar but arrays, my solution is stored in sol3 as [x1 x2 x3]. I want to just reference each variable without doing it manually.
To CPLEX a variable is just an index and CPLEX does not know about the fact that subsets of your variables are organized in arrays, list, matrices, etc. So in general, the answer is "you can't do that". However, you are using the toolbox API which is designed to be a drop-in replacement for the matlab toolbox functions and thus has limited functionality.
You could switch to the class API. With this API you can explicitly create variables one by one or array by array and keep indices to the various variables. This is a bit more similar to what you would do in Python.
Related
I would like to find optimal hyperparamters for a specific function, I am using bayesopt routine in MATLAB.
I can set the variables to optimize like the following:
a = optimizableVariable('a',[0,1],'Type','integer');
But I have coupled variables, i.e, variables whose value depend on the existence of other variables, e.g., a={0,1}, b={0,1} iff a=1.
Meaning that b has an influence on the function if a==1.
I thought about creating a unique variables that encompasses all the possibilities, i.e., c=1 if a=0, c=2 if a=1,b=0, c=3 if a=1,b=0. The problem is that I am interested in optimizing continuous variables and the above approach does not hold anymore.
I tried something alone the line of
b = a * optimizableVariable('b',[0,1],'Type','integer');
But MATLAB threw an error.
Undefined operator '*' for input arguments of type 'optimizableVariable'.
After three months almost to the day, buried deep down in MATLAB documentation, the answer was to use constrained variables.
https://www.mathworks.com/help/stats/constraints-in-bayesian-optimization.html#bvaw2ar
I am trying to replicate this formula:
I have gathered all variables in my workspace. However estimating vec(Theta') does not seem to work and so I am a little bit stuck.
Theta = A*B-C;
vTheta = vec(Theta');
A, B and C are defined. The problem is that MATLAB does not seem to know the function vec to do what I would like to do with Theta as in the formula.
How to fix this?
I don't know where you got that equation from, but vec is a function in R, maybe it's related to that? If you want to convert a matrix Theta into a vector, do
Theta(:)
Edit: If you need to transpose the matrix first, MATLAB might not let you do Theta'(:). Instead do it in two steps:
tmp = Theta'; tmp(:)
As written above the Colon Operator is the way vectorize defined variable.
Yet, sometime we want to vectorize a sub set of a variable.
Let's say we have a matrix - mA and we'd like to vectorize a sub section of it - mA(2:3, 4:7).
One way is to define a new variable and vectorize it:
vA = mA(2:3, 4:7);
vA = vA(:);
Yet, what if we only wanted to use this inside another expression and only once?
Could we escape the need to generate explicit variable?
Well, unfortunately MATLAB doesn't have the view() functionality like in Julia.
Yet if you want to avoid explicitly naming new variable (I'm not sure if MATLAB's JIT Engine can also void the memory allocation as Julia) you can do:
reshape(mA(2:3, 4:7), [], 1)
This will always yield a column vector.
You can also use:
reshape(mA(2:3, 4:7), 1, [])
To generate row vector.
For instance you can do:
reshape(mA(2:3, 4:7), 1, []) * reshape(mA(2:3, 4:7), [], 1, )
This will be the sum of squared values of those elements.
I want to make symbolic functions theta1(t), theta2(t), theta3(t),...,thetaN(t) where N is some parameter I can define in MATLAB. I know that I can use something like sym('theta',[1 N]) to get [theta1, theta2, theta3,..., thetaN]. However, how can I do the same thing with theta being a function of t? The way to hard-code it would be like syms theta1(t) theta2(t) theta3(t) ... thetaN(t), but I want to make this general.
I do not want to directly use the sym command here because "support of character vectors that are not valid variable names and do not define a number will be removed in a future release", meaning something like sym('theta1(t)') would not be valid in future releases.
Any suggestions?
Figured part of it out. I could do something like the following
for i = 1:N
syms(strcat('theta',num2str(i),'(t)'))
end
However, if I want to assign a variable that contains all the symbolic expressions I'm still stuck. If I try
for i = 1:N
my_array(i) = syms(strcat('theta',num2str(i),'(t)'))
end
I get Error using syms (line 133). Using input and output arguments simultaneously is not supported. It works if I use sym instead of syms, but this leads to the warning I mentioned in my original post.
I would like to find global minima using an objective function I defined.
The code I used is:
opts = optimoptions(#fmincon,'Algorithm','interior-point');
gs = GlobalSearch;
problem = createOptimProblem('fmincon','x0',a,'objective', #(s) obj(supdata,s),'options',opts);
[xg, fg, exitflag, output, solutions ]=run(gs, problem)
The problem is that supdata is for a specific firm, and I need to get the optimal result for each firm (there are thousands of them). I want to apply this code using a big data input (which I already have, in a cell array, with each cell being a specific firm), and the output xg fg... are also varies among firms.
I was hoping to use a loop inside the function obj, but people here suggested that I to change my function to use cellfun: see my original question. But I don't know how to incorporate it into globalsearch. It is more complicated than just calling the solver fmincon.
I have a system of equations contained in an anonymous equation. Instead of defining all of the equations when i create the function, I would like to add one in each step of a for loop. Is this possible?
I suppose if you have a linear set of equations, you can construct it using a matrix, then you're free to include new operations by adding rows and columns to the matrix and/or its accompanying right hand side vector.
If you're really trying to use anonymous functions, say if your functions are non-linear, then I would suggest you to look into arrays of anonymous functions. For example,
A = cell(3,1); % Preallocate a 3 by 1 cell array
for ii = 1:3
A{ii} = #(x) x^2+ii; % Fill up the array with anonymous functions
end
Now if you check what's contained in cell array 'A',
A = #(x)x^2+ii
#(x)x^2+ii
#(x)x^2+ii
Don't worry about the display of 'ii' instead of the actual number of the loop variable as we gave it earlier, MATLAB has internally replaced them with those values. Changing 'ii' in the current function scope will also not affect their values in 'A' either. Thus,
A{1}(2) = 5, A{2}(2) = 6 and A{3}(2) = 7
If you're not familiar with cell arrays, you can read up on its usage here.
Again, what you're trying to achieve might be different. I hope this works for you.