# Setting up an optimization in matlab

4 views (last 30 days)
Lewis Waswa on 20 Apr 2024
Edited: Rupesh on 23 Apr 2024
Hi everyone.
I have an optimization problem that seeks to aid the selection of suitable conductors in a network planning exercise. I have set up two functions - one of the functions sets up the objective function which is to calculate the total cost based on the type of conductor and the cost associated with the type of conductor. The total cost is thus calculated based on the code below.
function TotCost = minTotCost(RepCondpath,fdrpath1,sheetname)
%fdrpath1: describes the path to the an excel file with the conductors on
%the network while RepCondpath described the path to the costs associated
%with each type of conductor.
[TR,TAmp, C_index]=rep_repository_cr_opt(RepCondpath);
[R,Amp]=getInitialCon_par(fdrpath1,sheetname);
% examine the Resistance of the conductor
% check the TR to find out the value of the
C_weights=C_index;
cost =zeros(length(R),1);
for i =1:length (R)
disp(i);
j= TR == R(i);
cost(i) = C_weights(j);
end
TotCost= sum(cost);
The second thing is to describe the constraints function. In this case, the two constraints are voltage levels (NPI) and conductor loading (CPI). These two are calculated based on the a powerflow function- which calculate the network's NPI and CPI using the following constraint function.
function[NPI,CPI]=ConstraintFnc(fdrpath1,sheetname)
fname1 = fdrpath1;sheet = sheetname;
fdrcon=get_fdrcon(fdrpath1,sheetname);
[VA,VB,VC,~,IA,IB,IC]= start_f7(1,1,1,1,sheetname,fdrpath1); % This is the powerflow function
ResRange1 = "G6:G12"; AmpRange="AG6:AG12";
R=xlsread(fname1,sheet,ResRange1);
Amp=xlsread(fname1,sheet,AmpRange);
[CPI,NPI]= BNPerf_opt(VA, VB, VC, IA, IB, IC); % This is the function that calculates the CPI and NPI
The objective is to find conductors in TR whose combination has the least total cost. How do I bring these two functions together to run on Matlab.
##### 1 CommentShow -1 older commentsHide -1 older comments
Torsten on 20 Apr 2024
Edited: Torsten on 20 Apr 2024
See how your functions can be used together with MATLAB's "fmincon" (or if you have discrete solution variables - "ga").

Sign in to comment.

### Answers (1)

Rupesh on 23 Apr 2024
Edited: Rupesh on 23 Apr 2024
Hi Lewis,
I understand that your problem involves optimizing conductor selection for network planning, focusing on minimizing costs while considering voltage and loading constraints. A more pinpointed approach to integrate your MATLAB functions with optimization algorithms is to use “fmincon” functionality of MATLAB. Using “fmincon” in this scenario provides is beneficial because it excels at handling complex optimization problems with continuous decision variables, constraints, and a nonlinear objective function. You can go though below declarations and pseudo code sample to get better idea about the approach.
Objective Function (minTotCost)
This function calculates the total cost of conductor selection. Ensure it accepts a vector of decision variables representing the selected conductors and returns the total cost.
Constraint Function Adaptation
Modify your constraint function to output in a format suitable for MATLAB's optimization tools. It should provide inequality constraints (c) for voltage levels and loading, and possibly equality constraints (ceq), though it seems your problem mainly involves inequalities.
For Continuous Variables with fmincon
If your scenario allows for a continuous interpretation, or you wish to experiment with fmincon:
% Initial guess
x0 = ...;
% Nonlinear constraints
nonlcon = @(x) ConstraintFnc_Adapted(x);
% Optimization options
options = optimoptions('fmincon', 'Display', 'iter');
% Running the optimization
[x, fval] = fmincon(@(x) minTotCost(x, ...), x0, [], [], [], [], lb, ub, nonlcon, options);
Using ga for Discrete Optimization
Given the discrete nature of conductor types, ga (Genetic Algorithm) is appropriate. % Number of conductors to select
nVars = ...;
% Define lower and upper bounds based on your conductors
lb = ...;
ub = ...;
% All variables are integers (discrete)
IntCon = 1:nVars;
% Optimization options
options = optimoptions('ga', 'Display', 'iter');
% Running the optimization
[x, fval] = ga(@(x) minTotCost(x, ...), nVars, [], [], [], [], lb, ub, @(x) ConstraintFnc_Adapted(x), IntCon, options);
You can also refer to below documents regarding more information about “fmincon” and “ga” usages with practical examples.
Hope it helps!
##### 0 CommentsShow -2 older commentsHide -2 older comments

Sign in to comment.

### Categories

Find more on Get Started with Optimization Toolbox in Help Center and File Exchange

R2022b

### Community Treasure Hunt

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

Start Hunting!