Fixing a value inside the objective function while using FMINCON

Hi,
I am using 'fmincon' inside a script 'Script.m' optimizing the output of the function 'cost_simplified' based on the independent variable 'q' ( i have provided the code script). But while varying the values of 'q' I need to keep the value of a double which is dependent on 'q' i.e., 'X.pa' same as 'contact_points' which is a constant taken as input ( i.e. i need to satisfy 'X.pa==contact_points', and then proceed further inside the function 'cost_simplified').
How can i meet this constrain ('X.pa==contact_points') inside the objective function 'cost_simplified' ?
I have tried for 'if ~isequal(X.pa,contact_points) return end' . But using 'return' brings out the error "Output Argument "..." not assigned during call to "Script.m>cost_simplified". If i assign output argument to 'zero' , optimization algorithm 'fmincon' stops as the ouput 'zero' remains constant after every iteration.
nonlincon=@(q) graspcon(q,G,contact_points,Kc_new,X); %%% concept of extra parameters
[q{i},cost_val_new(i)]= fmincon (@(q) cost_simplified(w,yopt,q,G,contact_points,object.normals,mu,fmin,fmax,k,X,object),q0,A,b,Aeq,beq,lb,ub,nonlincon);

3 Comments

Why do you need to constraint it inside the objective function? Why not enforce it in the usual way, using nonlincon (if it is nonlinear) or with Aeq,beq if it is linear?
Because 'X.pa' is a function of the new 'q'. Everytime 'q' iterates, 'X.pa' changes. But for some values of 'q' , 'X.pa == contact_points" , only for those values of 'X.pa' i need to continue the optimization.
I have provided the sample code upto which i should check the equality constraint
function [cost] = cost_simplified(w,y,q,G,contact_points,n,mu,fmin,fmax,k,X,object)
X = moveHand(X,q);
X = addtipContact(X,1,1:4);
if ~isequal(X.pa,contact_points)
return
end
%%%% then proceed further in the code

Sign in to comment.

 Accepted Answer

Matt J
Matt J on 30 Jun 2019
Edited: Matt J on 30 Jun 2019
You should not try to abort your objective function just because you know that the q fmincon is in the process of evaluating doesn't satisfy intended constraints. Preventing fmincon from completing its evaluation of q is denying it the very information it needs to search for a solution.

7 Comments

After the command line "X = addtipContact(X,1,1:4);" , "X.pa" is 7*4 double and "contact_points" is a 7*4 double too. How can i proceed to implement it as a constrain, i am not getting this part. I am browsing through the documentation, but still in vain.
function [cost] = cost_simplified(w,y,q,G,contact_points,n,mu,fmin,fmax,k,X,object)
X = moveHand(X,q);
X = addtipContact(X,1,1:4);
% if ~isequal(X.pa,contact_points)
% return
% end
%%%% then proceed further in the code
Is it a non-linear, differentiable constraint? If so, just put it inside your nonlincon function. That's what the nonlincon function is meant for.
I get the following error when i add it in non-linear constraint ceq(2) "Unable to perform assignment because the indices on the left side are not compatible with the size of the right side."
function [c,ceq] = graspcon(q,G,contact_points,Kc_new,X)
X = moveHand(X,q);
%
X = addFtipContact(X,1,1:4);
% if ~isequal(X.pa,contact_points)
% return
% end
X.Jtilde = jacobianMatrix(X);
X.J = X.H * X.Jtilde;
c=[];
ceq(1)=null((inv(pinv(Kc_new) + X.J*pinv(X.Kq)*hand.J' + X.J*X.S*pinv(X.Kz)*X.S'*X.J'))*G')-0;
ceq(2)=X.pa-contact_points;
end
con1=null((inv(pinv(Kc_new) + X.J*pinv(X.Kq)*hand.J' + X.J*X.S*pinv(X.Kz)*X.S'*X.J'))*G')-0;
con2=X.pa-contact_points;
ceq=[con1(:);con2(:)]
However, your first constraint can never be satisfied. The output of null() is never zero. It is also not a differentiable function
I have this constraint in the form " nullspace(K*G') = 0" . So i put it in the non-linear constraint under nonlincon.
nullspace is a command from the Symbolic Math Toolbox. Your nonlcon function must not return symbolic results.
Thank You very much Sir, the constraint no. 2 is getting satisfied in the final results. Regarding constraint 1, i have changed the code to
con1=double(null(sym((inv(pinv(Kc_new) + X.J*pinv(X.Kq)*X.J' + X.J*X.S*pinv(X.Kz)*X.S'*X.J'))*G')))

Sign in to comment.

More Answers (0)

Products

Release

R2019a

Community Treasure Hunt

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

Start Hunting!