Got Questions? Get Answers.
Discover MakerZone

MATLAB and Simulink resources for Arduino, LEGO, and Raspberry Pi

Learn more

Discover what MATLAB® can do for your career.

Opportunities for recent engineering grads.

Apply Today

Thread Subject:
Matlab Optimisation with multiple combinations of inequality

Subject: Matlab Optimisation with multiple combinations of inequality

From: Andrew Alkiviades

Date: 29 Jun, 2012 13:15:35

Message: 1 of 29

Hi I am trying to code a linear optimisation using linprog to determine the optimum number of PV (x1) and WT (x2) units under a given objective function shown below

f = [((CRF.*Panel_capacity./PV_energy)+OM_PV)((CRF.*WT_capacity/WT_energy)+OM_WT)];
Apart from CRF which is a constant, I have 27 selections for "Panel capacity" and "PVenergy" and "OM_PV" and 3 selections of "WT_capacity", "WTenergy" and "OM_WT".

I am trying to run linprog to examine the optimum x1 and x2 values for all the 27 and 3 combinations (which in total are 27*3 = 81 possible combinations). Therefore I am hoping for an output in some form similar to say:

5*x1,2 and 10*x2,3 which would mean the optimum combination is 5 units of the second PV panel (from the 27 in total) and 10 units of the third WT .

My inequality constraint is the following

b = Demand_vector;
Where "Demand_vector" is a 8760x1 vector -same for all cases.

A = [PV WT];
Where PV is currently a 8760x1 and WT is an 8760x1 to have a total 8760x2 matrix of A (this should give me a 2x1 vector of x's which is what I'm looking for).

