How Can I Customize Simulated Annealing Algorithm?
8 Ansichten (letzte 30 Tage)
Ältere Kommentare anzeigen
B. Burak
am 28 Mär. 2024
Bearbeitet: B. Burak
am 10 Jul. 2024
I have an optimization problem with discrete design variables, all of which are picked from a set {-45, 0, 45, 90}, to solve with built-in simulannealbnd function of Optimization Toolbox. In other words, optimal (x*) will be a combination of those values from the set. I read up on it from respective documentation and found out that it is by default for continuous variables. Again in the documentation there are ways to customize it so I decided to tweak with 'AnnealingFcn' option. This way I will randomly generate next iteration point as I wanted. I wrote my code as follows:
rng(7, 'twister')
ply_nums = 64;
E_11 = 127600 * 1e6;
E_22 = 13000 * 1e6;
G_12 = 6400 * 1e6;
v_12 = 0.3;
q = Q([E_11, E_22, G_12], v_12);
a = 0.508;
b = 0.254;
t = 0.127 * 1e-3;
z0 = 0;
N_xx = 1;
N_yy = 1;
N_xy = 0;
ply_qs = uniform_ply_qs(q, ply_nums);
ply_ts = uniform_ts(ply_nums, t);
ply_locations = PLY_ZS(ply_ts, z0);
lb = -90 * ones(1, ply_nums);
ub = 90 * ones(1, ply_nums);
values = [-45, 0, 45, 90];
x0 = values(randi(length(values), 1, ply_nums));
F = @(x) -lambda_critical(ply_qs, x, ply_locations, a, b, N_xx, N_yy, N_xy);
function newx = mynewx(optimValues, ~)
newx = values(randi(length(optimValues.x)), 1, length(optimValues.x));
end
options = optimoptions(@simulannealbnd, ...
'Display', ...
'iter', ...
'AnnealingFcn', @mynewx, ...
'MaxIterations', 2000);
[x, F, exitflag, output] = simulannealbnd(F, x0, lb, ub, options);
disp('x : ')
disp(x)
disp('F(x) : ')
disp(F)
However, this code throws the following error when I run it:
Incorrect number or types of inputs or outputs for function values.
Error in sa_run>mynewx (line 40)
newx = values(randi(length(optimValues.x)), 1, length(optimValues.x));
Error in globaloptim.simulannealbnd.sanewpoint (line 17)
newx(:) = options.AnnealingFcn(optimvalues,problem);
Error in globaloptim.simulannealbnd.saengine (line 29)
solverData = globaloptim.simulannealbnd.sanewpoint(solverData,problem,options);
Error in globaloptim.simulannealbnd.driver (line 28)
solverData = globaloptim.simulannealbnd.saengine(solverData,problem,options);
Error in simulannealbnd (line 197)
globaloptim.simulannealbnd.driver(FUN, x0, [], [], [], [], lb, ub, options, defaultopt);
Error in sa_run (line 50)
[x, F, exitflag, output] = simulannealbnd(F, x0, lb, ub, options);
How can I solve the error here? What do I miss here?
0 Kommentare
Akzeptierte Antwort
Abhimenyu
am 5 Apr. 2024
Hi Burhan,
From the information shared, I could infer that you are solving an optimization problem with discrete design variables using a custom annealing function. The error message indicates that there is an issue with the function values returned by the custom annealing function, "mynewx". Specifically, the dimensions or types of the output are not as expected. The problem lies in the line where "newx" is updated, as "values(randi(length(optimValues.x)), 1, length(optimValues.x))" results in an incorrect shape for "newx".
To fix this, a single value must be generated from the "values" array for each design variable. Please refer to the below-mentioned corrected MATLAB code for the "mynewx" function:
%Corrected mynewx function
function newx = mynewx(optimValues, ~)
newx = values(randi(length(values), 1, length(optimValues.x)));
end
In this corrected version, "randi(length(values), 1, length(optimValues.x))" generates a row vector of random integers where each integer is between 1 and the length of "values", and each integer corresponds to an index in "values". This vector is then used to index into "values", producing a new array "newx" where each element is randomly chosen from "values".
I hope this helps!
Weitere Antworten (0)
Siehe auch
Kategorien
Mehr zu Simulated Annealing finden Sie in Help Center und File Exchange
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!