# How to restart a script for optimization after shutting down

4 次查看（过去 30 天）
Jean Boyer 2023-6-15

I would like to run a genetic algorithm to adjust certain parameters in a simulation that takes a very long time to converge.
Given a population of 50 6-uplets and 100 iterations, I estimate the time needed to converge at just over a week.
The problem is that I can't leave the computer running over the weekend. So my question is, how can I resume the calculation where I left off before switching off the computer?

### 采纳的回答

Govind KM 2023-6-15

Hi Jean,
It looks like you basicallly need a way to save the state of the GA before switching off the computer. One way to do this would be to run the algorithm for a specific number of generations (say N) each time. Once the execution of the first N generations is complete, you can get the final population as an output from the ga function, which can then be used for the next set of generations as the initial population. You can run the algorithm in parts in this way, only running it when you are sure that you have the required time to run the specified number of generations. You can reduce N as per the availability of your computer. I assume all other parameters such as fitness function would remain the same between the execution of these generations.
% Create the GA options structure
options = optimoptions('ga');
% Set the population size and the initial population matrix
% Let p be the final population after the execution of the previous set of
% generations
populationSize = size(p, 1);
options.PopulationSize = populationSize;
options.PopulationType = 'custom';
options.InitialPopulationMatrix = p;
You can refer to the following documentation site for detailed information about passing the initial population to the function and getting final population as output :
https://in.mathworks.com/help/gads/ga.html (Input arguments -> Options -> InitialPopulationMatrix, Outputs -> Population)

### 类别

Help CenterFile Exchange 中查找有关 Problem-Based Optimization Setup 的更多信息

### Community Treasure Hunt

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

Start Hunting!

Translated by