Skip to content

Commit

Permalink
Add files via upload
Browse files Browse the repository at this point in the history
  • Loading branch information
americocunhajr authored Aug 16, 2024
1 parent 57a1749 commit 7ef5b0c
Show file tree
Hide file tree
Showing 5 changed files with 80 additions and 11 deletions.
34 changes: 27 additions & 7 deletions CEopt-1.0/CEopt.m
Original file line number Diff line number Diff line change
@@ -1,11 +1,11 @@
% -----------------------------------------------------------------
% CEopt.m
% -----------------------------------------------------------------
% programmer: Americo Cunha Jr
% Programmer: Americo Cunha Jr
% [email protected]
%
% Originally programmed in: Jun 18, 2021
% Last updated in: Apr 10, 2024
% Last updated in: Aug 15, 2024
% -----------------------------------------------------------------
% This routine employs the Cross-entropy (CE) method to solve the
% following optimization problem:
Expand Down Expand Up @@ -106,12 +106,32 @@
% CEstr - The updated Cross-Entropy object struct containing the final
% state of the algorithm and possibly additional diagnostic
% information.
% -----------------------------------------------------------------
% References:
%
% Reference:
% Reuven Y. Rubinstein, Dirk P. Kroese
% The Cross-Entropy Method: A Unified Approach to Combinatorial
% Optimization, Monte-Carlo Simulation, and Machine Learning
% Springer-Verlag, 2004.
% [1] Reuven Y. Rubinstein, Dirk P. Kroese,
% The Cross-Entropy Method: A Unified Approach to Combinatorial
% Optimization, Monte-Carlo Simulation, and Machine Learning,
% Springer-Verlag, 2004.
%
% [2] A. Cunha Jr, M. V. Issa, J. C. Basilio, J. G. Telles Ribeiro,
% CEopt: A MATLAB Package for Non-convex Optimization with the
% Cross-Entropy Method, ArXiv, 2024
% -----------------------------------------------------------------
% Copyright (C) 2024 Americo Cunha Jr et al.
%
% This program is free software: you can redistribute it and/or modify
% it under the terms of the GNU General Public License as published by
% the Free Software Foundation, either version 3 of the License, or
% (at your option) any later version.
%
% This program is distributed in the hope that it will be useful,
% but WITHOUT ANY WARRANTY; without even the implied warranty of
% MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
% GNU General Public License for more details.
%
% You should have received a copy of the GNU General Public License
% along with this program. If not, see <https://www.gnu.org/licenses/>.
% -----------------------------------------------------------------
function [Xopt,Fopt,ExitFlag,CEstr] = ...
CEopt(fun,xmean0,sigma0,lb,ub,nonlcon,CEstr)
Expand Down
2 changes: 1 addition & 1 deletion CEopt-1.0/MainCEoptExample1Ext.m
Original file line number Diff line number Diff line change
Expand Up @@ -33,7 +33,7 @@

% CE optimizer
tic
[Xopt,Fopt,ExitFlag,CEobj] = CEopt(F,mu0,sigma0,lb,ub)
[Xopt,Fopt,ExitFlag,CEstr] = CEopt(F,mu0,sigma0,lb,ub)
toc

% domain for plotting
Expand Down
2 changes: 1 addition & 1 deletion CEopt-1.0/MainCEoptExample2.m
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,7 @@

% CE optimizer
tic
[X_opt,F_opt,ExitFlag,CEstr] = CEopt(F,mu0,sigma0,lb,ub,[],CEstr)
[Xopt,Fopt,ExitFlag,CEstr] = CEopt(F,mu0,sigma0,lb,ub,[],CEstr)
toc

% objective function
Expand Down
6 changes: 4 additions & 2 deletions CEopt-1.0/MainCEoptExample5.m
Original file line number Diff line number Diff line change
Expand Up @@ -30,7 +30,7 @@
thetaA = p(3,:);
a0 = 1;
b0 = x(1);
c0 = b0;
c0 = x(1);
d0 = x(2);
gamma = x(3);
thetaD = x(4);
Expand All @@ -49,5 +49,7 @@
xEc1 = (xEc0 - mean(xEc0))/(max(xEc0) - min(xEc0));
yEc1 = (yEc0 - mean(yEc0))/(max(xEc0) - min(xEc0));

F = sum(abs(xE1 - xEc1) + abs(yE1 - yEc1));
F1 = sum((xE1 - real(xEc1)).^2 + (yE1 - real(yEc1)).^2);
F2 = sum(abs(imag(xEc1).^2) + abs(imag(yEc1).^2) + abs(imag(thetaB).^2));
F = F1 + F2;
end
47 changes: 47 additions & 0 deletions CEopt-1.0/MainCEoptExample7.m
Original file line number Diff line number Diff line change
@@ -0,0 +1,47 @@
clc; clear; close all;

disp(' ------------------- ')
disp(' MainCEoptExample6Ext.m ')
disp(' ------------------- ')

% bound for design variables
lb = [-6 -4];
ub = [ 2 4];

% objective function and constraints
F = @PatternSearchFunc;
nonlcon = @ConicConstraints;

% cross-entropy optimizer struct
CEobj.isVectorized = 1; % vectorized function
CEobj.TolCon = 1.0e-6; % relative tolerance

tic
[Xopt,Fopt,ExitFlag,CEobj] = CEopt(F,[],[],lb,ub,nonlcon,CEobj)
toc

% objective function
function F = PatternSearchFunc(x)
x1 = x(:,1);
x2 = x(:,2);
F = zeros(size(x1,1),1);
for i = 1:size(x,1)
if x1(i) < -5
F(i) = (x1(i)+5).^2 + abs(x2(i));
elseif x1(i) < -3
F(i) = -2*sin(x1(i)) + abs(x2(i));
elseif x1(i) < 0
F(i) = 0.5*x1(i) + 2 + abs(x2(i));
elseif x1 >= 0
F(i) = .3*sqrt(x1(i)) + 5/2 + abs(x2(i));
end
end
end

% equality and inequality constraints
function [G,H] = ConicConstraints(x)
x1 = x(:,1);
x2 = x(:,2);
G = 2*x1.^2 + x2.^2 - 3;
H = (x1+1).^2 - (x2/2).^4;
end

0 comments on commit 7ef5b0c

Please sign in to comment.