What happens when a custom made function needs an input for every evaluation in an iteration while optimising the function ?

1 view (last 30 days)
if sh > sum(Short)
err_sh = sh - sum(Short);
fprintf('Error : The current quntities ordered by contract and spot purchase has led to a delay in shortage \n more than maximum allowed delay in shortage of %d for %d units.\n\n\n', ms,err_sh);
fprintf('%d parts should be spot purchased in %dth month \n',err_sh,t);
choice = input('To increase the maximum allowable shortage enter 1 \nTo increase the the spot purchase Quantity 2 \n');
if choice ==1
ms1 = ms;
chec = 0;
while chec == 0
ms = input('enter the new allowable maximum delay in shortage \n') - 1;
if ms > ms1
chec = 1;
else
fprintf('Error : The new allowable maximum delay in shortage should be greater than the old allowable maximum delay in shortage %d /n',ms1);
end
end
%cost_test1(InboundS) is the functioin.
[Y,Z,K,S,H] = cost_test1(InboundS);
return
elseif choice == 2
InboundS(t) = InboundS(t) + err_sh;
fprintf('The new spot purchase matrix is %i\n',InboundS);
[Y,Z,K,S,H] = cost_test1(InboundS);
return
end
I am currently running a surrogate optimisation for my custom made function with an array of 12 decision variables. The custom made function depending on the 12 variable value might request for an input to run the function further. cost_test1(InboundS) is the functioin i am using. It asks for an input(which is stored as choice), if the sh > sum(Short) .The user have to enter a value for the input for the function to run.
How does matlab take care of it. When i try to optimise the funciton it doesnt ask me for an input ? What is happening ?
  4 Comments
dpb
dpb on 21 Jun 2021
I'll note that if this were to be to ask for user input every iteration it could get very tiring as it may take hundreds or even thousands of iterations to find a solution...I don't think this will turn out to be practical even if it does work as coded...
dpb
dpb on 21 Jun 2021
[Y,Z,K,S,H] = cost_test1(InboundS);
Also, you don't show the function definition nor the call to fsolve to see the problem definition, but the above makes one suspicious your function may not return a column vector of X for the solution to the general X==0 form expected by fsolve

Sign in to comment.

Answers (1)

dpb
dpb on 21 Jun 2021
I created a testing function that mimicked one just used in another Q? --
function T=testit(x)
persistent c
if isempty(c)
c=input('Enter constant: ');
end
T=1050/x-37.5-c;
end
This resulted in the following session
>> fsolve(@testit,20)
Enter constant: 6
Equation solved.
fsolve completed because the vector of function values is near zero
as measured by the value of the function tolerance, and
the problem appears regular as measured by the gradient.
<stopping criteria details>
ans =
24.1379
>>
which proves the code as written would work so, in fact, the if condition is not being satisfied.
But, as noted, this is not a practical way to try to do this -- it will not be feasible to enter these data manually during a solution -- for every iteration, the function will be call at least twice; maybe more depending on the shape...
>> opt=optimoptions('fsolve','Display','iter');
>> fsolve(@testit,20,opt)
Norm of First-order Trust-region
Iteration Func-count f(x) step optimality radius
0 2 81 23.6 1
1 4 42.25 1 15.5 1
2 6 1.39441 2.5 2.25 2.5
3 8 0.000924441 0.621071 0.0549 6.25
4 10 4.50351e-10 0.0168478 3.82e-05 6.25
5 12 1.00089e-22 1.17757e-05 1.8e-11 6.25
>>
so the above without the use of persistent would have required 12 times to enter the same value...this will wear you out in short order for a complicated problem.
You'll need some other way to set the auxiliary parameter(s) to your problem.
  2 Comments
Kiran Kumar Asokan
Kiran Kumar Asokan on 21 Jun 2021
I didnt intend to enter every iteration to enter a input and then try optimising the function. I used the same function which I had modeled for some other purpose and without being cautious tried optimising. I understand this is not the best way.
How can i check the input of every iteration for a surrogate function. With that i can cross check if the function is satisfying the if condition or not right.
And in the above fsolve problem you pointed out once you assigned value 6 to c it doesnt satisfy the condition in any of the further loop, i guess.
I am a beginner, please to bear me. I appreciate your efforts to help. Thanks :)
dpb
dpb on 21 Jun 2021
You need to just rewrite the function to have the needed input(s) coded into it or use one of the techniques shown in the documentation to pass auxiliary data to optimization routines.
One of the more convenient ways is to write the function with the auxiliary inputs as arguments, then use an anonymous function with the specific arguments wanted as the objective function to fsolve.
In the example above, persistent is the key -- when the function is called the very first time there is no variable "c" defined so isempty() returns True and you get the prompt -- since it is persistent, c now has and retains its value from call to call and so the second (and all subsequent) times, isempty() will return False and the function just goes on. NB: It would take explicitly adding code to reset c for this function or to clear cache now to ever give the initial result again -- restarting fsolve will still have the cached version of the function in memory.

Sign in to comment.

Products


Release

R2021a

Community Treasure Hunt

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

Start Hunting!