I am running an optimization problem with MATLAB’s fmincon, and I get the error
**Error using saveOptimizationParams
Too many input arguments.
Error in sqpInterface
Error in fmincon (line 672)
`[X,FVAL,EXITFLAG,OUTPUT,LAMBDA,GRAD,HESSIAN] = sqpInterface(funfcn,X,full(A),full(B),full(Aeq),full(Beq), …
**
The problem is to minize obj = (alpha * populationDeviation) + (beta * locationDeviation);
The is is my optimization problem
cd('/main/path')
path2root= '/path/to/something/I need/';
% Load initial parameters from file
initial_params = readParameters('parameters.txt');
% Load target data from file
target_population = load('/path/to/targetPop.txt');
% Define optimization parameters
alpha = 1; % Weight for population size deviation
beta = 1; % Weight for location deviation
% Define the optimization problem
objFunction = @(params) objectiveFun(params, target_population,alpha,beta);
initial_guess = [initial_params.natural_growth_rate, initial_params.natural_death_rate ];
lb = [0, 0];
ub = [Inf, Inf];
options = optimoptions('fmincon', 'Display', 'iter', 'Algorithm', 'sqp','OutputFcn', @saveOptimizationParams);
% Perform optimization
[opt_params, fval] = fmincon(@(params)objectiveFun(params, target_population,alpha,beta), initial_guess, [], [], [], [], lb, ub, [], options);
This is my objective function.
function obj = objectiveFun(params,target_population,alpha,beta)
growth_rate = params(1);% Currently not used
death_rate = params(2);% Currently not used
% Read the optimized parameters to a new file
initial_guess = readParameters('parameters.txt');
%initial_guess=[initial_params.growth_rate,initial_params.death_rate];
%Run ABM from jar fiel
system( 'java -jar /path/to/file.jar/')
% Load updated data from file
current_population = readCurrentPop('/path/to/data.csv');
% Calculate population deviation
P_current = size(current_population, 1);
P_target = size(target_population, 1);
populationDeviation = (P_current - P_target)^2;
% Calculate location deviation using the K nearest neigbor search
[Idx, D] = knnsearch(target_population, current_population);
locationDeviation=sum(D);
% Calculate combined objective
obj = (alpha * populationDeviation) + (beta * locationDeviation);
end
And lastly this is my output function
function stop = saveOptimizationParams(optimValues, state)
% File to write
fileName = '/path/to/parameters.txt';
% Initialize stop flag
stop = false;
% Check the state of the optimization
switch state
case 'iter'
% Write the parameters to the file at each iteration
currentParams = optimValues.x;
paramCellArray = struct('natural_growth_rate', currentParams(1), 'natural_death_rate', ocurrentParams(2));
writeParameters(fileName, paramCellArray);
end
end
Tried: I checked that the objective function (objectiveFun), and the output function (saveOptimizationParams) run when by themselves when tested separately
The optimization also runs when I exclude the output function part from options, that is
options = optimoptions('fmincon', 'Display', 'iter', 'Algorithm', 'sqp');
However if I include the output function, the error occurs, even if the function is just dummy with nothing inside.
I use the output function to update my data at each step and so I want to run it at every iteration.
If anyone has any suggestion, or know what I am missing, I’d appreciated it. Thanks for reading