MATLAB GA error help!

Note: Updated the files and error message
Like the Subject says, I need help in sorting out the errors. Basically I'm trying to find the optimal bidding coefficients bd with the purpose of enhancing the profit F for the 2nd player (the 2 in Custom_Fitness_Fcn). Now the problem is that whenever I try to run main I get these errors:
>> main
In an assignment A(:) = B, the number of elements in A and B must be the same.
Error in Custom_Constraint_Fcn (line 10)
P(n) = (R-ac(n))/bd(n);
Error in main>@(bd)Custom_Constraint_Fcn(ac,bd,limit,i,l,Q0,K)
Error in createAnonymousFcn>@(x)fcn(x,FcnArgs{:}) (line 11)
fcn_handle = @(x) fcn(x,FcnArgs{:});
Error in constrValidate (line 21)
[tmpCineq,tmpCeq] = nonlcon([Iterate.x Iterate.x]');
Error in gacommon (line 125)
[LinearConstr, Iterate,nineqcstr,neqcstr,ncstr] = constrValidate(NonconFcn, ...
Error in ga (line 336)
NonconFcn,options,Iterate,type] = gacommon(nvars,fun,Aineq,bineq,Aeq,beq,lb,ub, ...
Error in main (line 9)
[bd,F] = ga(FitnessFcn,nvars,[],[],[],[],[],[],ConstraintFcn,options);
Caused by:
Failure in initial user-supplied nonlinear constraint function evaluation.
>>
I've been trying to debug this thing for days, and I really need it in less than 2. Somebody please help!

14 Comments

need dataread as well as it initializing the key variables
sorry, I forgot, here:
UPDATE:
I've managed to isolate what is hopefully the root cause: I'm passing a 2 x 1 bd, instead of the expected (#ofpopulations) x 8 size bd. Can someone comment on what I've been doing wrong with the Custom_Creation_Fcn?
Capture.PNG
Your custom create function internally creates aa matrix of values. It extracts column 2 and transposes. The result is a single row . Therefore your population size is 1.
by the way the second parameter that ga passes to the create function is the fitness function . Your anonymous wrapper calls thar objective function and ignores it and passes fitness function in to your custom function . Which ignores it anyhow .
>Your custom create function internally creates aa matrix of values. It extracts column 2 and transposes. The result is a single row . Therefore your population size is 1.
Yes, I want this particular column to be transformed into a row vector. I need this to be an 1 x 8 row vector though, but it keeps on giving me a 2 x 1 column vector instead with the breakpoint at the first executable of the constraint like in the image above.
>by the way the second parameter that ga passes to the create function is the fitness function . Your anonymous wrapper calls thar objective function and ignores it and passes fitness function in to your custom function . Which ignores it anyhow .
I've fixed it. I could've sworn I cleared that out; must be the 12 hour shifts taking their toll. But anyway, since, editing those (all of the m files) the subscript mismatch has turned into :
In an assignment A(:) = B, the number of elements in A and B must be the same.
I still think its related to bd not being the expected row vector size. Thoughts?
not sure at the moment .
Why does your comment in the constraint function say it generates a column vector of constraints ? you are horzcat aa series of column vectors so you get a 2d array.
why is your genome length only 1 ? The genome length is the number of variables and so the length of bd but the length of bd needs to match the length of ac because you index those two together .
>not sure at the moment .
>Why does your comment in the constraint function say it generates a column vector of constraints ? you are horzcat aa series of column vectors so you get a 2d array.
Ooops. I've fixed it, but now it doesn't like vertcat:
Error using vertcat
Dimensions of matrices being concatenated are not consistent.
I attach these two images of the constraint equations since I'm not sure if I'm making much sense in trying to convey what I want for the constraint function:
1.PNG
>why is your genome length only 1 ? The genome length is the number of variables and so the length of bd but the length of bd needs to match the length of ac because you index those two together .
I changed it. This is really embarassing - I'm just so tired that I can't even pick up on these simple things. I've half a mind to just call in sick from work for the rest of the week.
BTW, thanks for helping me out here, man, it really means a lot.
Also I'm considering just ditching the CreationFcn, and just use an initial population for bd instead:
for m = 1:60
for n = player_no
Random_Numbers(n,:) = mvnrnd(mu(:,n),cov(:,:,n),1);
end
rd(m,:) = Random_Numbers(:,2).';
Random_Numbers2(m,:) = rd(m,:);
end
bd = Random_Numbers2;
I've removed the creation function, and just coded an initial population. Now the problem is that a similar error occurs:
Error using vertcat
Dimensions of matrices being concatenated are not consistent.
Error in Custom_Constraint_Fcn (line 21)
c = [Gen_Limit_max; Gen_Limit_min; Lod_Limit_max; Lod_Limit_min]; %generates a column vector of nonlinear
constraints
Error in main>@(bd)Custom_Constraint_Fcn(ac,bd,limit,i,l,Q0,K)
Error in createAnonymousFcn>@(x)fcn(x,FcnArgs{:}) (line 11)
fcn_handle = @(x) fcn(x,FcnArgs{:});
Error in constrValidate (line 21)
[tmpCineq,tmpCeq] = nonlcon([Iterate.x Iterate.x]');
Error in gacommon (line 125)
[LinearConstr, Iterate,nineqcstr,neqcstr,ncstr] = constrValidate(NonconFcn, ...
Error in ga (line 336)
NonconFcn,options,Iterate,type] = gacommon(nvars,fun,Aineq,bineq,Aeq,beq,lb,ub, ...
Error in main (line 10)
[bd,F] = ga(FitnessFcn,nvars,[],[],[],[],[],[],ConstraintFcn,options);
Caused by:
Failure in initial user-supplied nonlinear constraint function evaluation.
>>
Putting a breakpoint right at the very beginning of the constraint function yields a 2x8 size for bd. I suspect this has something to do with the 8x2 size limit() as in the matrix, but for the life of me I can't think of a fix.
bd:
0.0137725212504241 0.0612786475132243 0.161591023828413 0.0306411601964610 0.0911091201633450 0.0915716761288406 0.0489777513108497 0.0343236327910784
0.0137725212504241 0.0612786475132243 0.161591023828413 0.0306411601964610 0.0911091201633450 0.0915716761288406 0.0489777513108497 0.0343236327910784
The first two matrices being vertcat are i(end) by max(i) with all rows having the same value.
The third and fourth are l(end)-i(end) by max(l) with all rows having the same value.
You will not be able to horzcat or vertcat this collection together.
>The first two matrices being vertcat are i(end) by max(i) with all rows having the same value.
The third and fourth are l(end)-i(end) by max(l) with all rows having the same value.
You will not be able to horzcat or vertcat this collection together.
Ok. But before that I think my main issue is that for some reason the bd that gets passed to the constraint consists of this weird size and value that I have no idea where it came from [see images]. I'm weary of maybe my fitness function handle messing up the bd and from there it goes on to be passed to the constraint instead (If I haven't misunderstood, the function handle call "locks in" the variables it calls, is that right?).
Anyway, now that I've used a generated initial population instead of a custom creation function, is my code for the constraint correct? Or do I need to vectorialize some lines like R = ...?
[The wrong one is the first pic, and the correct bd value is the second pic]
NmhcVxh.png
D466bF2.png
your posted attached custom constraint function passes ac bd . your debugging snapshot shows aa custom constraint that expects bd ac.

Sign in to comment.

Answers (0)

Categories

Find more on Loops and Conditional Statements in Help Center and File Exchange

Tags

Asked:

on 5 Dec 2018

Commented:

on 6 Dec 2018

Community Treasure Hunt

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

Start Hunting!