**You are now following this question**

- You will see updates in your followed content feed.
- You may receive emails, depending on your communication preferences.

# Optimization of Artificial Immune system using Genetic algorithm

5 views (last 30 days)

Show older comments

The defined problem for DCA (AIS) gives an output function,

providing three different ouputs for a matrix of input weights.

The condition for the output is

if C(csm)>thr && C(mDC)>C(smDC)

C=1;

else

C=0;

end

Where Cp = data(target,1)

Cs = data(target,2)

Cd= data(target,3) for target to be a logical vector for labels

Obviously, I have a target data for which this condition should meet, only if I optimize the weights properly.

So, in order to do this I created the following code for GA fitness function

function [s,c]=DCA_weights(W, data, targets)

c(1)=0.5*(W(1) * data(targets,1) + W(3) * data(targets,3) + W(2) * data(targets,2))/(W(1) + W(2) + W(3));

c(2)=0.5*(W(1) * data(targets,1) + W(3) * data(targets,3) + W(2) * data(targets,2))/(W(1) + W(2) + W(3));

c(3)=0.5*(W(1) * data(targets,1) + W(3) * data(targets,3) + W(2) * data(targets,2))/(W(1) + W(2) + W(3));

s=c(2)./c(3); % minimizing the ratio of C(smDC)/(CmDC)

and constraint function as (After solving the C(csm) < thr)

function [c, ceq] = DCA_constraint_GA(W, data, target)

c = (9.6 - data(target,1))*W(1) + (9.6 - data(target,2))*W(2) + (9.6 - data(target,3))*W(3);

ceq=[];

I have defined both upper and lower bounds for weights as [-6 -6 -6] and upper bounds as [6 6 6]

and the final code is

ObjectiveFunction = @DCA_weights;

nvars = 3; % Number of variables

LB = [-6 -6 -6]; % Lower bound

UB = [6 6 6]; % Upper bound

ConstraintFunction = @DCA_constraint_GA;

[x,fval] = ga(ObjectiveFunction,nvars,[],[],[],[],LB,UB,ConstraintFunction);

But I always get the following response,

'Not enough input arguments'

I assume there may be one small error that I am missing, Please help.

Thanks in advance

##### 0 Comments

### Accepted Answer

Stephan
on 15 Oct 2018

Edited: Stephan
on 15 Oct 2018

Hi,

solvers like ga only pass only one argument to their objective functions - in your case W. If your function needs additional arguments you have to pass them in another way. See passing extra parameters and look which is the best method for you. I prefer nested functions - but there are also other methods. The use of global variables is not recommended.

If you fix this, it should work.

General example:

% call outer function to start the calculation

outer_fcn

% define outer function containing the additional variables / values:

function outer_fcn

data = ...;

targets = ...;

% call ga inside the outer function:

ObjectiveFunction = @DCA_weights;

nvars = 3; % Number of variables

LB = [-6 -6 -6]; % Lower bound

UB = [6 6 6]; % Upper bound

ConstraintFunction = @DCA_constraint_GA;

[x,fval] = ga(ObjectiveFunction,nvars,[],[],[],[],LB,UB,ConstraintFunction);

% nonlinear constraints function (1. inner function)

function [c, ceq] = DCA_constraint_GA(W, data, target)

c = (9.6 - data(target,1))*W(1) + (9.6 - data(target,2))*W(2) + (9.6 - data(target,3))*W(3);

ceq=[];

end

% objective function (2. inner function)

function [s,c]=DCA_weights(W, data, targets)

c(1)=0.5*(W(1) * data(targets,1) + W(3) * data(targets,3) + W(2) * data(targets,2))/(W(1) + W(2) + W(3));

c(2)=0.5*(W(1) * data(targets,1) + W(3) * data(targets,3) + W(2) * data(targets,2))/(W(1) + W(2) + W(3));

c(3)=0.5*(W(1) * data(targets,1) + W(3) * data(targets,3) + W(2) * data(targets,2))/(W(1) + W(2) + W(3));

s=c(2)./c(3); % minimizing the ratio of C(smDC)/(CmDC)

end

% end of outer function

end

Best regards

Stephan

##### 28 Comments

