Matlab CPLEX: add multiple SOCP constraints in cplexmiqcp - matlab

I have written my problem in MATLAB, using CPLEX as the solver. Due issues that are beyond my control (it is feasible), the CPLEX class API screws up when solving my problem. So, based on post found elsewhere on the internet, I am trying to solve using the toolbox API.
To solve my problem I need to use cplexmiqcp, which has the inputs:
cplexmiqcp(H,f,Aineq,bineq,Aeq,beq,l,Q,r,sostype,sosind,soswt,varLB,varUB,vartype,x0,options);
I have multiple SOCP constraints, and using the class API, I am able to define each of them using a structure, such as:
for n=1:numQCs
cplex.Model.qc(n).a=QC.a{n};
cplex.Model.qc(n).Q=QC.Q{n,1};
cplex.Model.qc(n).sense=QC.sense{n};
cplex.Model.qc(n).rhs=QC.rhs{n};
cplex.Model.qc(n).lhs=QC.lhs{n};
end
But how do I define multiple quadratic constraints for cplexmiqcp inputs? These are l,Q,r. When I try creating a structure as before, I get "Error: incorrect l,Q,r."

The documentation for the cplexmiqcp toolbox function is here. Quoting the documentation, for l, Q, and r, we have:
l: Double column vector or matrix for linear part of quadratic constraints
Q: Symmetric double matrix or row cell array of symmetric double matrices for quadratic constraints
r: Double or double row vector for rhs of quadratic inequality constraints
So, when we want to create one quadratic constraint, we can give a double column vector, a symmetric double matrix, and a double for l, Q, and r, respectively. When we want to create multiple quadratic constraints, we need to provide a matrix, a row cell array of symmetric double matrices, and a row vector for l, Q, and r, respectively.
Consider the following simple model:
Minimize
obj: x1 + x2 + x3 + x4 + x5 + x6
Subject To
c1: x1 + x2 + x5 = 8
c2: x3 + x5 + x6 = 10
q1: [ - x1 ^2 + x2 ^2 + x3 ^2 ] <= 0
q2: [ - x4 ^2 + x5 ^2 ] <= 0
Bounds
x2 Free
x3 Free
x5 Free
End
The MATLAB code would look like the following:
H = [];
f = [1 1 1 1 1 1]';
Aineq = []
bineq = []
Aeq = [1 1 0 0 1 0;
0 0 1 0 1 1];
beq = [8 10]';
l = [0 0;
0 0;
0 0;
0 0;
0 0;
0 0;];
Q = {[-1 0 0 0 0 0;
0 1 0 0 0 0;
0 0 1 0 0 0;
0 0 0 0 0 0;
0 0 0 0 0 0;
0 0 0 0 0 0], ...
[0 0 0 0 0 0;
0 0 0 0 0 0;
0 0 0 0 0 0;
0 0 0 -1 0 0;
0 0 0 0 1 0;
0 0 0 0 0 0]};
r = [0 0];
sostype = [];
sosind = [];
soswt = [];
lb = [ 0; -inf; -inf; 0; -inf; 0];
ub = []; % implies all inf
ctype = []; % implies all continuous
options = cplexoptimset;
options.Display = 'on';
options.ExportModel = 'test.lp';
[x, fval, exitflag, output] = cplexmiqcp (H, f, Aineq, bineq, Aeq, beq,...
l, Q, r, sostype, sosind, soswt, lb, ub, ctype, [], options);
fprintf ('\nSolution status = %s \n', output.cplexstatusstring);
fprintf ('Solution value = %f \n', fval);
disp ('Values =');
disp (x');

Related

Undefined function or variable for 3dos mechanism

I tried to make a 3dof(3 degrees of freedom mechanism) in matlab but i get this error and i don't know why.
this is for a school project and i need to simulate a human finger.
the code is running normal but after i enter the values for the angles it says that A,B,C are
undefined and i don't know why
a1 = input('valuarea lui q1(grade):');
a2 = input('valuarea lui q2(grade):');
a3 = input('valuarea lui q3(grade):');
L1=35;
L2=45;
L3=30;
z = [-10 10];
plot(z,10);
grid ON;
O=[0;0;0;1];
m= linspace(pi/2,pi/2+a1*pi/180,100);
n = linspace(-pi/2,a2*pi/180,100);
k=linspace(-pi/2,a3*pi/180,100);
for a=1:100
[A1,B1,C1] = Transform(m(a),n(a),k(a),L1,L2,L3);
x = [O(1) A(1) B(1) C(1)];
y = [O(2) A(2) B(2) C(2)];
Cx(i)= C1(1);
Cy(i) = C1(2);
i=i+1;
Plot = plot(x,y,'r',...1
'LineWidth',1);
title('Sumularea unui deget');
plot(Cx,Cy,'--g',...
'LineWidth',1);
pause(0.075);
delete(Plot);
end
plot(x,y,'r',...
'LineWidth',3);
function [A,B,C ] = Transform( m,n,p,l1,l2,l3 )
P = [0;0;0;1];
T1 = [cos(m) -sin(m) 0 0;sin(m) cos(m) 0 0;0 0 1 0; 0 0 0 1];
T2 = [cos(n) -sin(n) 0 11;sin(n) cos(n) 0 0;0 0 1 0; 0 0 0 1];
T3 = [cos(p) -sin(p) 0 12;sin(p) cos(p) 0 0;0 0 1 0; 0 0 0 1];
T4 = [1 0 0 13;0 1 0 0; 0 0 1 0; 0 0 0 1];
A = T1*T2*P;
B = T1*T2*T3*P;
C = T1*T2*T3*T3*P;
end
In your main function, this is the first use of the variables:
x = [O(1) A(1) B(1) C(1)];
They are never written previously. Instead A1 is written, which is a different variable. I guess you mixed the two up.

Linear combinations of all possible pairs of rows of a matrix in Matlab

I have a 2x1vector in Matlab called x.
clear
rng default
x=[5;6]; %[x1;x2]
I apply two linear transformations to x and obtain the 6x1 vectors A1 and A2.
Linear transformation 1
B1=10^6* [1 1;
0 0;
0 0;
0 0;
0 0;
0 0] - 10^6 * [0 0;
1 1;
0 0;
0 0;
0 0;
0 0] + [0 0;
0 0;
-1 0;
0 -1;
-1 1;
0 0];
A1=B1*x;
%A1=[10^6*(x1+x2); -10^6(x1+x2); -x1; -x2; x2-x1; 0]
Linear transformation 2
B2=10^6* [1 1;
0 0;
0 0;
0 0;
0 0;
0 0] - 10^6 * [0 0;
1 1;
0 0;
0 0;
0 0;
0 0] + [0 0;
0 0;
-1 1;
0 -1;
-1 0;
0 0];
A2=B2*x;
%A2=[10^6*(x1+x2); -10^6(x1+x2); x2-x1; -x1; -x2; 0]
Let y be a 1x2 vector with first element y1 picked from A1 and second element y2 picked from A2. As both A1 and A2 have 6 elements, y can take 36 values. Let Y be the 36x2 matrix listing in each row a possible value of y.
Hence,
%Y=[10^6*(x1+x2) 10^6*(x1+x2);
% 10^6*(x1+x2) -10^6*(x1+x2);
% 10^6*(x1+x2) x2-x1;
% 10^6*(x1+x2) -x2;
% 10^6*(x1+x2) -x1;
% 10^6*(x1+x2) -0;
% -10^6*(x1+x2) 10^6*(x1+x2);
% ...]
I would like your help to write down a code constructing a matrix C of size (2K)x2 such that
K=nchoosek(36,2), i.e., K=36!/(2*34!)=630
C*x=D, where D of size 2Kx1 reports, for every possible pair (i,j) of rows from Y,
[-Y(i,1)+Y(j,1);
-Y(i,2)+Y(j,2)]
Hence,
%D=[-10^6*(x1+x2)+10^6*(x1+x2); %rows 1,2 (first coordinate)
% -10^6*(x1+x2)-10^6*(x1+x2); %rows 1,2 (second coordinate)
% -10^6*(x1+x2)+10^6*(x1+x2); %rows 1,3 (first coordinate)
% -10^6*(x1+x2)+x2-x1; %rows 1,3 (second coordinate)
% ...]
Comments: let me clarify that I know which elements should be inside C in order to get the desired D, but I would like to avoid inserting them "manually". There should be some "pattern" that allows to quickly build C.
Also, ideally, I would like the code not to rely on the specific content of B1 and B2, i.e.: suppose I change B1 and B2 to other matrices producing different linear transformations of x; I would like the code to get C to remain valid.

Make a one simple code from several similar code

Hello everybody I have a very simple problem, I have too many data y, p and r. So I want to calculate it in a single code.
This an example of my code if I breakdown into separate code
y1=45
y2=56
y3=67
p1=34
p2=45
p3=56
r1=23
r2=34
r3=45
Ryaw1=[cosd(y1) -sind(y1) 0;
sind(y1) cosd(y1) 0;
0 0 1]
Rpitch1=[cosd(p1) 0 sind(p1);
0 1 0;
-sind(p1) 0 cos(p1)]
Rroll1=[1 0 0;
0 cosd(r1) -sind(r1);
0 sind(r1) cosd(r1)]
R1=Ryaw1*Rpitch1*Rroll1
Coordinate1=R1*X0
Ryaw2=[cosd(y2) -sind(y2) 0;
sind(y2) cosd(y2) 0;
0 0 1]
Rpitch2=[cosd(p2) 0 sind(p2);
0 1 0;
-sind(p2) 0 cos(p2)]
Rroll2=[1 0 0;
0 cosd(r2) -sind(r2);
0 sind(r2) cosd(r2)]
R2=Ryaw2*Rpitch2*Rroll2
Coordinate2=R2*X0
Ryaw3=[cosd(y3) -sind(y3) 0;
sind(y3) cosd(y3) 0;
0 0 1]
Rpitch3=[cosd(p3) 0 sind(p3);
0 1 0;
-sind(p3) 0 cos(p3)]
Rroll3=[1 0 0;
0 cosd(r3) -sind(r3);
0 sind(r3) cosd(r3)]
R3=Ryaw3*Rpitch3*Rroll3
Coordinate3=R3*X0
Coordinate=[Cooedinate1 Coordinate2 Coordinate3]
The goals is to find "Coordinate" (in matrix - combined from Coordinate1, Coordinate2, Coordinate3, .... ,Coordinate..) from every y, p and r data with the same "X0" as a single primary data for calculation.
Sorry for my bad english,
Thanks :)
Use vectors and matrices instead of individual scalars. These are indexed in almost the same way as you had before, i.e. y1 becomes y(1).
Then you can easily loop over the code 3 times and save the repetition.
See my commented code below.
% Define some X0. This should be a column vector.
X0 = [1; 2; 3];
% Make y,p,r into 3 element vectors
y = [45 56 67];
p = [34 45 56];
r = [23 34 45];
% Make R, Ryaw, Rpitch and Rroll 3x3x3 matrices
R = zeros(3,3,3);
Ryaw = zeros(3,3,3);
Rpitch = zeros(3,3,3);
Rroll = zeros(3,3,3);
% Make Coordinate a 3x3 matrix
Coordinate = zeros(3,3);
% Loop k from 1 to 3
% For each 3x3x3 matrix, the kth 3x3 matrix is equivalent to your Ryawk, Rpitchk, Rrollk, Rk
for k = 1:3
Ryaw(:,:,k) = [cosd(y(k)) -sind(y(k)) 0
sind(y(k)) cosd(y(k)) 0
0 0 1];
Rpitch(:,:,k)= [cosd(p(k)) 0 sind(p(k))
0 1 0
-sind(p(k)) 0 cos(p(k))];
Rroll(:,:,k) = [1 0 0
0 cosd(r(k)) -sind(r(k))
0 sind(r(k)) cosd(r(k))];
R(:,:,k) = Ryaw(:,:,k)*Rpitch(:,:,k)*Rroll(:,:,k);
Coordinate(:,k) = R(:,:,k)*X0;
end
disp(Coordinate)