However, as I mentioned above, I have 27 different PV's and 3 WT's (of the same type i.e 8760x1 which I want to examine all simultaneously and really need help in how to format the code for :

objective function - f (at the moment it doesn't account for all combs)

inequality constraint A = (at the moment it doesn't account for all combs)

Any help in how to format this linprog optimisation would be extremely appreciated, please let me know if more info is required.

Thank you

Subject: Matlab Optimisation with multiple combinations of inequality

From: Matt J

Date: 29 Jun, 2012 13:50:21

Message: 2 of 29

Andrew Alkiviades <andrew.alkiviades@gmail.com> wrote in message <75936e26-3348-462c-88b5-120e0cf4baa2@googlegroups.com>...
> Hi I am trying to code a linear optimisation using linprog to determine the optimum number of PV (x1) and WT (x2) units under a given objective function shown below
>
> f = [((CRF.*Panel_capacity./PV_energy)+OM_PV)((CRF.*WT_capacity/WT_energy)+OM_WT)];
> Apart from CRF which is a constant, I have 27 selections for "Panel capacity" and "PVenergy" and "OM_PV" and 3 selections of "WT_capacity", "WTenergy" and "OM_WT".
>
> I am trying to run linprog to examine the optimum x1 and x2 values for all the 27 and 3 combinations (which in total are 27*3 = 81 possible combinations). Therefore I am hoping for an output in some form similar to say:
>
> 5*x1,2 and 10*x2,3 which would mean the optimum combination is 5 units of the second PV panel (from the 27 in total) and 10 units of the third WT .
>
> My inequality constraint is the following
>
> b = Demand_vector;
> Where "Demand_vector" is a 8760x1 vector -same for all cases.
>
> A = [PV WT];
> Where PV is currently a 8760x1 and WT is an 8760x1 to have a total 8760x2 matrix of A (this should give me a 2x1 vector of x's which is what I'm looking for).
>
> However, as I mentioned above, I have 27 different PV's and 3 WT's (of the same type i.e 8760x1 which I want to examine all simultaneously and really need help in how to format the code for :
>
> objective function - f (at the moment it doesn't account for all combs)
>
> inequality constraint A = (at the moment it doesn't account for all combs)
>
> Any help in how to format this linprog optimisation would be extremely appreciated, please let me know if more info is required.
================



Suppose you have cell arrays of linprog data, where each cell element holds one of the 81 combinations

A{1}...A{81} %8760x2 matrices
f{1}...f{81} %1x2 row vectors
b{1}...b{81} %8760x1 column vectors

You can combine them into one big linear program as follows

Atotal=blockdiag(A{:});
ftotal=horzcat(f{:});
btotal=vertcat(b{:});

and then do

 X=reshape(linprog(ftotal,Atotal,btotal),2,[]);

The result will be a matrix X with 81 columns, each one a solution to one of your combinations of data.

It is also possible to generate the same X by solving each of the 81 programs individually, as follows, which might be better because linprog will then know that your overall problem actually consists of simpler, smaller problems:

X=zeros(2,81);
for k=1:81
  X(:,k)=linprog(f{k},A{k},b{k});
end

Subject: Matlab Optimisation with multiple combinations of inequality

From: Matt J

Date: 29 Jun, 2012 14:06:06

Message: 3 of 29

"Matt J" wrote in message <jskbqt$m93$1@newscl01ah.mathworks.com>...
>
> Suppose you have cell arrays of linprog data, where each cell element holds one of the 81 combinations
>
> A{1}...A{81} %8760x2 matrices
> f{1}...f{81} %1x2 row vectors
> b{1}...b{81} %8760x1 column vectors
>
> You can combine them into one big linear program as follows
>
> Atotal=blockdiag(A{:});
===============

That should be BLKDIAG, and you will need to make the A{i} sparse first,

A=cellfun(@(c) sparse(c), A,'uni',0);
Atotal=blkdiag(A{:});

Subject: Matlab Optimisation with multiple combinations of inequality

From: Andrew Alkiviades

Date: 29 Jun, 2012 14:19:41

Message: 4 of 29

On Friday, 29 June 2012 15:06:06 UTC+1, Matt J wrote:
> "Matt J" wrote in message <jskbqt$m93$1@newscl01ah.mathworks.com>...
> >
> > Suppose you have cell arrays of linprog data, where each cell element holds one of the 81 combinations
> >
> > A{1}...A{81} %8760x2 matrices
> > f{1}...f{81} %1x2 row vectors
> > b{1}...b{81} %8760x1 column vectors
> >
> > You can combine them into one big linear program as follows
> >
> > Atotal=blockdiag(A{:});
> ===============
>
> That should be BLKDIAG, and you will need to make the A{i} sparse first,
>
> A=cellfun(@(c) sparse(c), A,'uni',0);
> Atotal=blkdiag(A{:});

Matt, Thank you for your reply.

I believe that I will follow your second solution and solving each of the 81 programs individually.
I have a couple of basic questions on that solution (apologies but I am still a beginner with Matlab)

1) I still need to create the A's, b's and f's. Is this done using your suggested way of

A{1}...A{81} %8760x2 matrices
f{1}...f{81} %1x2 row vectors
b{1}...b{81} %8760x1 column vectors

if so could you provide an example for how to code for A? Is there a way of coding for all 81 A's simultaneously since there are quite a few?

Thank you for your time, your help is much appreciated and needed!

Subject: Matlab Optimisation with multiple combinations of inequality

From: Matt J

Date: 29 Jun, 2012 14:29:10

Message: 5 of 29

Andrew Alkiviades <andrew.alkiviades@gmail.com> wrote in message <bae1a473-6c8c-4b8a-b080-8412bc891f1a@googlegroups.com>...
>
> 1) I still need to create the A's, b's and f's. Is this done using your suggested way of
>
> A{1}...A{81} %8760x2 matrices
> f{1}...f{81} %1x2 row vectors
> b{1}...b{81} %8760x1 column vectors
>
> if so could you provide an example for how to code for A? Is there a way of coding for all 81 A's simultaneously since there are quite a few?
>
> Thank you for your time, your help is much appreciated and needed!
===============

You can use a for-loop to loop over the 81 different combinations and store each A,f,b to a cell. I imagine that's something you already know how to do because you were doing the same thing here:

http://www.mathworks.com/matlabcentral/newsreader/view_thread/321243

Subject: Matlab Optimisation with multiple combinations of inequality

From: Andrew Alkiviades

Date: 29 Jun, 2012 14:41:27

Message: 6 of 29

On Friday, 29 June 2012 15:29:10 UTC+1, Matt J wrote:
> Andrew Alkiviades <andrew.alkiviades@gmail.com> wrote in message <bae1a473-6c8c-4b8a-b080-8412bc891f1a@googlegroups.com>...
> >
> > 1) I still need to create the A's, b's and f's. Is this done using your suggested way of
> >
> > A{1}...A{81} %8760x2 matrices
> > f{1}...f{81} %1x2 row vectors
> > b{1}...b{81} %8760x1 column vectors
> >
> > if so could you provide an example for how to code for A? Is there a way of coding for all 81 A's simultaneously since there are quite a few?
> >
> > Thank you for your time, your help is much appreciated and needed!
> ===============
>
> You can use a for-loop to loop over the 81 different combinations and store each A,f,b to a cell. I imagine that's something you already know how to do because you were doing the same thing here:
>
> http://www.mathworks.com/matlabcentral/newsreader/view_thread/321243


hi Matt,
Do I need to do this for "b" since it is elements are constant throughout all 81 combinations?
I think I need a separate for loop for A and f (and b if needed)
Is there a way of doing it in one for loop to save comp time and space?
thank you

Subject: Matlab Optimisation with multiple combinations of inequality

From: Matt J

Date: 29 Jun, 2012 15:12:07

Message: 7 of 29

Andrew Alkiviades <andrew.alkiviades@gmail.com> wrote in message <ca6b63ed-d1fe-4d16-a643-bbb75c66074c@googlegroups.com>...
>
>
> hi Matt,
> Do I need to do this for "b" since it is elements are constant throughout all 81 combinations?

No.

> I think I need a separate for loop for A and f (and b if needed)

Why?

> Is there a way of doing it in one for loop to save comp time and space?

Why can't you compute f and A in the same for-loop if there are 81 combinations of both?

Subject: Matlab Optimisation with multiple combinations of inequality

From: Andrew Alkiviades

Date: 29 Jun, 2012 15:23:49

Message: 8 of 29

On Friday, 29 June 2012 16:12:07 UTC+1, Matt J wrote:
> Andrew Alkiviades <andrew.alkiviades@gmail.com> wrote in message <ca6b63ed-d1fe-4d16-a643-bbb75c66074c@googlegroups.com>...
> >
> >
> > hi Matt,
> > Do I need to do this for "b" since it is elements are constant throughout all 81 combinations?
>
> No.
>
> > I think I need a separate for loop for A and f (and b if needed)
>
> Why?
>
> > Is there a way of doing it in one for loop to save comp time and space?
>
> Why can't you compute f and A in the same for-loop if there are 81 combinations of both?

Matt, thanks

I have tried the following

A = cell(27,3);
f = cell(27,3);

for ii = 1:27

    PV = PV_output(:,:,ii);
    f = CRF*Panel_capacity(ii)/PVenergy(ii) + OM_PV;
 
    for jj = 1:3

        WT = WT_output(:,:,jj);
            f = CRF*WT_capacity(jj)/WTenergy(jj)+OM_WT;

    end
end

X=zeros(2,81);
for n=1:81
  X(:,n)=linprog(f{n},A{n},b{n});
end
        

I am not sure about creating the "f" vector cell which is a 2x1 in the overall 81 combination picture.
As it stands there is an error on the line that calls linprog which is :
"Cell contents reference from a non-cell array object."

I have checked the "class" of A and f.
"A" is a "cell" and "f" is a "double" so how do I fix the cell creation of f?

Thank you

        

Subject: Matlab Optimisation with multiple combinations of inequality

From: Matt J

Date: 29 Jun, 2012 15:48:07

Message: 9 of 29

Andrew Alkiviades <andrew.alkiviades@gmail.com> wrote in message <755f1408-0980-4f6d-a774-f515df4ae460@googlegroups.com>...
>
>
>
> I am not sure about creating the "f" vector cell which is a 2x1 in the overall 81 combination picture.
> As it stands there is an error on the line that calls linprog which is :
> "Cell contents reference from a non-cell array object."
===================
 

Can you do me a favor? Can you run the code below exactly as written and tell me if it does what you want? The result should be a cell array with X{ii,jj} the solution for combination ii,jj


X=cell(27,3);

for ii = 1:27

    PV = PV_output(:,:,ii);
    fpv= CRF*Panel_capacity(ii)/PVenergy(ii) + OM_PV;
 
    for jj = 1:3

        WT = WT_output(:,:,jj);
            fwt = CRF*WT_capacity(jj)/WTenergy(jj)+OM_WT;

      X{ii,jj}=linprog([fpv,fwt], [PV,WT],Demand_vector);

    end
end

Subject: Matlab Optimisation with multiple combinations of inequality

From: Andrew Alkiviades

Date: 29 Jun, 2012 15:57:27

Message: 10 of 29

On Friday, 29 June 2012 16:48:07 UTC+1, Matt J wrote:
> Andrew Alkiviades <andrew.alkiviades@gmail.com> wrote in message <755f1408-0980-4f6d-a774-f515df4ae460@googlegroups.com>...
> >
> >
> >
> > I am not sure about creating the "f" vector cell which is a 2x1 in the overall 81 combination picture.
> > As it stands there is an error on the line that calls linprog which is :
> > "Cell contents reference from a non-cell array object."
> ===================
>
>
> Can you do me a favor? Can you run the code below exactly as written and tell me if it does what you want? The result should be a cell array with X{ii,jj} the solution for combination ii,jj
>
>
> X=cell(27,3);
>
> for ii = 1:27
>
> PV = PV_output(:,:,ii);
> fpv= CRF*Panel_capacity(ii)/PVenergy(ii) + OM_PV;
>
> for jj = 1:3
>
> WT = WT_output(:,:,jj);
> fwt = CRF*WT_capacity(jj)/WTenergy(jj)+OM_WT;
>
> X{ii,jj}=linprog([fpv,fwt], [PV,WT],Demand_vector);
>
> end
> end

Hi Matt
I believe it does since I get a size(X) = 27,3 and a class(X) = cell. Therefore I am assuming that these are the 27x3 combinations of X in a cell format (therefore x1 and x2 in each cell?


However I get the following error

Exiting: One or more of the residuals, duality gap, or total relative error
 has stalled:
         the dual appears to be infeasible (and the primal unbounded).
         (The primal residual < TolFun=1.00e-08.)

Has this got something to do with the algorithm used in the optimisation? Currently, it is at the default as you know, how do we go about selecting the appropriate algorithm?

Thank you for all your help

Subject: Matlab Optimisation with multiple combinations of inequality

From: Andrew Alkiviades

Date: 29 Jun, 2012 16:01:59

Message: 11 of 29

On Friday, 29 June 2012 16:57:27 UTC+1, Andrew Alkiviades wrote:
> On Friday, 29 June 2012 16:48:07 UTC+1, Matt J wrote:
> > Andrew Alkiviades <andrew.alkiviades@gmail.com> wrote in message <755f1408-0980-4f6d-a774-f515df4ae460@googlegroups.com>...
> > >
> > >
> > >
> > > I am not sure about creating the "f" vector cell which is a 2x1 in the overall 81 combination picture.
> > > As it stands there is an error on the line that calls linprog which is :
> > > "Cell contents reference from a non-cell array object."
> > ===================
> >
> >
> > Can you do me a favor? Can you run the code below exactly as written and tell me if it does what you want? The result should be a cell array with X{ii,jj} the solution for combination ii,jj
> >
> >
> > X=cell(27,3);
> >
> > for ii = 1:27
> >
> > PV = PV_output(:,:,ii);
> > fpv= CRF*Panel_capacity(ii)/PVenergy(ii) + OM_PV;
> >
> > for jj = 1:3
> >
> > WT = WT_output(:,:,jj);
> > fwt = CRF*WT_capacity(jj)/WTenergy(jj)+OM_WT;
> >
> > X{ii,jj}=linprog([fpv,fwt], [PV,WT],Demand_vector);
> >
> > end
> > end
>
> Hi Matt
> I believe it does since I get a size(X) = 27,3 and a class(X) = cell. Therefore I am assuming that these are the 27x3 combinations of X in a cell format (therefore x1 and x2 in each cell?
>
>
> However I get the following error
>
> Exiting: One or more of the residuals, duality gap, or total relative error
> has stalled:
> the dual appears to be infeasible (and the primal unbounded).
> (The primal residual < TolFun=1.00e-08.)
>
> Has this got something to do with the algorithm used in the optimisation? Currently, it is at the default as you know, how do we go about selecting the appropriate algorithm?
>
> Thank you for all your help

Hi Matt
I believe it does since I get a size(X) = 27,3 and a class(X) = cell. Therefore I am assuming that these are the 27x3 combinations of X in a cell format (therefore x1 and x2 in each cell?


However I get the following error

Exiting: One or more of the residuals, duality gap, or total relative error
 has stalled:
         the dual appears to be infeasible (and the primal unbounded).
         (The primal residual < TolFun=1.00e-08.)

Has this got something to do with the algorithm used in the optimisation? Currently, it is at the default as you know, how do we go about selecting the appropriate algorithm?

Thank you for all your help

Subject: Matlab Optimisation with multiple combinations of inequality

From: Matt J

Date: 29 Jun, 2012 16:16:17

Message: 12 of 29

Andrew Alkiviades <andrew.alkiviades@gmail.com> wrote in message <5fe2e147-4660-4f35-a1b1-dab4aee860ae@googlegroups.com>...
> On Friday, 29 June 2012 16:57:27 UTC+1, Andrew Alkiviades wrote:
> > On Friday, 29 June 2012 16:48:07 UTC+1, Matt J wrote:
> > > Andrew Alkiviades <andrew.alkiviades@gmail.com> wrote in message <755f1408-0980-4f6d-a774-f515df4ae460@googlegroups.com>...
> > > >
> > > >
> > > >
> > > > I am not sure about creating the "f" vector cell which is a 2x1 in the overall 81 combination picture.
> > > > As it stands there is an error on the line that calls linprog which is :
> > > > "Cell contents reference from a non-cell array object."
> > > ===================
> > >
> > >
> > > Can you do me a favor? Can you run the code below exactly as written and tell me if it does what you want? The result should be a cell array with X{ii,jj} the solution for combination ii,jj
> > >
> > >
> > > X=cell(27,3);
> > >
> > > for ii = 1:27
> > >
> > > PV = PV_output(:,:,ii);
> > > fpv= CRF*Panel_capacity(ii)/PVenergy(ii) + OM_PV;
> > >
> > > for jj = 1:3
> > >
> > > WT = WT_output(:,:,jj);
> > > fwt = CRF*WT_capacity(jj)/WTenergy(jj)+OM_WT;
> > >
> > > X{ii,jj}=linprog([fpv,fwt], [PV,WT],Demand_vector);
> > >
> > > end
> > > end
> >
> > Hi Matt
> > I believe it does since I get a size(X) = 27,3 and a class(X) = cell. Therefore I am assuming that these are the 27x3 combinations of X in a cell format (therefore x1 and x2 in each cell?
> >
> >
> > However I get the following error
> >
> > Exiting: One or more of the residuals, duality gap, or total relative error
> > has stalled:
> > the dual appears to be infeasible (and the primal unbounded).
> > (The primal residual < TolFun=1.00e-08.)
> >
> > Has this got something to do with the algorithm used in the optimisation? Currently, it is at the default as you know, how do we go about selecting the appropriate algorithm?
> >
> > Thank you for all your help
>
> Hi Matt
> I believe it does since I get a size(X) = 27,3 and a class(X) = cell. Therefore I am assuming that these are the 27x3 combinations of X in a cell format (therefore x1 and x2 in each cell?
==================

That was the intention.


> However I get the following error
>
> Exiting: One or more of the residuals, duality gap, or total relative error
> has stalled:
> the dual appears to be infeasible (and the primal unbounded).
> (The primal residual < TolFun=1.00e-08.)
>
> Has this got something to do with the algorithm used in the optimisation?
============

No, it has to do with your data. It means that there is no bounded solution to one of your linear programs, given the data you have provided. You'll need to examine your data to see why the problem has no solution.

Subject: Matlab Optimisation with multiple combinations of inequality

From: Andrew Alkiviades

Date: 29 Jun, 2012 16:30:52

Message: 13 of 29

On Friday, 29 June 2012 17:16:17 UTC+1, Matt J wrote:
> Andrew Alkiviades <andrew.alkiviades@gmail.com> wrote in message <5fe2e147-4660-4f35-a1b1-dab4aee860ae@googlegroups.com>...
> > On Friday, 29 June 2012 16:57:27 UTC+1, Andrew Alkiviades wrote:
> > > On Friday, 29 June 2012 16:48:07 UTC+1, Matt J wrote:
> > > > Andrew Alkiviades <andrew.alkiviades@gmail.com> wrote in message <755f1408-0980-4f6d-a774-f515df4ae460@googlegroups.com>...
> > > > >
> > > > >
> > > > >
> > > > > I am not sure about creating the "f" vector cell which is a 2x1 in the overall 81 combination picture.
> > > > > As it stands there is an error on the line that calls linprog which is :
> > > > > "Cell contents reference from a non-cell array object."
> > > > ===================
> > > >
> > > >
> > > > Can you do me a favor? Can you run the code below exactly as written and tell me if it does what you want? The result should be a cell array with X{ii,jj} the solution for combination ii,jj
> > > >
> > > >
> > > > X=cell(27,3);
> > > >
> > > > for ii = 1:27
> > > >
> > > > PV = PV_output(:,:,ii);
> > > > fpv= CRF*Panel_capacity(ii)/PVenergy(ii) + OM_PV;
> > > >
> > > > for jj = 1:3
> > > >
> > > > WT = WT_output(:,:,jj);
> > > > fwt = CRF*WT_capacity(jj)/WTenergy(jj)+OM_WT;
> > > >
> > > > X{ii,jj}=linprog([fpv,fwt], [PV,WT],Demand_vector);
> > > >
> > > > end
> > > > end
> > >
> > > Hi Matt
> > > I believe it does since I get a size(X) = 27,3 and a class(X) = cell. Therefore I am assuming that these are the 27x3 combinations of X in a cell format (therefore x1 and x2 in each cell?
> > >
> > >
> > > However I get the following error
> > >
> > > Exiting: One or more of the residuals, duality gap, or total relative error
> > > has stalled:
> > > the dual appears to be infeasible (and the primal unbounded).
> > > (The primal residual < TolFun=1.00e-08.)
> > >
> > > Has this got something to do with the algorithm used in the optimisation? Currently, it is at the default as you know, how do we go about selecting the appropriate algorithm?
> > >
> > > Thank you for all your help
> >
> > Hi Matt
> > I believe it does since I get a size(X) = 27,3 and a class(X) = cell. Therefore I am assuming that these are the 27x3 combinations of X in a cell format (therefore x1 and x2 in each cell?
> ==================
>
> That was the intention.
>
>
> > However I get the following error
> >
> > Exiting: One or more of the residuals, duality gap, or total relative error
> > has stalled:
> > the dual appears to be infeasible (and the primal unbounded).
> > (The primal residual < TolFun=1.00e-08.)
> >
> > Has this got something to do with the algorithm used in the optimisation?
> ============
>
> No, it has to do with your data. It means that there is no bounded solution to one of your linear programs, given the data you have provided. You'll need to examine your data to see why the problem has no solution.

Great Matt I will look into that - thank you very much for all your help
is there an accept answer button here?

Subject: Matlab Optimisation with multiple combinations of inequality

From: Andrew Alkiviades

Date: 29 Jun, 2012 16:46:49

Message: 14 of 29

Matt
With this code


Demand = xlsread('demandtest.xlsx','Sheet1');
Demand_vector = Demand(:);
% Capital Recovery Factor (CRF)

discount_f = 0.05;

lifetime = 20;

CRF = ((discount_f*(1+discount_f)^lifetime)/(((1+discount_f)^lifetime)-1));


% Technology capital costs and O&M costs

 OM_PV = 0.4; % Average PV O&M costs /kWh output (ARUP)
 OM_WT = 0.2; % Average WT O&M costs (ARUP)
             
 CC_PV = 3339; % Average PV Capital costs /kW (ARUP)
 CC_WT = 1548; % Average WT Capital costs /kW (ARUP)
 


% Land area constraint and Technology land area requirements

max_PV_area = 20000; % high upper bound land constraint - adjust this
max_WT_area = 150000; % high upper bound land constraint

PV_area = 3; % temp until optimization works - then speak to Malcolm to determine total area required per panel (panel surface are + some buffer)
WT_area = 1000; % temp until optimization works - then convert rotor diameter to required land area





% linprog

WT_capacity = [170 90 100];


X = cell(27,3);
lb = [0;0];
ub = [max_PV_area/PV_area;max_WT_area/WT_area];

for ii = 1:27

    PV = PV_output(:,:,ii);
    fpv= CRF*Panel_capacity(ii)*CC_PV/PVenergy(ii) + OM_PV;
  
    for jj = 1:3

        WT = WT_output(:,:,jj);
            fwt = CRF*WT_capacity(jj)*CC_WT/WTenergy(jj)+OM_WT;

              [X{ii,jj},fval,exitflag] = linprog([fpv,fwt], [PV,WT],Demand_vector,[],[],lb,ub)

    end
end


I have added a lb and ub that is constant for all combinations but the result i get looks like this

Optimization terminated.

X =

    [2x1 double] [2x1 double] [2x1 double]
    [2x1 double] [2x1 double] [2x1 double]
    [2x1 double] [2x1 double] [2x1 double]
    [2x1 double] [2x1 double] [2x1 double]
    [2x1 double] [2x1 double] [2x1 double]
    [2x1 double] [2x1 double] [2x1 double]
    [2x1 double] [2x1 double] [2x1 double]
    [2x1 double] [2x1 double] [2x1 double]
    [2x1 double] [2x1 double] [2x1 double]
    [2x1 double] [2x1 double] [2x1 double]
    [2x1 double] [2x1 double] [2x1 double]
    [2x1 double] [2x1 double] [2x1 double]
    [2x1 double] [2x1 double] [2x1 double]
    [2x1 double] [2x1 double] [2x1 double]
    [2x1 double] [2x1 double] [2x1 double]
    [2x1 double] [2x1 double] [2x1 double]
    [2x1 double] [2x1 double] [2x1 double]
    [2x1 double] [2x1 double] [2x1 double]
    [2x1 double] [2x1 double] [2x1 double]
    [2x1 double] [2x1 double] [2x1 double]
    [2x1 double] [2x1 double] [2x1 double]
    [2x1 double] [2x1 double] [2x1 double]
    [2x1 double] [2x1 double] [2x1 double]
    [2x1 double] [2x1 double] [2x1 double]
    [2x1 double] [2x1 double] [2x1 double]
    [2x1 double] [2x1 double] [2x1 double]
    [2x1 double] [2x1 double] [2x1 double]


fval =

   1.2706e-14


exitflag =

     1

I don't seem to be able to access any of the X's and get a numerical value for each combination

Subject: Matlab Optimisation with multiple combinations of inequality

From: Matt J

Date: 29 Jun, 2012 17:54:07

Message: 15 of 29

Andrew Alkiviades <andrew.alkiviades@gmail.com> wrote in message <546e87db-670a-4031-a9dc-f091e8c8082a@googlegroups.com>...
>
>
> I have added a lb and ub that is constant for all combinations but the result i get looks like this
>
> Optimization terminated.
>
> X =
>
> [2x1 double] [2x1 double] [2x1 double]
> [2x1 double] [2x1 double] [2x1 double]
> [2x1 double] [2x1 double] [2x1 double]
>
>[snip]
>
>
> fval =
>
> 1.2706e-14
>
>
> exitflag =
>
> 1
>
> I don't seem to be able to access any of the X's and get a numerical value for each combination
================

Can you elaborate on what you mean by "don't seem to be able to access"? What happens if you type X{1,1}, X{2,1}, etc...

Subject: Matlab Optimisation with multiple combinations of inequality

From: Andrew Alkiviades

Date: 29 Jun, 2012 19:26:31

Message: 16 of 29

Matt, Apologies

What I mean is:

size(X) = 27,3 - which is what is needed (and type cell)

size(PV) = 8760, 1 (and type double) which should be 8760,1,27 right?
same with
size(WT) = 8760,1 (and type double) which should be 8760,1,3?

is this correct or is the same data set of PV and WT going into linprog?

Thank you

Subject: Matlab Optimisation with multiple combinations of inequality

From: Matt J

Date: 29 Jun, 2012 19:49:18

Message: 17 of 29

Andrew Alkiviades <andrew.alkiviades@gmail.com> wrote in message <b34f9492-7f32-4000-843b-63b1618a184d@googlegroups.com>...
> Matt, Apologies
>
> What I mean is:
>
> size(X) = 27,3 - which is what is needed (and type cell)
>
> size(PV) = 8760, 1 (and type double) which should be 8760,1,27 right?
> same with
> size(WT) = 8760,1 (and type double) which should be 8760,1,3?
>
> is this correct or is the same data set of PV and WT going into linprog?
==============

As you can see from your own code:

    PV = PV_output(:,:,ii);
    WT = WT_output(:,:,jj);

This is how PV, WT get created before they are passed to linprog for each fixed ii,jj.

Since PV_output(:,:,ii) and WT_output(:,:,jj) are each 8760x1, it makes sense that PV and WT are as well.

Also if PV_output(:,:,ii) and WT_output(:,:,jj) contain different data for different ii,jj then it should be clear that linprog is receiving different data for each ii,jj.

Subject: Matlab Optimisation with multiple combinations of inequality

From: Andrew Alkiviades

Date: 29 Jun, 2012 19:59:37

Message: 18 of 29

On Friday, 29 June 2012 20:49:18 UTC+1, Matt J wrote:
> Andrew Alkiviades <andrew.alkiviades@gmail.com> wrote in message <b34f9492-7f32-4000-843b-63b1618a184d@googlegroups.com>...
> > Matt, Apologies
> >
> > What I mean is:
> >
> > size(X) = 27,3 - which is what is needed (and type cell)
> >
> > size(PV) = 8760, 1 (and type double) which should be 8760,1,27 right?
> > same with
> > size(WT) = 8760,1 (and type double) which should be 8760,1,3?
> >
> > is this correct or is the same data set of PV and WT going into linprog?
> ==============
>
> As you can see from your own code:
>
> PV = PV_output(:,:,ii);
> WT = WT_output(:,:,jj);
>
> This is how PV, WT get created before they are passed to linprog for each fixed ii,jj.
>
> Since PV_output(:,:,ii) and WT_output(:,:,jj) are each 8760x1, it makes sense that PV and WT are as well.
>
> Also if PV_output(:,:,ii) and WT_output(:,:,jj) contain different data for different ii,jj then it should be clear that linprog is receiving different data for each ii,jj.


It seems to me (please correct me if I am wrong!) That the for loop is not functioning correctly and I need to index the PV and WT with curly brackets -{ii} and {jj} respectively since the X is coming out as

>> X

X =

    [] [] []
    [] [] []
    [] [] []
    [] [] []
    [] [] []
    [] [] []
    [] [] []
    [] [] []
    [] [] []
    [] [] []
    [] [] []
    [] [] []
    [] [] []
    [] [] []
    [] [] []
    [] [] []
    [] [] []
    [] [] []
    [] [] []
    [] [] []
    [] [] []
    [] [] []
    [] [] []
    [] [] []
    [] [] []
    [] [] []
    [] [] [2x1 double]

>> X{1,1}

ans =

     []

and therefore only get a value for X{27,3}

Subject: Matlab Optimisation with multiple combinations of inequality

From: Matt J

Date: 29 Jun, 2012 21:26:12

Message: 19 of 29

Andrew Alkiviades <andrew.alkiviades@gmail.com> wrote in message <e73a956f-cc35-4120-9bc1-9dd9fe85800b@googlegroups.com>...
>
>
> It seems to me (please correct me if I am wrong!) That the for loop is not functioning correctly and I need to index the PV and WT with curly brackets -{ii} and {jj} respectively since the X is coming out as
========================

Andrew, you're taking wild guesses at everything. It seems doubtful that you understand the difference between cell arrays (variables of class cell) and numeric arrays (e.g., matrices) and the meaning of the different indexing syntaxes. You really need to read the documentation on cell arrays (doc cell) first to understand how they work and to know what's going on in your own code. Guessing randomly will not get you where you want to go.

Once you do read the documentation, you will know that curly brackets only work on cell arrays . Since PV,WT are just matrices and not cell arrays, you cannot index them with curly brackets. If you had tried, you would have gotten an error, e.g.

>> PV=rand(8760,1); PV{1}
Cell contents reference from a non-cell array object.


Now, as for the X that you are getting, it appears to me that linprog is not managing in most cases to find a solution for the data you are giving it and is therefore returning []. The exception seems to be ii=27, jj=3. It manages to find a solution for that combination of data only.

In any case, the thing to do is to just focus for now on the data combination ii=1,jj=1 and try to figure out why it is not finding a solution for that case. Run LINPROG for that case only and check the output that it gives you, including the exitflag.

Subject: Matlab Optimisation with multiple combinations of inequality

From: Andrew Alkiviades

Date: 29 Jun, 2012 22:54:14

Message: 20 of 29

On Friday, 29 June 2012 22:26:12 UTC+1, Matt J wrote:
> Andrew Alkiviades <andrew.alkiviades@gmail.com> wrote in message <e73a956f-cc35-4120-9bc1-9dd9fe85800b@googlegroups.com>...
> >
> >
> > It seems to me (please correct me if I am wrong!) That the for loop is not functioning correctly and I need to index the PV and WT with curly brackets -{ii} and {jj} respectively since the X is coming out as
> ========================
>
> Andrew, you're taking wild guesses at everything. It seems doubtful that you understand the difference between cell arrays (variables of class cell) and numeric arrays (e.g., matrices) and the meaning of the different indexing syntaxes. You really need to read the documentation on cell arrays (doc cell) first to understand how they work and to know what's going on in your own code. Guessing randomly will not get you where you want to go.
>
> Once you do read the documentation, you will know that curly brackets only work on cell arrays . Since PV,WT are just matrices and not cell arrays, you cannot index them with curly brackets. If you had tried, you would have gotten an error, e.g.
>
> >> PV=rand(8760,1); PV{1}
> Cell contents reference from a non-cell array object.
>
>
> Now, as for the X that you are getting, it appears to me that linprog is not managing in most cases to find a solution for the data you are giving it and is therefore returning []. The exception seems to be ii=27, jj=3. It manages to find a solution for that combination of data only.
>
> In any case, the thing to do is to just focus for now on the data combination ii=1,jj=1 and try to figure out why it is not finding a solution for that case. Run LINPROG for that case only and check the output that it gives you, including the exitflag.

Matt thanks for the advice, I am starting to get my head round things!

Out of interest, if I wanted to add a third technology say, CHP could I just replicate what is done so far using a third for loop for 5 CHP;s in the form similar to

for kk= 1:5

    CHP{kk} = CHP_output(:,:,kk);

so it would be after the ii, and jj loop?

Subject: Matlab Optimisation with multiple combinations of inequality

From: Matt J

Date: 30 Jun, 2012 20:35:07

Message: 21 of 29

Andrew Alkiviades <andrew.alkiviades@gmail.com> wrote in message <b79535ae-dde1-45f9-b1e5-2a2527113200@googlegroups.com>...
>
> Matt thanks for the advice, I am starting to get my head round things!
>
> Out of interest, if I wanted to add a third technology say, CHP could I just replicate what is done so far using a third for loop for 5 CHP;s in the form similar to
>
> for kk= 1:5
>
> CHP{kk} = CHP_output(:,:,kk);
>
> so it would be after the ii, and jj loop?
============

You mean you now want to look at all ii,jj,kk combinations of
PV_output(:,:,ii)
WT_output(:,:,jj)
CHP_output(:,:,kk)
?

If so, the kk-loop would be nested inside the other two, not placed after it.

It's not clear to me why you are storing CH_output(:,:,kk) to a cell array, however. That is not similar to the code we have been working with. Why not just

CHP = CHP_output(:,:,kk);

In fact, why store CHP_output(:,:,kk) or the other _outputs to any intermediate variables at all? Why not send the data directly to linprog as in

 X{ii,jj,kk}=linprog(f,[PV_output(:,:,ii), WT_output(:,:,jj), CHP_output(:,:,kk)],Demand_vector,....);

Subject: Matlab Optimisation with multiple combinations of inequality

From: Andrew Alkiviades

Date: 30 Jun, 2012 22:29:37

Message: 22 of 29

Hi Matt

Yes the for loop for k would be nested not after.

I am working with cell arrays since I find it is the easiest way to confirm all the combinations and then access "optimum" values easily for further calculations in the code that I am working on.

In terms of computational speed however, I am not sure cells are the best way but speed is not something I am worried about - as long as it is correct!!!

Subject: Matlab Optimisation with multiple combinations of inequality

From: Matt J

Date: 30 Jun, 2012 23:04:24

Message: 23 of 29

Andrew Alkiviades <andrew.alkiviades@gmail.com> wrote in message <bb5cf745-2fae-4517-a14e-1f8577272670@googlegroups.com>...
> Hi Matt
>
> Yes the for loop for k would be nested not after.
>
> I am working with cell arrays since I find it is the easiest way to confirm all the combinations and then access "optimum" values easily for further calculations in the code that I am working on.
>
> In terms of computational speed however, I am not sure cells are the best way but speed is not something I am worried about - as long as it is correct!!!
================

Fair enough, if that's what you. Bear in mind though that by storing every combination of your PV, WT and now CHP data to separate cell arrays, you are multiplying your memory consumption considerably. Originally, for example, you only had 3 sets of WT_ouput(:,:,j) data. Now, copies of each these same data sets will occur in memory 27*5 more times, because that's how many combinations each of them participates in.

Subject: Matlab Optimisation with multiple combinations of inequality

From: Andrew Alkiviades

Date: 30 Jun, 2012 23:11:51

Message: 24 of 29

On Sunday, 1 July 2012 00:04:24 UTC+1, Matt J wrote:
> Andrew Alkiviades <andrew.alkiviades@gmail.com> wrote in message <bb5cf745-2fae-4517-a14e-1f8577272670@googlegroups.com>...
> > Hi Matt
> >
> > Yes the for loop for k would be nested not after.
> >
> > I am working with cell arrays since I find it is the easiest way to confirm all the combinations and then access "optimum" values easily for further calculations in the code that I am working on.
> >
> > In terms of computational speed however, I am not sure cells are the best way but speed is not something I am worried about - as long as it is correct!!!
> ================
>
> Fair enough, if that's what you. Bear in mind though that by storing every combination of your PV, WT and now CHP data to separate cell arrays, you are multiplying your memory consumption considerably. Originally, for example, you only had 3 sets of WT_ouput(:,:,j) data. Now, copies of each these same data sets will occur in memory 27*5 more times, because that's how many combinations each of them participates in.

Ahh yes thats a very good point! Even more I am further thinking of adding a battery storage later on to this code which will complicate things further - especially with memory. - having said that I will be reducing the 27 PVs to 8.
I am not sure I understand how I can get the combinations of all PVs and WTs and CHPs without the cell though.
Are you suggesting then to go straight from the equation of PV_output to linprog?

Subject: Matlab Optimisation with multiple combinations of inequality

From: Matt J

Date: 30 Jun, 2012 23:52:26

Message: 25 of 29

Andrew Alkiviades <andrew.alkiviades@gmail.com> wrote in message <3b0f90c0-6932-4094-8110-df6e1af93008@googlegroups.com>...
>
> I am not sure I understand how I can get the combinations of all PVs and WTs and CHPs without the cell though.
> Are you suggesting then to go straight from the equation of PV_output to linprog?
==============

Well, in the code examples I've been showing you, we've been putting the PVs and WTs together in particular combinations only at the point in the code where we need them. When a combination is no longer needed, we throw that combination away, or overwrite it with something else. Basically, I've always given you something like this:

for i=1:N
 for j=1:M

   A=[PV_output(:,:,i), WT_output(:,:,j)];
  
   linprog(f,A,b)


 end
end

So, in the above, the matrix A uses PV and WT in some particular combination. The point to understand, though, is that A gets overwritten with a new combination with each new pass through the double for-loop. No one particular combination gets saved (except the final combination in the loop unless you clear A from memory afterward). Because you aren't storing all combinations simultaneously, you conserve memory better.

I can understand, though, if you want to access a particular combination with an easy syntax, but to do that it might be sufficient just to write yourself a small anonymous function that takes a pair (i,j) as input at assembles the corresponding data combination when you want it, e.g.

getcomb=@(i,j) [PV_output(:,:,i), WT_output(:,:,j)];

Now you can do things like,

A=getcomb(1,2);
A=getcomb(27,3);

etc...

Subject: Matlab Optimisation with multiple combinations of inequality

From: Andrew Alkiviades

Date: 2 Jul, 2012 14:44:14

Message: 26 of 29

On Sunday, 1 July 2012 00:52:26 UTC+1, Matt J wrote:
> Andrew Alkiviades <andrew.alkiviades@gmail.com> wrote in message <3b0f90c0-6932-4094-8110-df6e1af93008@googlegroups.com>...
> >
> > I am not sure I understand how I can get the combinations of all PVs and WTs and CHPs without the cell though.
> > Are you suggesting then to go straight from the equation of PV_output to linprog?
> ==============
>
> Well, in the code examples I've been showing you, we've been putting the PVs and WTs together in particular combinations only at the point in the code where we need them. When a combination is no longer needed, we throw that combination away, or overwrite it with something else. Basically, I've always given you something like this:
>
> for i=1:N
> for j=1:M
>
> A=[PV_output(:,:,i), WT_output(:,:,j)];
>
> linprog(f,A,b)
>
>
> end
> end
>
> So, in the above, the matrix A uses PV and WT in some particular combination. The point to understand, though, is that A gets overwritten with a new combination with each new pass through the double for-loop. No one particular combination gets saved (except the final combination in the loop unless you clear A from memory afterward). Because you aren't storing all combinations simultaneously, you conserve memory better.
>
> I can understand, though, if you want to access a particular combination with an easy syntax, but to do that it might be sufficient just to write yourself a small anonymous function that takes a pair (i,j) as input at assembles the corresponding data combination when you want it, e.g.
>
> getcomb=@(i,j) [PV_output(:,:,i), WT_output(:,:,j)];
>
> Now you can do things like,
>
> A=getcomb(1,2);
> A=getcomb(27,3);
>
> etc...

Matt, is it possible to have the X as an integer value since the results I am getting are for example - 0.5X1 and 0.7X2 ?
The reason I ask is because I am trying to work with manufacturers on the market and so physically speaking there is no 0.5X1 and 0.7X2? Or do I just round up the X values at the end of the optimisation?

Subject: Matlab Optimisation with multiple combinations of inequality

From: Matt J

Date: 2 Jul, 2012 15:14:07

Message: 27 of 29

Andrew Alkiviades <andrew.alkiviades@gmail.com> wrote in message <2f94b22a-bbae-4105-bcec-197187b10fd9@googlegroups.com>...
>
>
> Matt, is it possible to have the X as an integer value since the results I am getting are for example - 0.5X1 and 0.7X2 ?

I'm not sure what the notation 0.5X1 means or how it indicates that X1 is not an integer. Do you mean X1=0.5 and X2=0.7?

Anyway, MATLAB unfortunately does not have an integer programming solver, except BINTPROG, which works only for binary integer programs. You would have to look to other packages beyond MATLAB for that.


> Or do I just round up the X values at the end of the optimisation?
==============

That's a question that only you can answer. Whether such an approximate solution is tolerable depends on the application and the tolerances of the end-user, which only you know.

Subject: Matlab Optimisation with multiple combinations of inequality

From: Andrew Alkiviades

Date: 2 Jul, 2012 19:54:08

Message: 28 of 29

On Monday, 2 July 2012 16:14:07 UTC+1, Matt J wrote:
> Andrew Alkiviades <andrew.alkiviades@gmail.com> wrote in message <2f94b22a-bbae-4105-bcec-197187b10fd9@googlegroups.com>...
> >
> >
> > Matt, is it possible to have the X as an integer value since the results I am getting are for example - 0.5X1 and 0.7X2 ?
>
> I'm not sure what the notation 0.5X1 means or how it indicates that X1 is not an integer. Do you mean X1=0.5 and X2=0.7?
>
> Anyway, MATLAB unfortunately does not have an integer programming solver, except BINTPROG, which works only for binary integer programs. You would have to look to other packages beyond MATLAB for that.
>
>
> > Or do I just round up the X values at the end of the optimisation?
> ==============
>
> That's a question that only you can answer. Whether such an approximate solution is tolerable depends on the application and the tolerances of the end-user, which only you know.

Hi Matt

What I mean is that I require an integer value of x's since they represent the optimum number of units of a particular technology.
My results ( i have 4 x's in the objective function) look something like:

 x =
 6.6667
 0.0420
 0
 0

but of course you can't have say 0.04 wind turbines so I was just wondering if there is a way to force linprog to find the optimum integer values of x?

Thanks!
Therefore, physically speaking

Subject: Matlab Optimisation with multiple combinations of inequality

From: Matt J

Date: 2 Jul, 2012 20:23:07

Message: 29 of 29

Andrew Alkiviades <andrew.alkiviades@gmail.com> wrote in message <3765b2cd-63c2-483e-bccb-d0ec2ffad0fa@googlegroups.com>...
>
>so I was just wondering if there is a way to force linprog to find the optimum integer values of x?
=================

Like I said, you can't. However, because you have a small number (n=4) of variables, what you might do is post-process the output of linprog by checking all of its 2^4 nearest integer neighbours and manually evaluate the objective function at those points. Take the one that has the most optimum value (and which satisfies the contraints).

Tags for this Thread

No tags are associated with this thread.

What are tags?

A tag is like a keyword or category label associated with each thread. Tags make it easier for you to find threads of interest.

Anyone can tag a thread. Tags are public and visible to everyone.

Contact us