BR
on 15 Oct 2018

Hey Stephen, thanks for the reply. Uhh, I tried to pass multiple arguments to GA when I tried to optimize NN and it worked. I created the following code;

load cancer_dataset.mat

options = optimoptions('gamultiobj');

inputs = cancerInputs;

targets = cancerTargets;

[ I N ] = size(inputs); % [ 9 699 ]

[O N ] = size(targets);

H=10;

Nw = (I+1)*H+(H+1)*O;

h = @(x) mse_test_NN(x, net, inputs, targets);

% number of neurons

n = 10;

% create a neural network

net = feedforwardnet(n);

% configure the neural network for this dataset

net = configure(net, inputs, targets);

PopulationSize_Data=200;

options = optimoptions(options,'PopulationSize', PopulationSize_Data);

options = optimoptions(options,'CreationFcn', @gacreationnonlinearfeasible);

options = optimoptions(options,'SelectionFcn', { @selectiontournament [] });

options = optimoptions(options,'CrossoverFcn', { @crossoverintermediate [] });

options = optimoptions(options,'MutationFcn', { @mutationuniform [] });

options = optimoptions(options,'Display', 'off');

[x_ga_opt, err_ga] = ga(h, Nw, options);

How come its not working now??

Is it because of the function handle I created but that was again constraint independent, So are you suggesting that I should create another function handle for the constraint as well.

Thanks

Stephan
on 15 Oct 2018

Edited: Stephan
on 15 Oct 2018

How come its not working now??

See the line of your code:

h = @(x) mse_test_NN(x, net, inputs, targets);

This is exactly the Syntax from passing extra Parameters - section: anonymous functions. Everything is in one script, without defined functions and an anonymous function does the job.

So you did it right unconsciously in this case by 'choosing' this way to pass your extra Parameters to ga.

-------------------------------------------------------------------------------------------------------------------------------------------

So are you suggesting that I should create another function handle for the constraint as well

Since your constraints and your objective function are a little to much code to put in a function handle i would suggest to do like i wrote in my answer and use nested functions.

Leave them as they are, but make an outer function to them, where the call of ga is included and the extra parameters are given in. You will see that the variables that occur in both (inner and outer functions) will be colored light blue. This is what you want. Here is an other example for fsolve as a picture to let you see what i mean:

.

All the light blue variables are extra parameters passed to objective function by nested function. Note that the objective function only depends on x (which it gets from the solver) - all other variables are 'known', because the structure of nested functions.

I hope this helped.

BR
on 15 Oct 2018

Thanks very much Stephen, I haven't tried this yet but I expect this should work.

Cheers

BR
on 15 Oct 2018

Edited: BR
on 15 Oct 2018

Hey Stephen, sorry to bother again. I created two separate handles for the same and It still didn't worked. I got the following error;

Not enough input arguments.

Error in DCA_constraint_GA (line 2)

c = (9.6 - data(target,1))*W(1) + (9.6 - data(target,2))*W(2) + (9.6 - data(target,3))*W(3);

Another question I wish to know is that the values of data(target,[1,2,3]) is not just a value its a vector of values. So, how should I handle this? Please, if you can help.

Thanks

Stephan
on 16 Oct 2018

Edited: Stephan
on 16 Oct 2018

Another question I wish to know is that the values of data(target,[1,2,3]) is not just a value its a vector of values. So, how should I handle this?

Just run the code and then type it in the command line - for example:

>> data(target,[1,2,3])

ans =

0.7779 -3.4300 1.7461

1.6822 3.4000 -0.1083

3.1000 1.9061 0.5777

-2.3891 -1.5500 -4.0100

3.9900 0.6235 1.1069

-0.9830 0.6557 4.2000

>> data(target,1)

ans =

0.7779

1.6822

3.1000

-2.3891

3.9900

-0.9830

So you can see that data(target,1) is a vector.

--------------------------------------------------------------------------------------------------------------------

Hey Stephen, sorry to bother again. I created two separate handles for the same and It still didn't worked. I got the following error:

The good news is, that your function handles seem to work in general as far as i can see.

If i run your code i get the error:

>> DCA_sphere_GA

Not enough input arguments.

