Why Am I getting this warning about SNOPT derivative structure
2 views (last 30 days)
Show older comments
Warning: User is not providing SNOPT derivatives structures
> In snopt (line 334)
In lambert4_64bit (line 245)
Warning: Derivatives provided but not structures: estimating structure via snJac.
> In snopt (line 347)
In lambert4_64bit (line 245)
0 Comments
Answers (1)
Anurag Ojha
on 19 Jun 2024
Hi Mudrik
The warning message indicates that the SNOPT solver expects specific derivative structures for the objective function and constraints but did not receive them. Instead, SNOPT is estimating these structures via the snJac function.
In optimization, providing derivative (Jacobian and Hessian) information is crucial for solvers to efficiently and accurately find solutions. When using SNOPT in MATLAB, it is beneficial to provide these derivatives explicitly. The structure of these derivatives should match what SNOPT expects. If not provided, SNOPT will try to estimate them, which can lead to inefficiencies or inaccuracies.
To resolve this warning, you should provide the derivatives explicitly if possible. If your objective function and constraints are smooth and differentiable, you can manually calculate and provide these derivatives. If not, MATLAB will attempt to estimate them.
Here's how you can modify your MATLAB code to address this
% Define the objective function and its gradient
function [f, gradf] = myObjective(x)
% Example objective function: Rosenbrock's function
f = 100*(x(2) - x(1)^2)^2 + (1 - x(1))^2;
% Gradient of the objective function
gradf = [-400*x(1)*(x(2)-x(1)^2) - 2*(1-x(1));
200*(x(2)-x(1)^2)];
end
% Define the constraints and their gradients
function [c, ceq, gradc, gradceq] = myConstraints(x)
% Example constraints: Inequality and Equality constraints
c = [x(1)^2 + x(2)^2 - 1]; % Inequality constraint (circle of radius 1)
ceq = [x(1) + x(2) - 1]; % Equality constraint (line x1 + x2 = 1)
% Gradient of the inequality constraint
gradc = [2*x(1); 2*x(2)];
% Gradient of the equality constraint
gradceq = [1; 1];
end
% Main script to solve the optimization problem
function main()
% Define the initial guess
x0 = [0.5; 0.5];
% Set optimization options
options = optimoptions('fmincon', ...
'SpecifyObjectiveGradient', true, ...
'SpecifyConstraintGradient', true, ...
'Algorithm', 'sqp'); % 'sqp' uses SNOPT under the hood
% Solve the optimization problem
[x, fval, exitflag, output] = fmincon(@myObjective, x0, [], [], [], [], [], [], @myConstraints, options);
% Display the optimization results
disp(['Optimization exit flag: ' num2str(exitflag)]);
disp(['Optimized solution: ' num2str(x')]);
disp(['Objective function value: ' num2str(fval)]);
end
% Call the main function to execute the optimization
main();
0 Comments
See Also
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!