Permalink
Browse files

Made sparsity pattern code more clear

The sparsity pattern data is provided in the solution only for
visualization. If the user defines the analytic gradients, then
they aleady know the sparsity pattern. For numerical gradients,
FMINCON determines the pattern internally.
  • Loading branch information...
MatthewPeterKelly committed Sep 16, 2016
1 parent 17b092a commit 005d957126ee02fcca37f32a97d902796654a96b
Showing with 17 additions and 7 deletions.
  1. +2 −2 demo/gradientsTutorial/MAIN.m
  2. +15 −5 directCollocation.m
@@ -41,13 +41,13 @@
%%%% Switch between a variety of methods
% method = 'trapezoid';
% method = 'trapGrad';
method = 'trapGrad';
% method = 'hermiteSimpson';
% method = 'hermiteSimpsonGrad';
% method = 'chebyshev';
% method = 'rungeKutta';
% method = 'rungeKuttaGrad';
method = 'gpops';
% method = 'gpops';
View
@@ -65,7 +65,7 @@
P.objective = @(z)( ...
myObjGrad(z, pack, F.pathObj, F.bndObj, F.weights, gradInfo) ); %Analytic gradients
[~, objGradInit] = P.objective(zGuess);
sparsityPattern.objective = (objGradInit~=0)';
sparsityPattern.objective = (objGradInit~=0)'; % Only used for visualization!
else
P.objective = @(z)( ...
myObjective(z, pack, F.pathObj, F.bndObj, F.weights) ); %Numerical gradients
@@ -74,8 +74,8 @@
P.nonlcon = @(z)( ...
myCstGrad(z, pack, F.dynamics, F.pathCst, F.bndCst, F.defectCst, gradInfo) ); %Analytic gradients
[~,~,cstIneqInit,cstEqInit] = P.nonlcon(zGuess);
sparsityPattern.equalityConstraint = (cstEqInit~=0)';
sparsityPattern.inequalityConstraint = (cstIneqInit~=0)';
sparsityPattern.equalityConstraint = (cstEqInit~=0)'; % Only used for visualization!
sparsityPattern.inequalityConstraint = (cstIneqInit~=0)'; % Only used for visualization!
else
P.nonlcon = @(z)( ...
myConstraint(z, pack, F.dynamics, F.pathCst, F.bndCst, F.defectCst) ); %Numerical gradients
@@ -109,8 +109,18 @@
soln.info.nlpTime = nlpTime;
soln.info.exitFlag = exitFlag;
soln.info.objVal = objVal;
if flagGradCst || flagGradObj
soln.info.sparsityPattern = sparsityPattern;
if flagGradCst || flagGradObj % Then return sparsity pattern for visualization
if flagGradObj
[~, objGradInit] = P.objective(zSoln);
sparsityPattern.objective = (objGradInit~=0)';
end
if flagGradCst
[~,~,cstIneqInit,cstEqInit] = P.nonlcon(zSoln);
sparsityPattern.equalityConstraint = (cstEqInit~=0)';
sparsityPattern.inequalityConstraint = (cstIneqInit~=0)';
end
soln.info.sparsityPattern = sparsityPattern;
end
soln.problem = problem; % Return the fully detailed problem struct

0 comments on commit 005d957

Please sign in to comment.