Error using Nonlcon: "Not Enough Input Arguments"
Afficher commentaires plus anciens
Attempting to apply non linear constraints to my ga optimization, however, I keep recieving this error:

I can't seem remedy this issue. The design vector should be a 1x40 vector x and is what is being fed to nonlcon. Hope someone more experienced can shed some light on my problem. I attached my main script and nonlcon function below. Thanks.
Main Script:
%% Male Macro/Caloric Requirements & Optimization
clear all
clc
% Physical attributes that determine macros/calorie requirements for avg.
% Male: Age=25/Height=5'9"/Weight=197.9 lb/Goal=Lose 0.5 lb per week/ % Activity Level=Lightly Active 3 times per week
% Macro/Caloric requirements
% Protein: 142g
% Carbs: 310g
% Fats: 66g
% Calories: 2,325 calories
% fitnessfcn is the cost function that calculates the grocery cost.
fitnessfcn = @(x) 0.23*x(1)+0.16*x(2)+1.1*x(3)+0.39*x(4)+1.45*x(5)+0.22*x(6)+0.54*x(7)+0.84*x(8)+0.52*x(9)+3.24*x(10)+...
0.14*x(11)+0.53*x(12)+0.23*x(13)+0.29*x(14)+0.35*x(15)+1*x(16)+0.84*x(17)+0.26*x(18)+0.62*x(19)+0.17*x(20)+...
1.9*x(21)+0.85*x(22)+0.15*x(23)+0.32*x(24)+0.32*x(25)+...
0.4*x(26)+0.48*x(27)+0.15*x(28)+1.31*x(29)+1.19*x(30)+0.9*x(31)+0.14*x(32)+0.16*x(33)+0.3*x(34)+0.28*x(35)+...
0.79*x(36)+0.25*(37)+0.89*x(38)+1.38*x(39)+0.86*x(40);
% Number of design variables.
nvars = 40;
% Design variable definitions
% Dairy: x1=whole milk/x2=butter/x3=greek yogurt/x4=shredded cheddar cheese/x5=casein protein
% x26=cottage cheese/x27=ice cream/x28=sour cream
% Protein: x6=eggs/x7=bacon/x8=sausage links/x9=turkey sausage/x10=ribeye steak
% x29=chicken breasts/x30=ground beef/x31=ground turkey
% Grains: x11=oatmeal/x12=bagel/x13=whole wheat bread/x14=flour tortillas/x15=honey nut cheerios
% x32=brown rice/x33=white rice/x34=quinoa
% Vegetables: x16=white onion/x17=bell peppers/x18=portobello mushrooms/x19=spinach/x20=potatoe
% x35=broccolli/x36=lettuce/x37=cauliflower
% Fruit: x21=blueberries/x22=strawberries/x23=bananas/x24=mango/x25=avocado
% x38=granny smith apple/x39=tomatoe/x40=orange
% A*x <= b
% A=linear inequality coefficents
% b=linear ineqaulity constraints (right hand side values)
A=[-8,0,-16,-6,-24,-6,-4,-9,-9,-21,-5,-9,-4,-3,-3,-2,-1,-5,-2,-3,-1,-1,-1,0,-1,-14,-4.5,0,-19,-20,-19,-4,-4,-6,-1,-1,-2,0,-1,-1;... % Protein macro constraint
-12,0,-6,0,-3,0,0,-4,-5,0,-27,-53,-21,-16,-30,-15,-7,-6,-3,-26,-20,-11,-27,-27,-4,-3,-22.5,0,0,0,0,-34,-37,-28,-4,-3,-5,-29,-6,-22;... % Carb macro constraint
-8,-11,0,-9,-1,-5,-7,-14,-5,-23,-3,-1,-2,-2,-2,0,0,-1,0,0,0,0,0,0,-7,-1,-13.5,-6,-10,-23,-17,-1,0,-2,0,0,0,0,0,0;... % Fat macro constraint
-1,-1,-1,-1,-1,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,-1,-1,-1,0,0,0,0,0,0,0,0,0,0,0,0;... % Dairy variety constraint
0,0,0,0,0,-1,-1,-1,-1,-1,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,-1,-1,-1,0,0,0,0,0,0,0,0,0;... % Protein variety constraint
0,0,0,0,0,0,0,0,0,0,-1,-1,-1,-1,-1,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,-1,-1,-1,0,0,0,0,0,0;... % Grains variety constraint
0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,-1,-1,-1,-1,-1,0,0,0,0,0,0,0,0,0,0,0,0,0,0,-1,-1,-1,0,0,0;... % Vegetables variety constraint
0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,-1,-1,-1,-1,-1,0,0,0,0,0,0,0,0,0,0,0,0,-1,-1,-1;... % Fruit variety constraint
149,100,90,110,120,70,80,180,120,280,150,270,110,90,140,64,19,26,25,110,80,48,105,110,78,79,2.25,60,160,280,230,160,160,170,25,10,20,110,27,87;... %calorie upper limit
-149,-100,-90,-110,-120,-70,-80,-180,-120,-280,-150,-270,-110,-90,-140,-64,-19,-26,-25,-110,-80,-48,-105,-110,-78,-79,-2.25,-60,-160,-280,-230,-160,-160,-170,-25,-10,-20,-110,-27,-87]; %calorie lower limit
b=[-142;... % Protein macro constraint in grams
-310;... % Carbs macro constraint in grams
-66;... % Fats macro constraint in grams
-3;... % Dairy variety for three meals
-3;... % Protein variety for three meals
-3;... % Grains variety for three meals
-3;... % Vegetables variety for three meals
-3;... % Fruit variety for three meals
2475;... % Upper limit of calorie intake
-2175]; % Lower limit of calorie intake
% Aeq * x = beq
% Aeq=linear eqaulity coefficients
% beq=linear eqaulity constraints (right hand side values)
Aeq=[];
beq=[];
% Lower and upper bounds for all fourty design variables
Lb=[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];
Ub=[2,2,2,2,2,2,2,2,2,2,2,2,2,2,2,2,2,2,2,2,2,2,2,2,2,2,2,2,2,2,2,2,2,2,2,2,2,2,2,2];
intcon=1:40; % restricts all fourty design variables to only being able to be integers
[x,fval,exitflag,output,population] = ga(fitnessfcn,nvars,A,b,Aeq,beq,Lb,Ub,nonlcon,intcon);
% x: Best point that ga located during its iterations % fval: Fitness (objective) function evaluated at x
% exitflag: Integer giving the reason ga stopped iterating
% Positive Numbers = reached a convergence stop condition
% 0 = maximum number of generations exceceeded
% -2 = No feasible point found
% Other negative numbers = Prematurely terminated
% output: Structure containing output from each generation and other information % about algorithm performance % population: Matrix whose rows contain the members of the final population % plot fitness value vs. generations
opts = optimoptions(@ga,'PlotFcn',{@gaplotbestfun});
[X,Fval] = ga(fitnessfcn,nvars,A,b,Aeq,beq,Lb,Ub,nonlcon,intcon,opts);
% Calculate values of macronutrients (protein, carbs, fats), cost/solution, % and calories/solution from x (solution vector).
protein_macro_values_vector=[8,0,16,6,24,6,4,9,9,21,5,9,4,3,3,2,1,5,2,3,1,1,1,0,1,14,4.5,0,19,20,19,4,4,6,1,1,2,0,1,1]';
carb_macro_values_vector=[12,0,6,0,3,0,0,4,5,0,27,53,21,16,30,15,7,6,3,26,20,11,27,27,4,3,22.5,0,0,0,0,34,37,28,4,3,5,29,6,22]';
fat_macro_values_vector=[8,11,0,9,1,5,7,14,5,23,3,1,2,2,2,0,0,1,0,0,0,0,0,0,7,1,13.5,6,10,23,17,1,0,2,0,0,0,0,0,0]';
grocery_cost_values_vector=[0.23,0.16,1.1,0.39,1.45,0.22,0.54,0.84,0.52,3.24,0.14,0.53,0.23,0.29,0.35,1,0.84,0.26,0.62,0.17,1.9,0.85,0.15,0.32,0.32,0.4,0.48,0.15,1.31,1.19,0.9,0.14,0.16,0.3,0.28,0.79,0.25,0.89,1.38,0.86]';
calorie_values_vector=[149,100,90,110,120,70,80,180,120,280,150,270,110,90,140,64,19,26,25,110,80,48,105,110,78,79,2.25,60,160,280,230,160,160,170,25,10,20,110,27,87]';
% Solution vector -> [protein in g, carbs in g, fats in g, cost, calories].
solution_vector(1)=x*protein_macro_values_vector;
solution_vector(2)=x*carb_macro_values_vector;
solution_vector(3)=x*fat_macro_values_vector;
solution_vector(4)=x*grocery_cost_values_vector;
solution_vector(5)=x*calorie_values_vector;
nonlcon:
% Non-linear constraints for Non-Linear Constraint Example
function [ c,ceq ] = nonlcon(x)
c(1) = -(x(1)/x(1))-(x(2)/x(2))-(x(3)/x(3))-(x(4)/x(4))-(x(5)/x(5))-(x(26)/x(26))-(x(27)/x(27))-(x(28)/x(28))+3; % Diversity variety constraint dairy
c(2) = -(x(6)/x(6))-(x(7)/x(7))-(x(8)/x(8))-(x(9)/x(9))-(x(10)/x(10))-(x(29)/x(29))-(x(30)/x(30))-(x(31)/x(31))+3; % Diversity variety constraint protein
c(3) = -(x(11)/x(11))-(x(12)/x(12))-(x(13)/x(13))-(x(14)/x(14))-(x(15)/x(15))-(x(32)/x(32))-(x(33)/x(33))-(x(34)/x(34))+3; % Diversity variety constraint grains
c(4) = -(x(16)/x(16))-(x(17)/x(17))-(x(18)/x(18))-(x(19)/x(19))-(x(20)/x(20))-(x(35)/x(35))-(x(36)/x(36))-(x(37)/x(37))+3; % Diversity variety constraint vegetables
c(5) = -(x(21)/x(21))-(x(22)/x(22))-(x(23)/x(23))-(x(24)/x(24))-(x(25)/x(25))-(x(38)/x(38))-(x(39)/x(39))-(x(40)/x(40))+3; % Diversity variety constraint fruits
ceq=[];
end
Réponses (1)
[x,fval,exitflag,output,population] = ga(fitnessfcn,nvars,A,b,Aeq,beq,Lb,Ub,@nonlcon,intcon);
instead of
[x,fval,exitflag,output,population] = ga(fitnessfcn,nvars,A,b,Aeq,beq,Lb,Ub,nonlcon,intcon);
and
[X,Fval] = ga(fitnessfcn,nvars,A,b,Aeq,beq,Lb,Ub,@nonlcon,intcon,opts);
instead of
[X,Fval] = ga(fitnessfcn,nvars,A,b,Aeq,beq,Lb,Ub,nonlcon,intcon,opts);
Although I don't understand your constraints. x(i)/x(i) will always be 1 ...
2 commentaires
Brendan Wilhelm
le 29 Avr 2022
Torsten
le 29 Avr 2022
Test whether "intlinprog" instead of "ga" works for your problem. It will be way faster.
Catégories
En savoir plus sur Solver-Based Nonlinear Optimization dans Centre d'aide et File Exchange
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!