Mixed Integer Programming using genetic algorithm - matlab

i have my objective function as
function val = fitness( X )
val = 10*X(7)+20*X(8)+50*X(9)+10*X(10)+20*X(11)+50*X(12);
end
and i am trying to call ga as
ga(#fitness,12,A,b,[],[],lb,[],[],IntCon)
A = 9X9 matrix
b = 9X1 matrix
lb = 9X1 Zero matrix
IntCon = [1:12]
i am getting the following error message
Error using preProcessLinearConstr (line 48)
The number of columns in A must be the same as the length of X0.
Error in gacommon (line 100)
[Iterate.x,Aineq,bineq,Aeq,beq,lb,ub,msg,exitFlag] = ...
Error in ga (line 319)
[x,fval,exitFlag,output,population,scores,FitnessFcn,nvars,Aineq,bineq,Aeq,beq,lb,ub,
please provide an example using ga function solving mixed integer problem.

The issue is that Ab is of size 9x1, while fitness() expects the size to be at least 12x1.
E.g. the following has no error:
A = ones(12,12);
b = ones(12,1);
lb = zeros(12,1);
IntCon = [1:12];
ga(#fitness,12,A,b,[],[],lb,[],[],IntCon)
For more information, see Mixed Integer Optimization.

Related

Optimization with genetic algorithm in matlab

I have written a simple optimization code using genetic algorithm.I don't know why I get error during running the code.Here is my code:
f = #(x1,x2) 1-x1.^2+(x1-x2).^2;
A = [1 1;-1 2;2 1];
b =[2 2 3]' ;
Aeq = [];
beq = [];
Lb = [0 0]';
Ub = [];
[Xopt,Fval] = ga(f,2,A,b,Aeq,beq,Lb,Ub)
I don not know why matlab gives me error.I wrote this programm based on the "Genetic algorithm Documentation" bit still gives me error:
Error using #(x1,x2)1-x1.^2+(x1-x2).^2
Not enough input arguments.
Error in createAnonymousFcn>#(x)fcn(x,FcnArgs{:}) (line 11)
fcn_handle = #(x) fcn(x,FcnArgs{:});
Error in makeState (line 48)
firstMemberScore = FitnessFcn(state.Population(initScoreProvided+1,:));
Error in galincon (line 18)
state = makeState(GenomeLength,FitnessFcn,Iterate,output.problemtype,options);
Error in ga (line 351)
[x,fval,exitFlag,output,population,scores] = galincon(FitnessFcn,nvars, ...
Caused by:
Failure in initial user-supplied fitness function evaluation. GA cannot continue
Objective functions of all optimization methods in MATLAB only accept 1 argument. According to ga documents:
fun — Objective function
Objective
function, specified as a function handle or function name. Write the
objective function to accept a row vector of length nvars and return a
scalar value.
When the 'UseVectorized' option is true, write fun to accept a
pop-by-nvars matrix, where pop is the current population size. In this
case, fun returns a vector the same length as pop containing the
fitness function values. Ensure that fun does not assume any
particular size for pop, since ga can pass a single member of a
population even in a vectorized calculation.
Change you objective function and it should work:
f = #(x) 1-x(1).^2+(x(1)-x(2)).^2;

Calculating numerical integral using MATLAB

I have an equation of the following form:
where A,B,C, and q are 3-by-3 matrix and Tr[...] represent trace. And
here b is a constant. The explicit form of A,B(x,y,E),C(x,y,E), q(x,y) matrices is written in the below MATLAB code. I am trying to solve it using the integral3() function of MATLAB. But it is giving me errors.
I wrote the function for the integrant in two different ways. And run the script:
integral3(#fun1,-pi,pi,-pi,pi,-inf,inf)
function file 1:
function out = fun1(x,y,E)
%=============just some constants==========
DbyJ = 2/sqrt(3);
T = 1e-2;
eta = 1e-3;
b = 1/T;
D = 1+1i*DbyJ;
fk1 = 1+exp(1i*x);
fk2 = 1+exp(1i*y);
fk1k2 = 1+exp(1i*(x-y));
%=============Matrices==========
A = eye(3); A(1,1) = 1;
q = [0, 1i*D*exp(-1i*x), 0 ;
-1i*conj(D)*exp(1i*x), 0,-1i*D*exp(1i*(x-y));
0,1i*conj(D)*exp(1i*(y-x)),0];
h = [0 -D*conj(fk1) -conj(D)*conj(fk2);
-conj(D)*fk1 0 -D*fk1k2;
-D*fk2 -conj(D)*conj(fk1k2) 0];
B = ((E-1i*eta)*eye(3) - h)^(-1);
C = conj(B);
Term1 = A*(B-C)*q*(B-C);
trc = trace(Term1);
N = -b*exp(b*E)/((exp(b*E)-1)^2);
out = trc*E*N;
It gave me the following error:
Error using horzcat
Dimensions of arrays being concatenated are not consistent.
Error in fun1 (line 19)
q = [0, 1i*D*exp(-1i*x), 0 ;
Then I tried to solve Tr[...] part symbolically and removed the matrices from integrant. The function file is very large for this, so, I am not putting it here. Anyway, it give me error that
Error using *
Incorrect dimensions for matrix multiplication. Check that the number of columns in the first matrix matches the number of rows in the second matrix. To perform elementwise multiplication, use '.*'.
Error in fun1 (line 33)
trc = (D*exp(-x*1i)*((exp(conj(x ... (it is a very long expression that I calculated symbolically to remove matrices.)
Question:
Why integral3() is not working?
Is there any other way to solve this kind of integrals numerically? (In Python or in any other software/language).
Thank you for reading.
TLDD:
How can I solve the above given integral numerically?

Undefined operator '.^' for input arguments of type 'dataset' - MatLab

I have this matlab function to calculate the cosine similarity between 60 vectors of a dataset. 60 rows correspond to the vector number and 2 columns to the x and y components of each vector.
function [cosSim] = cosineSimilarity(data)
[n_row n_col] = size(data);
norm_r = sqrt(sum(abs(data).^2,2));
for i = 1:n_row
for j = i:n_row
cosSim(i,j) = dot(data(i,:), data(j,:)) / (norm_r(i) * norm_r(j));
cosSim(j,i) = cosSim(i,j);
end
end
end
The main script:
cd(matlabroot)
cd('help/toolbox/stats/examples')
ds = dataset('XLSFile','TestCosSim.xlsx');
c = cosineSimilarity(ds);
When I run the script, I get the following errors:
Undefined operator '.^' for input arguments of type 'dataset'.
Error in cosineSimilarity (line 8)
norm_r = sqrt(sum(data.^2,2));
Error in CosSimTest (line 6)
c = cosineSimilarity(ds);
Does anybody have an idea of why this is happening?
Thanks a lot in advance.

How do I make simulation/optimization work in Matlab?

I am trying to link SimEvent and the optimization module of MATLAB. For that, I first need to define a function that runs the simulation then call it in an optimization function. I got the idea of the simulation/optimization code from the link below:
http://au.mathworks.com/videos/optimizing-manufacturing-production-processes-68961.html
I tried to go through all the code I see in this video but, when I am applying it, it is not working. Here is my code:
function finalresults = SimOpt ()
intcon = [1];
A=[];
b=[];
Aeq=[];
beq = [];
lb = [1];
ub= [10];
finalresults= intlinprog(#f,intcon,A,b,Aeq,beq,lb,ub);
function obj = f(vecX)
NumServers = vecX(1);
NumTruck = vecX(2);
set_param('concreting10/Positioning and Unloading','NumberOfServers',num2str(NumServers));
set_param('concreting10/Washing','NumberOfServers',num2str(NumTruck));
simOut = sim('concreting10','SaveOutput','on','OutputSaveName','WaitingTimeInQueue');
z = simOut.get('WaitingTimeInQueue');
waiting = max(z);
cost = [100 200]*vecX';
obj = waiting*1000+cost;
end
end
When I run the whole code I get this warning:
Error using intlinprog (line 122) INTLINPROG requires the following inputs to be of data type double: 'f'.
Error in SimOpt (line 26) finalresults= intlinprog(#f,intcon,A,b,Aeq,beq,lb,ub);
Any help will be appreciated.
Change the last line in the function to
obj = waiting * 1000.0 + cost
MATLAB and many other HLLs convert data type to integer if multiplied by an integer type constant value. So it is necessary to multiply the constant as double type by adding a decimal point.

Subscripted assignment dimension mismatch for Matrix transpose

My motive here is to calculate parameters of a model using Least Square Estimation method.
Variable W stores the 'time' and CF stores the no. of faults occurred. I tried testing it on different Software Reliability Models (more like functions, as I've define below in 'func' variable), and only in some of them this error occurred.
W=xlsread(file,'A:A');
CF=xlsread(file,'B:B');
a(1)=100;
a(2)=1;
dy = CF*0.04;
func = inline('a(1).*(1-exp(-a(2).*W))','a','W');
Nrepeat=100;
sd = 0.3;
zfactor = 2;
outcut=10;
pList=zeros(Nrepeat,3);
for rep =1:Nrepeat
rep
[a,r,j] = nlinfit(W,CF,func,a);
%copy fit to list
pList(rep,:) = a'; <--- ERROR
ERROR :
Subscripted assignment dimension mismatch.
Error in lse_go (line 59)
pList(rep,:) = a';
I have tried using a.'; , a,' , a' but still it shows the same error. How do I remove this error?