├── README.md ├── con_fun_cvar.m ├── con_fun_dc.m ├── con_fun_eps.m ├── example_run.m ├── gensample.m ├── lincave.m ├── main_function.m ├── obj_fun.m ├── opt_cvar.m ├── opt_dc.m ├── opt_eps.m ├── quantile.m └── result.jpg /README.md: -------------------------------------------------------------------------------- 1 | # Sequential Convex Approximations to Joint Chance Constrained Programs: A Monte Carlo Approach 2 | 3 | ## Introduction 4 | This is a Matlab implementation of the sequential convex approximation algorithms for joint chance constrained problem. It includes a comparison between both conditional value-at-risk (CVaR) and sequential convex approximation for value-at-risk (Iterative dc). 5 | 6 | ## Using the code 7 | Use Matlab to run `example_run.m` directly. You may expect to see the result figure below: 8 | 9 | 10 | 11 | ## Files explanation: 12 | - `example_run.m`: runing file, first open 13 | - `main_function.m`: including generating samples, apply cvar approximation, epsilon approximation and dc approximation, return results for a particular setting 14 | - `gensample.m`: generate normal distributions for all random variables 15 | - `obj_fun.m`: objective function 16 | - `quantile.m`: quantile for constraints 17 | - `opt_cvar.m, opt_dc.m, opt_eps.m`: optimization for cvar, one step dc approximation, epsilon approximation 18 | - `con_fun_cvar.m, con_fun_dc.m, con_fun_eps.m`: constraints for cvar, one step dc approximation, epsilon approximation 19 | - `lincave.m`: linear approximation for concave function 20 | 21 | ## Citation 22 | @article{hong2011sequential, 23 | title={Sequential convex approximations to joint chance constrained programs: A Monte Carlo approach}, 24 | author={Hong, L Jeff and Yang, Yi and Zhang, Liwei}, 25 | journal={Operations Research}, 26 | volume={59}, 27 | number={3}, 28 | pages={617--630}, 29 | year={2011}, 30 | publisher={INFORMS} 31 | } 32 | -------------------------------------------------------------------------------- /con_fun_cvar.m: -------------------------------------------------------------------------------- 1 | % Conditional value at risk constraints 2 | function [c,ceq,GC,GCeq] = con_fun_cvar(x) 3 | 4 | global sample alpha sample_size condim dim; 5 | 6 | L=zeros(condim,sample_size); % allocate memory 7 | Grad=zeros(dim,sample_size,condim); % allocate memory 8 | for i=1:condim 9 | L(i,:)=((sample(:,:,i).^2)*(x.^2))'-100; % calculate value for each constraint 10 | Grad(:,:,i)=2*(sample(:,:,i).^2)'.*(x*ones(1,sample_size)); % calculate gradient 11 | end 12 | 13 | [Z I]=max(L); % find the maximum constraint that dominate 14 | G=zeros(dim,sample_size); % allocate memory 15 | for i=1:sample_size 16 | G(:,i)=Grad(:,i,I(i)); % find the dominated constraint gradient 17 | end 18 | 19 | Z_sort=sort(Z); % sort for taking quantile 20 | q=Z_sort(ceil(sample_size*(1-alpha))); % take the alpha quantile of constraint 21 | 22 | c=mean(Z.*(Z>=q))/alpha; % nonlinear constraint 23 | GC = G*(Z>=q)'/sample_size/alpha; % nonlinear constraint gradient 24 | ceq = []; % nonlinear equality constraint 25 | GCeq = []; % nonlinear equality con gradient -------------------------------------------------------------------------------- /con_fun_dc.m: -------------------------------------------------------------------------------- 1 | % convex approximation of dc constraints 2 | function [c,ceq,GC,GCeq] = con_fun_dc(x) 3 | 4 | global sample alpha sample_size condim dim epsilon x_star f_star g_star; 5 | 6 | L=zeros(condim,sample_size); % allocate memory 7 | Grad=zeros(dim,sample_size,condim); % allocate memory 8 | for i=1:condim 9 | L(i,:)=((sample(:,:,i).^2)*(x.^2))'-100; % calculate value for each constraint 10 | Grad(:,:,i)=2*(sample(:,:,i).^2)'.*(x*ones(1,sample_size)); % calculate gradient 11 | end 12 | 13 | [Z I]=max(L); % find the maximum constraint that dominate 14 | G=zeros(dim,sample_size); % allocate memory 15 | for i=1:sample_size 16 | G(:,i)=Grad(:,i,I(i)); % find the dominated constraint gradient 17 | end 18 | 19 | c=mean((Z+epsilon).*(Z>=-epsilon))/alpha-epsilon-f_star-(x-x_star)'*g_star; % nonlinear constraint 20 | GC = G*(Z>=-epsilon)'/sample_size/alpha-g_star; % nonlinear constraint gradient 21 | ceq = []; % nonlinear equality constraint 22 | GCeq = []; % nonlinear equality con gradient -------------------------------------------------------------------------------- /con_fun_eps.m: -------------------------------------------------------------------------------- 1 | % epsilont approximation constraints 2 | function [c,ceq,GC,GCeq] = con_fun_eps(x) 3 | 4 | global sample alpha sample_size condim dim epsilon; 5 | 6 | L=zeros(condim,sample_size); % allocate memory 7 | Grad=zeros(dim,sample_size,condim); % allocate memory 8 | for i=1:condim 9 | L(i,:)=((sample(:,:,i).^2)*(x.^2))'-100; % calculate value for each constraint 10 | Grad(:,:,i)=2*(sample(:,:,i).^2)'.*(x*ones(1,sample_size)); % calculate gradient 11 | end 12 | 13 | [Z I]=max(L); % find the maximum constraint that dominate 14 | G=zeros(dim,sample_size); % allocate memory 15 | for i=1:sample_size 16 | G(:,i)=Grad(:,i,I(i)); % find the dominated constraint gradient 17 | end 18 | 19 | c=mean((Z+epsilon).*(Z>=-epsilon))/alpha-epsilon; % nonlinear constraint 20 | GC = G*(Z>=-epsilon)'/sample_size/alpha; % nonlinear constraint gradient 21 | ceq = []; % nonlinear equality constraint 22 | GCeq = []; % nonlinear equality con gradient -------------------------------------------------------------------------------- /example_run.m: -------------------------------------------------------------------------------- 1 | % here is the runing file 2 | clear; clc; close all; 3 | 4 | Dim=20; % variable dimension 5 | ConDim=10; % number of constraints 6 | Alpha=0.05; % alpha 7 | SampleSize=1000; % sample size 8 | Eps=1; % epsilon for approximation 9 | StopEps=0.001; % stopping criteria for dc loop 10 | MaxIter=1000; % maximum iteration for an optimization (CVar or DC both) 11 | MaxDCIter=15; % maximum number for dc loop 12 | TolFun=1e-4; % tolerance function value for an optimization (CVar or DC both) 13 | TolCon=1e-4; % tolerance constraint value for an optimization (CVar or DC both) 14 | 15 | % main function in the example 16 | [x_cvar, fval_cvar, runtime_cvar, x_eps, fval_eps, runtime_eps, x_dc, fval_dc, runtime_dc] ... 17 | =main_function(Dim,ConDim,Alpha,SampleSize,Eps,StopEps,MaxIter,MaxDCIter,TolFun,TolCon); 18 | 19 | % plot 20 | figure(1); 21 | hold on; 22 | plot(fval_dc,'b'); 23 | plot(fval_cvar*ones(1,length(fval_dc)),'r'); 24 | ylabel('function value') 25 | legend('Iterative dc','CVaR'); 26 | hold off; -------------------------------------------------------------------------------- /gensample.m: -------------------------------------------------------------------------------- 1 | % generate sample for the problem 2 | function sample=gensample(dim,condim,sample_size) 3 | 4 | sample=zeros(sample_size,dim,condim); 5 | for i=1:condim 6 | sample(:,:,i)=normrnd(0,1,sample_size,dim); % 3d matrix 7 | % size: sample_size x dim x condim 8 | % each 2d matrix is for one constraint 9 | end -------------------------------------------------------------------------------- /lincave.m: -------------------------------------------------------------------------------- 1 | % return linear approximation for concave function f(x0) + f'(x0)*(x-x0) 2 | % f is the f(x0) 3 | % g is the f'(x0) 4 | function [f g] = lincave(x) 5 | 6 | global sample alpha sample_size condim dim; 7 | 8 | L=zeros(condim,sample_size); % allocate memory 9 | Grad=zeros(dim,sample_size,condim); % allocate memory 10 | for i=1:condim 11 | L(i,:)=((sample(:,:,i).^2)*(x.^2))'-100; % calculate value for each constraint 12 | Grad(:,:,i)=2*(sample(:,:,i).^2)'.*(x*ones(1,sample_size)); % calculate gradient 13 | end 14 | 15 | [Z I]=max(L); % find the maximum constraint that dominate 16 | G=zeros(dim,sample_size); % allocate memory 17 | for i=1:sample_size 18 | G(:,i)=Grad(:,i,I(i)); % find the dominated constraint gradient 19 | end 20 | 21 | f = mean(Z.*(Z>=0))/alpha; 22 | g = G*(Z>=0)'/sample_size/alpha; -------------------------------------------------------------------------------- /main_function.m: -------------------------------------------------------------------------------- 1 | % joint chance constrained programming 2 | function [x_cvar fval_cvar runtime_cvar x_eps fval_eps runtime_eps x_dc fval_dc runtime_dc] ... 3 | =mainfunction(Dim,ConDim,Alpha,SampleSize,Eps,StopEps,MaxIter,MaxDCIter,TolFun,TolCon) 4 | 5 | global dim condim alpha sample sample_size epsilon; 6 | 7 | dim=Dim; % dim is the dimension of random vector 8 | 9 | condim=ConDim; % condim is the number of constraints 10 | 11 | alpha=Alpha; % alpha is the probabilistic constraint level 12 | 13 | sample_size=SampleSize; % number of random samples 14 | 15 | % sample is a 3d matrix, with size: sample_size x dim x condim 16 | sample=gensample(dim,condim,sample_size); % generate random vector 17 | 18 | epsilon=Eps; % epsilon for approximation 19 | 20 | stopepsilon=StopEps; % stopping criteria 21 | 22 | maxiter=MaxIter; % maximum iteration for each optimization 23 | 24 | maxdciter=MaxDCIter; % maximum iteration for dc loop 25 | 26 | tolfun=TolFun; % tolerance function value in optimization 27 | 28 | tolcon=TolCon; % tolerance constraint value in optimization 29 | 30 | % set starting point 31 | x0=zeros(dim,1); 32 | 33 | % run cvar at the first time 34 | [x_cvar fval_cvar runtime_cvar]=opt_cvar(x0,maxiter,tolfun,tolcon); 35 | q_cvar=quantile(alpha,x_cvar); 36 | 37 | % run epsilon var approximation at the first time 38 | [x_eps fval_eps runtime_eps]=opt_eps(x0,maxiter,tolfun,tolcon); 39 | q_eps=quantile(alpha,x_eps); 40 | 41 | % run dc iterative run, fix epsilon right now 42 | x_dc(:,1)=x_eps; 43 | fval_dc(1)=fval_eps; 44 | runtime_dc(1)=runtime_eps; 45 | for i=2:maxdciter 46 | x_star=x_dc(:,i-1); 47 | [x_dc(:,i) fval_dc(i) runtime_dc(i)] = opt_dc(x_dc(:,i-1),maxiter,tolfun,tolcon); 48 | if abs(fval_dc(i)-fval_dc(i-1)) < stopepsilon 49 | break; 50 | end 51 | end -------------------------------------------------------------------------------- /obj_fun.m: -------------------------------------------------------------------------------- 1 | % objective function 2 | function [fval, fgrad]=obj_fun(x) 3 | 4 | global dim; 5 | 6 | % sum over 7 | fval=-sum(x); 8 | 9 | if nargout > 1 10 | fgrad=-ones(dim,1); 11 | end -------------------------------------------------------------------------------- /opt_cvar.m: -------------------------------------------------------------------------------- 1 | % Cvar optimization 2 | function [x fval runtime] = opt_cvar(x0,maxiter,tolfun,tolcon) 3 | 4 | global dim; 5 | 6 | % linear constraints and optimization set 7 | lb=zeros(dim,1); % x>=lb 8 | options=optimset('GradObj','on', 'GradConstr','on', ... 9 | 'MaxFunEvals',maxiter, ... 10 | 'TolFun',tolfun, 'TolCon',tolcon); 11 | 12 | % optimize 13 | tic; 14 | [x,fval]=fmincon(@obj_fun,x0,[],[],[],[],lb,[],@con_fun_cvar,options); 15 | runtime=toc; -------------------------------------------------------------------------------- /opt_dc.m: -------------------------------------------------------------------------------- 1 | % one convex approximation for dc 2 | function [x fval runtime] = opt_dc(x0,maxiter,tolfun,tolcon) 3 | 4 | global dim x_star f_star g_star; 5 | 6 | % linear constraints and optimization set 7 | lb=zeros(dim,1); % x>=lb 8 | options=optimset('GradObj','on', 'GradConstr','on', ... 9 | 'MaxFunEvals',maxiter, ... 10 | 'TolFun',tolfun, 'TolCon',tolcon); 11 | 12 | x_star=x0; 13 | % Calculate linear approximation of concave part 14 | [f_star g_star] = lincave(x_star); 15 | 16 | % optimize 17 | tic; 18 | [x,fval]=fmincon(@obj_fun,x0,[],[],[],[],lb,[],@con_fun_dc,options); 19 | runtime=toc; -------------------------------------------------------------------------------- /opt_eps.m: -------------------------------------------------------------------------------- 1 | % epsilon approximation optimization 2 | function [x fval runtime] = opt_eps(x0,maxiter,tolfun,tolcon) 3 | 4 | global dim; 5 | 6 | % linear constraints and optimization set 7 | lb=zeros(dim,1); % x>=lb 8 | options=optimset('GradObj','on', 'GradConstr','on', ... 9 | 'MaxFunEvals',maxiter, ... 10 | 'TolFun',tolfun, 'TolCon',tolcon); 11 | 12 | % optimize 13 | tic; 14 | [x,fval]=fmincon(@obj_fun,x0,[],[],[],[],lb,[],@con_fun_eps,options); 15 | runtime=toc; -------------------------------------------------------------------------------- /quantile.m: -------------------------------------------------------------------------------- 1 | % quantile for constraints 2 | function q = quantile(gamma,x) 3 | 4 | global sample sample_size condim; 5 | 6 | L=zeros(condim,sample_size); 7 | for i=1:condim 8 | L(i,:)=((sample(:,:,i).^2)*(x.^2))'-100; 9 | end 10 | 11 | Z=max(L); 12 | 13 | Z_sort=sort(Z); % Sort for taking quantile 14 | q=Z_sort(ceil(sample_size*(1-gamma))); % Take the alpha quantile of constraint -------------------------------------------------------------------------------- /result.jpg: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/yangyi02/sequential_chance_constrained/da15e4ce917d58eb78239c92b4565b6ad5b12279/result.jpg --------------------------------------------------------------------------------