Help for ga using parameters as inputs

11 views (last 30 days)
Hello;
I have a function given as follows. I plan to use ga for optimization that will change the sequence(decision variables), the rest will change fom case to case but of course will not change during optimization. I know we set the number of variables for optimization using ga, but how can I transfer (setups, processingtimes...weights) values from the script that runs the ga?
Thanks.
++
function weighted_makespan = calculate_weighted_makespan(sequence, setups, processing_times, due_times, weights)
++

Accepted Answer

Namnendra
Namnendra on 21 Feb 2024
Hi Fatih,
In MATLAB, the Genetic Algorithm (GA) function ga allows you to optimize a given objective function by varying a set of decision variables. If you have additional parameters that you need to pass to your objective function (like 'setups', 'processing_times', 'due_times', and 'weights' in your case), you can do so using anonymous functions or nested functions that capture these parameters from the enclosing workspace.
Here's an example of how you can set up your optimization using an anonymous function to pass the additional parameters to your objective function:
% Define your additional parameters
setups = [...]; % Your setups data
processing_times = [...]; % Your processing times data
due_times = [...]; % Your due times data
weights = [...]; % Your weights data
% Define the number of variables (the length of 'sequence')
numVariables = length(processing_times); % or however you determine the number of jobs
% Define an anonymous function that captures the additional parameters
% and passes them along with the decision variables to your objective function
objFun = @(sequence) calculate_weighted_makespan(sequence, setups, processing_times, due_times, weights);
% Set GA options if necessary (optional)
options = optimoptions('ga', 'PopulationSize', 100, 'MaxGenerations', 100, ...);
% Call the GA solver
[best_sequence, best_weighted_makespan] = ga(objFun, numVariables, [], [], [], [], [], [], [], options);
% Your objective function remains the same as you've defined it
function weighted_makespan = calculate_weighted_makespan(sequence, setups, processing_times, due_times, weights)
% Implementation of your function...
end
In this example, 'objFun' is an anonymous function that wraps your 'calculate_weighted_makespan' function and includes the additional parameters that won't change during the optimization. When you call the 'ga' function, you pass 'objFun' as the objective function to be optimized. The GA will then optimize the sequence variable, while the other parameters ('setups', 'processing_times', 'due_times', and 'weights') are passed through unchanged.
Remember to replace [...] with your actual data for setups, processing times, due times, and weights. The 'numVariables' should be set to the number of decision variables in your problem (for example, the length of the sequence array that needs to be optimized). The 'options' line is optional and can be customized based on your specific requirements for the GA algorithm.
I hope the above solution resolves the issue.
Thank you.

More Answers (0)

Community Treasure Hunt

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

Start Hunting!