Solver based optimization passing extra parameters

My solver function is giving an error: Unrecognized function or variable 'aa'.
c = sum(1./(1+exp(-x))*1./(1+exp(-y')).*(1.-aa),'all') - 0.1;
Error in fmincon (line 654)
[ctmp,ceqtmp] = feval(confcn{3},X,varargin{:});
Caused by:
Failure in initial nonlinear constraint function evaluation. FMINCON cannot continue.
Objective function is:
function f = objectiveFcn(optimInput)
x = optimInput(1:8124);
y = optimInput(8125:end);
f = -sum(1./(1+exp(-x))*1./(1+exp(-y')),'all');
end
function [c,ceq] = constraintFcn(optimInput)
x = optimInput(1:8124);
y = optimInput(8125:end);
c = sum(1./(1+exp(-x))*1./(1+exp(-y')).*(1.-aa),'all') - 0.1;
ceq = [];
end
I am using the solver based approach. Can you please tell me how can I pass the 'aa' variable inside the constraint function. I have seen the mathworks page but I couldn't implement it to my case. Can anyone please show me how can I do it for the solver based approach for my case.

답변 (1개)

Matt J
Matt J 2021년 9월 17일
편집: Matt J 2021년 9월 17일
nonlcon=@(x)constraintFcn(x,aa)

댓글 수: 4

I have seen this page but still couldn't work it out. It's I guess because I can not figure out how to do it for the solver GUI. For example, in the optimizer GUI solver is already given like
[solution,objectiveValue] = fmincon(@objectiveFcn,w,[],[],[],[],[],[],...
@constraintFcn,options4);
Here nonlcon = @constraintFcn but the solver doesn't allow me to change it. So I am not able to write what you said. I am trying to change constraintFcn itself to have the aa variable as an input such as function [c,ceq] = constraintFcn(optimInput,aa), but I get the Not enough input arguments then.
What I understand from the Passing extra parameters page maybe I need to write an extra function? Can you please ellobrate a bit more?
Matt J
Matt J 2021년 9월 17일
편집: Matt J 2021년 9월 17일
You should be writing this:
aa=something;
nonlcon=@(x)constraintFcn(x,aa);
[solution,objectiveValue] = fmincon(@objectiveFcn,w,[],[],[],[],[],[],...
nonlcon,options4);
Sorry to bother you again but you see the solver GUI doesn't allow me to change fmincon function. Curser is a ghost as you can see. I can only change the constraintFcn but as you can see, @(x)constraintFcn(x,aa) writing is not working. I also tried to change it to nonlcon=@(x)constraintFcn(x,aa) to change the inside of the fmincon as you said but again this solver GUI is not letting me touch the fmincon.
If you right-click on the task, you can convert it to editable code:

댓글을 달려면 로그인하십시오.

카테고리

제품

릴리스

R2021a

질문:

2021년 9월 17일

댓글:

2021년 9월 17일

Community Treasure Hunt

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

Start Hunting!

Translated by