Call function multiple times but execute once

83 views (last 30 days)
Good Day all,
I am kind of stranded and I need all the helps I can possible get.
I have a main code which I am running, inside the main code I reference a function 3 times, the function is multple output, as such, each time I run the main function, it runs the reference function 3 times as well to extract all three output even when all the function are from the same source, which to me is kind of fustrating as it takes time to run the function one time, how much more running it three times. My challenge is how can I possible ajust the code so it can only runs the function one time.
  9 Comments
joshua Abam
joshua Abam on 3 Jan 2020
Hi Walter
sorry I have not been able to respond to your messenge ealier
I have a scenerio playing in my head which i want to achieve, as I have explained earlier
The Challenge I am having is I wish to run both nonconst and parcel function one after the other however, I don't need the digital function to be run twice, as demostrated below. I wish the second function should be able to benefit from the first run.
Thanks
Joshua
eg.
function [c, ceq] = nonconst(x)
u = 20;
v = 40;
[total_value, Delta_value, energy_value] = digital(x);
c = total_value-u*v;... ` `%first constrain
u*energy_value; %2nd constrain
ceq = [];
end
function lineweight = parcel(x)
[total_value, Delta_value, energy_value] = digital(x);
p_s = 7850; %Density of steel Kg/m^3
g = 9.812; %Acceleration due to gravitation
k = 0.72
lineweight(1) = p_s*g*(pi/4)*(x(1)+x(2))*((x(3))^2-0.254^2);
lineweight(2) = k*Delta_value
T2 = table(x(1), x(2), x(3), lineweight(1), lineweight(2))
end

Sign in to comment.

Accepted Answer

Walter Roberson
Walter Roberson on 7 Dec 2019
  1 Comment
joshua Abam
joshua Abam on 10 Dec 2019
Edited: Rik on 10 Dec 2019
Dear Walter
I have used the memoize function hoiwever initial it look correct however on further examination it seems not to be, as it seems to be repeating the process which I am trying to eliminate in the first instance. Now if you go thru the code, when you call 'nonconst' function it start by calling 'total_value' function which is gotten from digital function, after that it calls 'energy_value' function and still repeat the calculation of the digital function that has been carried under total_value. My question are, is there a way to extract result from the previous run of the digital function without running the function afresh.
function [x,fval] = gafunction(x)
%Define at what Generation is GA stall
ObjectiveFunction = @parcel; %fitness function
nvars = 3; % Number of variables
A = []; %Linear Inequality Constraint matrix
b = []; %Linear inequality constraint vector
Aeq = []; %Linear equality Constraint matrix
beq = []; %Linear equality constraint vector
lb = [168, 400, 0.296]; %Vector of Lower bounds
ub = [172, 500, 0.312]; %Vector of Upper bounds
% Initial design point could be determined in 2 ways: One is to leave it to
% GA automatically generate; or it can be specified by the users.
%x0 = []; %Initial Feasible Point
nonlcon = @nonconst; %Nonlinear constraints
Intcon = []; %Vector indicating variables that take integer values
%Excuate GA with output
[x,fval] = ga(ObjectiveFunction,nvars,A,b,Aeq,beq,lb,ub,nonlcon,Intcon)
end
function lineweight = parcel(x)
%
p_s = 7850; %Density of steel Kg/m^3
g = 9.812; %Acceleration due to gravitation
lineweight = p_s*g*(pi/4)*(x(1)+x(2))*((x(3))^2-0.254^2);
T2 = table(x(1), x(2), x(3),lineweight)
end
function [D_total,D_value,D_energy] = digital(x)
% Your calculations
% ;;;;;;;;
% ;;;;;;;;
Dtetha = 180;
Dmax = 360-Dtetha;
% initialize the scores out of the loop
k = 1;
D_total = 0;
for direction = 0:Dtetha:Dmax
energy_direction = direction;
% Your calculations
%;;;;;;;;;;;;;;
% ;;;;;;;;;;;;;;
% scores = [2345,345,87,964,673,9843,763,783];
scores = 5768*[0.2326*x(1)+x(3),0.2552*x(2)+x(1)]; %,0.0234,0.0014,0.0062,0.0868,0.2108,0.1836];
weight = [507*x(3)^3, 459*x(1)^2]; %, 563, 764, 783, 2345, 6532, 5627];
density = [1.2, 8.4]; %, 5.6, 7.4, 9.8, 9.3, 6.2, 8.2];
score_value = scores(k); % % it possible till the length of the direction is equal to scores
weight_value = weight(k);% Your calculations
density_value = density(k);
% ;;;;;
% ;;;;;
D_auto = energy_direction*score_value*weight_value*density_value;
D_value = score_value*density_value;
D_energy = energy_direction*weight_value*density_value;
% Table_2 = table(wave_direction,scores(k))
% for n = 1:length(wave_direction)
D_total = D_total+ D_auto;
k = k+1; % increment count variable for each iteration
end
Table_1 = table(energy_direction, score_value, weight_value, density_value,D_auto, D_value, D_energy, D_total)
end
function [c, ceq] = nonconst(x)
u = 20;
v = 40;
c = total_value(x)-u*v;... ` `%first nonlinear constrain
u*energy_value(x);... %2nd Nonlinear constrain
v*Delta_value(x);
ceq = [];
end
function D_total = total_value(x)
D_total = digital(x) % ca
% fmem = memoize(@digital)
end
function D_energy = energy_value(x)
fmem = memoize(@digital) %to memoize the digital output
[~, ~, D_energy] = fmem(x)
end
function D_value = Delta_value(x)
fmem = memoize(@digital) %to memoize the digital output
[~, D_value, ~] = fmem(x)
end

Sign in to comment.

More Answers (1)

Steven Lord
Steven Lord on 10 Dec 2019
I think it unlikely that ga is calling your function with exactly, down to the last bit, the same inputs multiple times. It's likely calling your function with inputs that are very close to one another, inputs that may even be displayed in the default format as the same, but that are very slightly different. Because of that memoization probably isn't going to help you.
Let me take a step back. Why do you want or need to try to reduce the number of calls ga makes to your function? Is it a performance consideration? Are the objective function evaluations expensive in terms of money (for example, are you planning to use ga to optimize parameters that require running a physical experiment for each set of parameters evaluated?) Is it a stylistic concern?
If you're concern is performance, what about the multiple calls to your objective function are the performance bottleneck? My strong suspicion is that it is your table call in the objective function as the other functions you call in your objective function require much less processing than creating a table. If you're doing this to show the parameter values after each iteration, depending on exactly why you need this information you may be able to instead specify an OutputFcn in some optimoptions that you pass into ga. See the section on ga options in the documentation for optimoptions for more information about the OutputFcn option.
  2 Comments
Rik
Rik on 10 Dec 2019
As Steven explained: memoization only works if the input is exactly the same. That is why I suggested adding that code to your function. It will error if it runs twice with the same input. If it does run multiple times, that means the input wasn't the same.
Also, please use the tools explained on this page to make your code more readable.

Sign in to comment.

Community Treasure Hunt

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

Start Hunting!