how to deal binary case in GA

1 view (last 30 days)
Khalid
Khalid on 26 Jan 2016
Answered: Walter Roberson on 26 Jan 2016
Hope you will be in the best of health.
I am working on optimization of energy and HEMS using GA tool box.
our aim is to turn on and off the automatically operated appliances through ga. which means that our population must be in binary [0,1] form.
  1. How to tell the ga to take population as binary data.
  2. I have made a code for fitness function. When I run it on ga toolbox it gives a message "not enough input arguments".
could you please help me in this regard?
Thanking you in anticipation.
here is the code
clc;
clear all;
close all;
ObjectiveFunction = @pricefunction;
nvars = 1920; % Number of variables
LB = zeros(16,120); % Lower bound
UB = ones(16,120); % Upper bound
ConstraintFunction = @constraint;
[x,fval] = ga(ObjectiveFunction,nvars,[],[],[],[],LB,UB, ...
ConstraintFunction)
function[opt] = pricefunction(X)
%%Power Scheduling Matrix %%%%%%%%%%%%%%%%%%%%
N = 16;
PC = [1 1 1 1.8 1.8 0.5 0.5 0.5 1.5 0.6 0.38 1.5 1.5 0.05 0.05 0.8]/5; % power consumption vector in 12 min resolution
PCM = zeros(N,120);
for j=1:N
PCM(j,:) = PC(j).* X(j,:);
end
PSCD = sum(PCM);
%%Real Time Pricing Function %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
y = zeros(1,120);
y(:,1:5) = 2;
y(:,6:10) = 1.9;
y(:,11:25) = 1.7;
y(:,26:30) = 2;
y(:,31:35) = 2.2;
y(:,36:40) = 2.4;
y(:,41:45) = 2.6;
y(:,46:50) = 2.7;
y(:,51:55) = 2.8;
y(:,56:60) = 3.2;
y(:,61:65) = 3.5;
y(:,66:70) = 4;
y(:,71:75) = 4.5;
y(:,76:80) = 5;
y(:,81:85) = 5.2;
y(:,86:90) = 4;
y(:,91:95) = 3.5;
y(:,95:97) = 3.2;
y(:,98:100)= 3.1;
y(:,101:105)= 2.8;
y(:,106:110)= 2.7;
y(:,111:115)= 2.4;
y(:,116:120)= 2.2;
au = y./5 ;
bu = 1.4423 * au ;
%%combining RTEP with IBR %%%%%%%%%%%%%%%%%%%%%
cu = 0.4;
pr = zeros(1,120);
for i = 1:120
if PSCD(i) < cu;
pr(i) = au(i);
else
pr(i) = bu(i);
end
end
%%First Minimization function
F1 = sum(PSCD .* pr);
opt = F1/(sum(pr)*max(PSCD))
% Normalization
% opt = F1/(F1.*max(PSCD)); % Normalization
end
function X = constraint(X)
N = 16; % number of appliances
a = [41 61 86 1 91 1 41 71 86 101 1 1 66 1 91 71]; % Start time
b = [60 85 120 30 115 25 60 90 105 120 60 25 85 30 120 91]; % End time
l = [5 5 10 5 10 2 2 2 3 2 5 1 1 10 10 5];
X = zeros(16,120);
for i=1:N
diff = b(i)-a(i) + 1;
x = zeros(1,diff);
ix = randperm(diff);
x(ix(1:l(i))) = 1;
X(i, a(i):b(i)) = x ;
end
end

Answers (1)

Walter Roberson
Walter Roberson on 26 Jan 2016
Your nonlinear constraint function needs to pay attention to its input, and it needs to return two outputs, each of which is a row vector. See http://www.mathworks.com/help/gads/ga.html#inputarg_nonlcon
For binary, you should be passing ga() another parameter after your nonlinear constraint function handle. The extra parameter, IntCon, should be the vector [1:nvars] if you want all of the variables to be integers.
In your price function,
for j=1:N
PCM(j,:) = PC(j).* X(j,:);
end
is expecting X to be a 16 x 120 array. It will not be: it will be a row vector of length 1920. You can reshape it once it is inside the routine. The same holds true for the nonlinear constraint function, that it will be passed a row vector.
Your LB and UB should be vectors. They will probably work the way you have specified them, but it is better to pass vectors.

Tags

Community Treasure Hunt

Find the treasures in MATLAB Central and discover how the community can help you!

Start Hunting!