Error in DCA_weights (line 2)

c(1)=0.5*(W(1) * data(targets,1) + W(3) * data(targets,3) + W(2) * data(targets,2))/(W(1) + W(2) + W(3));

Error in DCA_sphere_GA>@(W)DCA_weights(W,data)

Error in createAnonymousFcn>@(x)fcn(x,FcnArgs{:}) (line 11)

fcn_handle = @(x) fcn(x,FcnArgs{:});

Error in gacon (line 23)

Iterate.f = FitnessFcn(Iterate.x');

Error in ga (line 405)

[x,fval,exitFlag,output,population,scores] = gacon(FitnessFcn,nvars, ...

Error in DCA_sphere_GA (line 42)

[x,fval] = ga(h,nvars,[],[],[],[],LB,UB,h1);

This is due to DCA_weights depends on W, data and target, but your function handle doesnt call all of them. You do:

h = @(W) DCA_weights(W, data);

instead of:

h = @(W) DCA_weights(W, data, target);

If i perform this correction, the weights function seems to want to run, but then there is a new error:

Unable to perform assignment because the indices on the left side are not compatible with the size of the right side.

Error in DCA_weights (line 2)

Error in DCA_sphere_GA>@(W)DCA_weights(W,data,target)

Error in createAnonymousFcn>@(x)fcn(x,FcnArgs{:}) (line 11)

fcn_handle = @(x) fcn(x,FcnArgs{:});

Error in gacon (line 23)

Iterate.f = FitnessFcn(Iterate.x');

Error in ga (line 405)

[x,fval,exitFlag,output,population,scores] = gacon(FitnessFcn,nvars, ...

Error in DCA_sphere_GA (line 42)

[x,fval] = ga(h,nvars,[],[],[],[],LB,UB,h1);

This seems to be a problem of dimensions. Since i have not really an idea of the Content in your code i dont know exactly where the problem is here. But i suspect, that W(1)...W(3) could be a problem. Consider that W is a 3x3 Matrix:

W =

2.0000 1.0000 2.0000

0 0 2.0000

2.0000 1.0000 -3.9000

If you call W(1) or W(2) you get scalars not vectors:

>> W(1)

ans =

2

>> W(2)

ans =

0

>> W(3)

ans =

2

I suspect this is not what you want. To get the row vectors for example use:

>> W(1,:)

ans =

2 1 2

Since you want (do you?) to perform a Matrix multiplication with:

W * data(target)

the only operation that works with W and the other vector would be:

>> W(:,1) * data(target,1)'

ans =

1.5559 3.3643 6.2000 -4.7782 7.9800 -1.9661

0 0 0 0 0 0

1.5559 3.3643 6.2000 -4.7782 7.9800 -1.9661

All other Matrix multiplications are not allowed, due to the rules of linear algebra. Note that data(target,1) is transposed here.

I think you should look at this point and find out what calculation (or maybe another bug somewhere before) leads to this error.

Then you can continue debugging. I'll stay tuned.

BR
on 16 Oct 2018

Edited: BR
on 16 Oct 2018

Hey,

I made the following change in DCA_weights:

function [s,c]=DCA_weights(W, data, target)

c(1,:)=0.5*(W(:,1)'* data (target,:)')/(W(1) + W(2) + W(3));

c(2,:)=0.5*(W(:,2)'* data (target,:)')/(W(1) + W(2) + W(3));

c(3,:)=0.5*(W(:,3)'* data (target,:)')/(W(1) + W(2) + W(3));

s=c(2)./c(3);

which is a right option but I am still getting the same error

Subscripted assignment dimension mismatch.

Error in DCA_weights (line 2)

c(1,:)=0.5*(W(:,1)'* data (target,:)');

I think it may be because its taking W(:,k) now as a vector to optimize which may be is not allowed to do in matlab. I am not sure...

Well to explain the problem more exhaustively, that equation i mentioned before has 3 outputs for 9 different W's. It can be seen in the code I uploaded to you in DCA_sphere_GA. So, its like, for

W = [2 1 2;

0 0 2;

2 1 -3.9];

for k= 1:3

c(k)=0.5*(W(1,k) * data (1,1) + W(2,k) * data(2,1) + W(3,k) * data(3,1))/(W(1,k) + W(2,k) + W(3,k));

end

But as you can see, this can be redesigned to write it as

c(1,:)=0.5*(W(:,1)'* data (target,:)')

SO, that's why i was writing it in this way. But this gives rise to another problem - 'Subscripted assignment dimension mismatch.'

The possible change that I was considering is to restructure the fitness function as -

function [s,c]=DCA_weights(W, data, target)

x = data (target,:);

c(1)=0.5*(W(1,1) * x(1,1) + W(3,1) * x(1,3) + W(2,1) * x(1,2))/(W(1,1) + W(2,1) + W(3,1));

c(2)=0.5*(W(1,2) * x(1,1) + W(3,2) * x(1,3) + W(2,2) * x(1,2))/(W(1,2) + W(2,2) + W(3,2));

c(3)=0.5*(W(1,3) * x(1,1) + W(3,3) * x(1,3) + W(2,3) * x(1,2))/(W(1,3) + W(2,3) + W(3,3));

s=c(2)./c(3);

But again this error message turned up

Index exceeds matrix dimensions.

Error in DCA_weights (line 3)

c(1)=0.5*(W(1,1) * x(1,1) + W(3,1) * x(1,3) + W(2,1) * x(1,2))/(W(1,1) + W(2,1) + W(3,1));

I am not sure again where I am making a mistake. Thanks for considering.

Stephan
on 16 Oct 2018

Edited: Stephan
on 16 Oct 2018

Hi,

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.

Your function doesnt meet this requirement. If you run your code (just to have variables in Workspace) and type this line in the command window:

[s,c] = DCA_weights(W, data, target)

you get a matrix and a scalar value:

s =

0.427930961393379

c =

0.631017677411916 0.315508838705958 -1.514247310377161

0.393454844238717 0.196727422119359 1.323359732612840

0.919435328908184 0.459717664454092 0.969871928547664

-1.599778735489660 -0.799889367744830 0.970104335199522

1.274234711658103 0.637117355829051 0.613737520113848

0.804238527018159 0.402119263509080 -2.129324212061409

So your function works as it should - but it does not meet the requirements to give a scalar value back to ga.

--> next point to fix

BR
on 16 Oct 2018

How about if I don't take 'c' as the output, but only 's'. It still doesn't work.

following error

Index exceeds matrix dimensions.

Error in DCA_weights (line 3)

c(1)=0.5*(W(1,1) * x(1,1)' + W(3,1) * x(1,3)' + W(2,1) * x(1,2)')/(W(1,1) + W(2,1) + W(3,1));

Stephan
on 16 Oct 2018

Would not it be easier to limit the output argument of the function to the scalar value? So to do this here:

function s = DCA_weights (W, data, target)

instead of this function declaration:

function [s, c] = DCA_weights (W, data, target)

Then you can achieve a bug-free running code with the following change:

function s = DCA_weights(W, data, target)

c(:,1)=0.5*(W(:,1)'* data (target,1)')/(W(1) + W(2) + W(3));

c(:,2)=0.5*(W(:,2)'* data (target,2)')/(W(1) + W(2) + W(3));

c(:,3)=0.5*(W(:,3)'* data (target,3)')/(W(1) + W(2) + W(3));

s=c(2)./c(3);

end

This version runs and seems to give a result...

VERY BIG BUT:

You should make sure that what is calculated here also makes sense in terms of content. It would not help if you worked on the indexes until Matlab returns any result instead of an error message, if this result has no content value. But you have to worry about that because I can not know which connections are relevant here. I'm happy to help, but I hope you have the ability to verify the results you've received to make sure they're correct, not just "crafted" solutions that are of no use.

Stephan
on 17 Oct 2018

There is one more thing that seems to be wrong here. You define the options via optimoptions for the gamultiobj solver, but to procede the optimization you call ga solver.

If you run the optimization with ga (with options also changed to ga) you get a result for x which is pretty near the bounds:

x =

-5.9999 -6.0000 -5.9999

>> fval

fval =

0.5426

If you change both entries to gamultiobj you get:

x =

-3.2364 0.4830 -5.0124

>> fval

fval =

0.5426

Interesting is that this doesnt effect the value of the result, but the values of x. So perhaps this gives you a clue if your result is calculated correctly.

BR
on 17 Oct 2018

Edited: BR
on 17 Oct 2018

Yeah, I made that change. Still the same result with lots of false positives.

Can I ask, how are you getting only three values there should be 9 values. Speaking of which we also have to take care of the denominator in the equation that we are considering, It should be

c(1,:) = [*********]/(W(1,1) + W(2,1) + W(3,1));

c(2,:) = [*********]/(W(1,2) + W(2,2) + W(3,2));

c(3,:) = [*********]/(W(1,3) + W(2,3) + W(3,3));

There is one more thing I wish to know. Should I only optimize the function for target data rather than whole data?

Sorry fr the late response my friend. I can assure you, your support is worth a lifetime for me.

Thanks

Baqar

Stephan
on 17 Oct 2018

Edited: Stephan
on 17 Oct 2018

Can I ask, how are you getting only three values there should be 9 values.

Since W is your variable which values are changed by ga/gamultiobj and you set nvars = 3 in the call of ga, the size of W is 1x3:

W =

-0.8799 -0.9225 2.3144

This are the initial values for W sended from ga to your objective function.

------------------------------------------------------------------------

we also have to take care of the denominator in the equation that we are considering, It should be

c(1,:) = [*********]/(W(1,1) + W(2,1) + W(3,1));

c(2,:) = [*********]/(W(1,2) + W(2,2) + W(3,2));

c(3,:) = [*********]/(W(1,3) + W(2,3) + W(3,3));

This sounds like you expect W to be of size 3x3 - it isnt. Should it be?

BR
on 17 Oct 2018

Edited: BR
on 17 Oct 2018

Yes, you're right. W is a matrix of 3x3. But if, I consider given form of equation, I am getting the same error 'Index exceeds matrix dimensions'

Umm, Another thing is since using these weights, my results are not proper, so I think we might have to change the fitness function. Rather than minimizing 's', I should minimize error or maximize accuracy instead. This is something I plan to do next.

Stephan
on 17 Oct 2018

Edited: Stephan
on 17 Oct 2018

If you need W as a 3x3 matrix, then set nvars = 9, this will give you an initial W:

W =

-0.8799 -0.9225 2.3144 0.5880 -0.3098 -1.0330 1.1034 0.3421 -2.2640

Then you could rearrange this W to 3x3 in your objective function:

function s = DCA_weights(W, data, target)

W = reshape(W,3,3);

c(:,1)=0.5*(W(:,1) * data (target,1))'/(W(1,1) + W(2,1) + W(3,1));

c(:,2)=0.5*(W(:,2) * data (target,2))'/(W(1,2) + W(2,2) + W(3,2));

c(:,3)=0.5*(W(:,3) * data (target,3))'/(W(1,3) + W(2,3) + W(3,3));

s=c(2)./c(3);

end

to get W in a quadratic form:

W =

-0.8799 0.5880 1.1034

-0.9225 -0.3098 0.3421

2.3144 -1.0330 -2.2640

But now you again have the Problem that for example

data (target,1))

has a size of 6x1:

size_data =

0.7779

1.6822

3.1000

-2.3891

3.9900

-0.9830

which can not be multiplicated with each other due to the rules of linear algebra. So i suggest you care about if

data (target,1))

data (target,2))

data (target,3))

can be correct. This is how i would start. Which size of matrix do you expect from this multiplication? What does this mean for the size of data(target,1)?

BR
on 17 Oct 2018

Edited: BR
on 17 Oct 2018

Another Important fact that i wish to highligt is, if you calculate the values of c(1,:), c(2,:) and c(3,:) with the optimized weights is

-1.71500000000000 -1.71500000000000 -1.71500000000000

1.70000000000000 1.70000000000000 1.70000000000000

1.55000000000000 1.55000000000000 1.55000000000000

-2.00500000000000 -2.00500000000000 -2.00500000000000

1.99500000000000 1.99500000000000 1.99500000000000

2.10000000000000 2.10000000000000 2.10000000000000

i.e all are equal for a row meaning that the ratio it minimized was only unitll '1'. That hinders with the algorithm.

The chnage that I then made in DCA_weights (fitness function) was

function s = DCA_weights(a,b,c, data, target)

x = data(target,:);

c(1)=0.5*(a(1) * x(:,1) + a(3) * x(:,3) + a(2) * x(:,2))/(a(1) + a(2) + a(3));

c(2)=0.5*(b(1) * x(:,1) + b(3) * x(:,3) + b(2) * x(:,2))/(b(1) + b(2) + b(3));

c(3)=0.5*(c(1) * x(:,1) + c(3)* x(:,3) + c(2) * x(:,2))/(c(1) + c(2) + c(3));

s=c(3)./c(2);

where

a(1)= W(1,1);

a(2)= W(2,1);

a(3)= W(3,1);

b(1) = W(1,2);

b(2) = W(2,2);

b(3) = W(3,2);

c(1) = W(1,3);

c(2) = W(2,3);

c(3) = W(3,3);

to solve the above 9 variable problem for W. And this time the error was

Not enough input arguments.

Error in DCA_weights (line 9)

x = data(target,:);

This is something that intrigues me the most, why an error at

x = data(target,:);

and not at

c(1)=0.5*(a(1) * x(:,1) + a(3) * x(:,3) + a(2) * x(:,2))/(a(1) + a(2) + a(3));

Thanks

Stephan
on 17 Oct 2018

Edited: Stephan
on 17 Oct 2018

Dont do this:

function s = DCA_weights(a,b,c, data, target)

use:

function s = DCA_weights(W, data, target)

W = reshape(W,3,3);

a(1)= W(1,1);

a(2)= W(2,1);

a(3)= W(3,1);

b(1) = W(1,2);

b(2) = W(2,2);

b(3) = W(3,2);

c(1) = W(1,3);

c(2) = W(2,3);

c(3) = W(3,3);

x = data(target,:);

c(1)=0.5*(a(1) * x(:,1) + a(3) * x(:,3) + a(2) * x(:,2))/(a(1) + a(2) + a(3));

c(2)=0.5*(b(1) * x(:,1) + b(3) * x(:,3) + b(2) * x(:,2))/(b(1) + b(2) + b(3));

c(3)=0.5*(c(1) * x(:,1) + c(3)* x(:,3) + c(2) * x(:,2))/(c(1) + c(2) + c(3));

s=c(3)./c(2);

end

Stephan
on 17 Oct 2018

For me it worked - beside the error of:

Unable to perform assignment because the left and right sides have a different number of elements.

Error in DCA_weights (line 23)

c(1)=0.5*(a(1) * x(:,1) + a(3) * x(:,3) + a(2) * x(:,2))/(a(1) + a(2) + a(3));

My kids are home now - ill come back tomorrow...

BR
on 17 Oct 2018

Edited: BR
on 17 Oct 2018

I think its because there are 2 c's involved, so I implemented it like this and it worked.

function s = DCA_weights(W, data, target)

x = data(target,:);

W = reshape(W,3,3);

a(1)= W(1,1);

a(2)= W(2,1);

a(3)= W(3,1);

b(1) = W(1,2);

b(2) = W(2,2);

b(3) = W(3,2);

d(1) = W(1,3);

d(2) = W(2,3);

d(3) = W(3,3);

c(1,:)=0.5*(a(1) * x(:,1) + a(3) * x(:,3) + a(2) * x(:,2))/(a(1) + a(2) + a(3));

c(2,:)=0.5*(b(1) * x(:,1) + b(3) * x(:,3) + b(2) * x(:,2))/(b(1) + b(2) + b(3));

c(3,:)=0.5*(d(1) * x(:,1) + d(3)* x(:,3) + d(2) * x(:,2))/(d(1) + d(2) + d(3));

But anyways, we can discuss this later, Family first

Cheers mate, see you later.

Take care.

Stephan
on 17 Oct 2018

Edited: Stephan
on 17 Oct 2018

I would suggest to change c and d to keep it a bit easier:

function s = DCA_weights(W, data, target)

W = reshape(W,3,3);

a(1)= W(1,1);

a(2)= W(2,1);

a(3)= W(3,1);

b(1) = W(1,2);

b(2) = W(2,2);

b(3) = W(3,2);

c(1) = W(1,3);

c(2) = W(2,3);

c(3) = W(3,3);

x = data(target,:);

d(:,1)=0.5*(a(1) * x(:,1) + a(3) * x(:,3) + a(2) * x(:,2))/(a(1) + a(2) + a(3))

d(:,2)=0.5*(b(1) * x(:,1) + b(3) * x(:,3) + b(2) * x(:,2))/(b(1) + b(2) + b(3));

d(:,3)=0.5*(c(1) * x(:,1) + c(3)* x(:,3) + c(2) * x(:,2))/(c(1) + c(2) + c(3));

s=d(3)./d(2);

end

Since the result of the RHS is a 6x1 vector for d, i changed d(1,:) to d(:,1).

Running this code gives no errors - but this is not a proof that this result i correct.

Add this lines after your ga call:

x = reshape(x,3,3)

fval

to see results directly. Also adapt bounds to 9 variables:

LB = [-6 -6 -6 -6 -6 -6 -6 -6 -6]; % Lower bound

UB = [6 6 6 6 6 6 6 6 6]; % Upper bound

Now you get big negative values for fval and x is a 3x3 matrix

BR
on 17 Oct 2018

Edited: BR
on 17 Oct 2018

I dont understand 'How am I getting now 'x' as a matrix of 70*3??

Did you used the same constraint as I am using

function [c, ceq] = DCA_constraint_GA(W, data, target)

c = W(:,1)'*(9.6 - data(target,1))' + W(:,2)'*(9.6 - data(target,2))' + W(:,3)'*(9.6 - data(target,3))';

ceq=[];

BR
on 18 Oct 2018

Edited: BR
on 18 Oct 2018

Hey mate, apologies for the late reply. Extremely sorry, All day with my supervisor.

No, it didn't work, probably because, I think there's another change I made, that's the way 's' is calculated.

I changed it to

s = d(:,3)./d(:,2);

Coz, s = d(3)/d(2) would be picking values only from first column of 'd' rather.

But then again, this shouldn't effect the results. It should still be 3*3 which I get only if I calculate 's' in the latter way. Nevertheless, even if I use my output (where I consider the final row vector of x, which is 70 *9 in size, as the optimized weights, reshaped in 3*3) to calculate the final result, after some ablation, I am getting improved accuracy.

For this the way i proceeded was instead of following the original Dendritic cell algorithm which compares the two C(smDC) and C(mDC), [which in our code were d(:,3) and d(:,2)] respectively, and if C(mDC) is greater than C(smDC), we call it as anomaly. But since even for optimization and for calculating the final values of 'd' (in DCA_weights) after optimization it was found that all three values were equal for the optimized weights.

So I changed the condition for the anomaly here that instead of comparing the two outputs (C(smDC) and C(mDC) OR [d(:,3) and d(:,2)]), we looked for the two where they are equal. This improved our detection accuracy to 100% True Positives and 0% FP.

Thanks & regards

Baqar

### More Answers (0)

### See Also

### Categories

### Community Treasure Hunt

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

Start Hunting!**An Error Occurred**

Unable to complete the action because of changes made to the page. Reload the page to see its updated state.

Select a Web Site

Choose a web site to get translated content where available and see local events and offers. Based on your location, we recommend that you select: .

You can also select a web site from the following list:

## How to Get Best Site Performance

Select the China site (in Chinese or English) for best site performance. Other MathWorks country sites are not optimized for visits from your location.

### Americas

- América Latina (Español)
- Canada (English)
- United States (English)

### Europe

- Belgium (English)
- Denmark (English)
- Deutschland (Deutsch)
- España (Español)
- Finland (English)
- France (Français)
- Ireland (English)
- Italia (Italiano)
- Luxembourg (English)

- Netherlands (English)
- Norway (English)
- Österreich (Deutsch)
- Portugal (English)
- Sweden (English)
- Switzerland
- United Kingdom (English)

### Asia Pacific

- Australia (English)
- India (English)
- New Zealand (English)
- 中国
- 日本Japanese (日本語)
- 한국Korean (한국어)