How to formulate this expression

I am new to MATLAB and I want to formulate the following lease square expression in Matlab. I have some codes that I am typing here. But the optimization problem solution seems not to be correct. Does anyone has an idea why?
First, I want to solve the heat equation
$$T_t(x,t) = - L_x . T(x,t) + F(x,t)$$
where L_x is Laplacian matrix of the graph.
then find y from the following least square.
$$ \min_y \sum_{j} \sum_{i} (\hat{T}_j(t_i) - T_j(t_i, y))^2$$
Thanks in advance!!
Here is my code:
%++++++++++++++++ main ++++++++++++++++++++
% incidence matrix for original graph
C_hat = [ 1 -1 0 0 0 0;...
0 1 -1 0 0 -1;...
0 0 0 0 -1 1;...
0 0 0 1 1 0;...
-1 0 1 -1 0 0];
% initial temperature for each vertex in original graph
T_hat_0 = [0 7 1 9 4];
[M_bar,n,m_bar,T_hat_heat,T_hat_temp] = simulate_temp(T_hat_0,C_hat);
C = [ 1 1 -1 -1 0 0 0 0 0 0;...
0 -1 0 0 1 -1 1 0 0 0;...
0 0 1 0 0 1 0 -1 -1 0;...
0 0 0 1 0 0 -1 0 1 -1;...
-1 0 0 0 -1 0 0 1 0 1];
%
% initial temperature for each vertex in original graph
T_0 = [0 7 1 9 4];
%
% initial temperature simulation
[l,n,m,T_heat,T_temp] = simulate_temp(T_0,C);
%
% bounds for variables
lb = zeros(m,1);
ub = ones(m,1);
%
% initial edge weights
w0 = ones(m,1);
% optimization problem
% w = fmincon(#fun, w0, [], [], [], [], lb, ub);
%++++++++++++++++++++ function++++++++++++++++++++++++++++
function [i,n,m,T_heat,T_temp] = simulate_temp(T,C)
%
% initial conditions
delta_t = 0.1;
M = 20; %% number of time steps
t = 1;
[n,m] = size(C);
I = eye(n);
L_w = C * C';
T_ini = T';
Temp = zeros(n,1);
% Computing Temperature
%
for i=1:M
K = 2*I + L_w * delta_t;
H = 2*I - L_w * delta_t;
%
if i == 1
T_heat = (K \ H) * T_ini;
%
t = t + delta_t;
else
T_heat = (K \ H) * Temp;
%
t = t + delta_t;
end
% replacing column of T_final with each node temperature in each
% iteration. It adds one column to the matrix in each step
T_temp(:,i) = T_heat;
%
Temp = T_heat;
end
end
%++++++++++++++++++ function+++++++++++++++++++++++++++++++++++++++++
function w_i = fun(w);
%
for r=1:n
for s=1:M_bar
w_i = (T_hat_temp(r,s) - T_temp(r,s)).^2;
end
end
To give a more clear answer, I need more information about what form you have the functions F_j and E_j in.
I've assumed that you feed each F_j a value, x_i, and get back a number. I've also assumed that you feed E_j a value x_i, and another value (or vector) y, and get back a value.
I've also assumed that by 'i' and 'j' you mean the indices of the columns and rows respectively, and that they're finite.
All I can suggest without knowing more info is to do this:
Pre-calculate the values of the functions F_j for each x_i, to give a matrix F - where element F(i,j) gives you the value F_j(x_i).
Do the same thing for E_j, giving a matrix E - where E(i,j) corresponds to E_j(x_i,y).
Perform (F-E).^2 to subtract each element of F and E, then square them element-wise.
Take sum( (F-E).^2**, 2)**. sum(M,2) will sum across index i of matrix M, returning a column vector.
Finally, take sum( sum( (F-E).^2, 2), 1) to sum across index j, the columns, this will finally give you a scalar.

how can I vectorize setting the index values to one in Matlab?

I have the following loop that does what I need:
> whos Y
Name Size Bytes Class Attributes
Y 10x5000 400000 double
> whos y
Name Size Bytes Class Attributes
y 5000x1 40000 double
Y = zeros(K,m);
for i=1:m
Y(y(i),i)=1;
end
I would like to vectorize it and I have tried without success e.g.
Y = zeros(K,m);
Y(y,:)=1;
The idea is to get a vector of:
y = [9, 8, 7, .. etc]
and convert it to:
Y = [[0 0 0 0 0 0 0 0 1 0]' [0 0 0 0 0 0 0 1 0 0]' [0 0 0 0 0 0 1 0 0 0]' ... etc]
this I need in the context of a multi-class ANN implementation.
Have you considered using sparse matrix?
n=numel(y);
Y = sparse( y, 1:n, 1, n, n );
If you really must have the full matrix, you can call
Y = full(Y);
Here's one solution you could use. It's a starting point from which you could optimise
k = 10;
n = 20;
y = randi(k, 1, n);
columns = 1:n;
offsets = k*(columns-1);
indices = offsets + y;
Y = zeros(k, n);
Y(indices) = 1