Skip to content

Commit

Permalink
improve error and warning comprehensability for infeasibility and bounds
Browse files Browse the repository at this point in the history
  • Loading branch information
rebeccamccabe committed Dec 2, 2024
1 parent 93d33c7 commit b7de1c8
Show file tree
Hide file tree
Showing 3 changed files with 37 additions and 4 deletions.
14 changes: 11 additions & 3 deletions mdocean/optimization/gradient_optim.m
Original file line number Diff line number Diff line change
Expand Up @@ -118,9 +118,17 @@
probs{i} = problem;

tol = eps(2);
if any(abs(X_opt_raw(b.mins_flexible) - b.X_mins(b.mins_flexible)) < tol) ...
|| any(abs(X_opt_raw(b.maxs_flexible) - b.X_maxs(b.maxs_flexible)) < tol)
warning('Optimization is up against a flexible variable bound, consider changing bounds')
min_active = abs(X_opt_raw - b.X_mins) < tol;
max_active = abs(X_opt_raw - b.X_maxs) < tol;
flexible_min_active = b.mins_flexible & min_active;
flexible_max_active = b.maxs_flexible & max_active;
if any(flexible_min_active) || any(flexible_max_active)
var_opt = b.var_names(1:end-1);
max_string = strcat(var_opt(flexible_max_active)," ");
min_string = strcat(var_opt(flexible_min_active)," ");
msg = ['Optimization is up against a flexible variable bound, consider changing bounds. ',...
'At max: ' horzcat(max_string{:}) ', at min: ' horzcat(min_string{:})];
warning(msg)
end

X_opt = [X_opt_raw; evaluate(X(end),struct())]; % add material back onto design vector
Expand Down
20 changes: 19 additions & 1 deletion mdocean/optimization/multiobjective/pareto_search.m
Original file line number Diff line number Diff line change
Expand Up @@ -84,12 +84,30 @@
'ParetoSetChangeTolerance',1.6e-8,'MaxIterations',100);
if ~isempty(X0)
probMO.options.InitialPoints = X0_struct;
else
probMO.x0 = [];
warning('All starting points are infeasible')
end
probMO.solver = 'paretosearch';
probMO.nvars = num_DVs;
%% Execute pareto search
disp('Finished finding pareto seed points. Now starting paretosearch.')
[x,fval,flag,output,residuals] = paretosearch(probMO);
try
[x,fval,flag,output,residuals] = paretosearch(probMO);
catch ME
if (strcmp(ME.identifier,'MATLAB:emptyObjectDotAssignment'))
msg = ['Pareto search could not find any feasible starting points, ' ...
'so plot errored. Retrying without plot.'];
warning(msg)
probMO.options.PlotFcn = [];
[x,fval,flag,output,residuals] = paretosearch(probMO);
if flag==-2
error(output.message)
end
else
rethrow(ME)
end
end

%% Process and save results
utopia = min(fval);
Expand Down
7 changes: 7 additions & 0 deletions mdocean/optimization/sensitivities/param_sweep.m
Original file line number Diff line number Diff line change
Expand Up @@ -103,6 +103,13 @@
slope_LCOE = get_slope(LCOE, ratios, LCOE_nom);
slope_Pvar = get_slope(P_var, ratios, Pvar_nom);

if all(~isfinite(slope_LCOE),'all') || all(~isfinite(slope_Pvar),'all')
msg = ['All slopes are NaN, meaning all optimizations failed. ' ...
'This might be because no feasible solution can be found. ' ...
'slope_LCOE = ', num2str(slope_LCOE), ' and slope_Pvar = ', num2str(slope_Pvar)];
error(msg)
end

[~,LCOE_sort_idx] = sort(abs(slope_LCOE),'MissingPlacement','first');
[~,Pvar_sort_idx] = sort(abs(slope_Pvar),'MissingPlacement','first');

Expand Down

0 comments on commit b7de1c8

Please sign in to comment.