function [SupplyOfferCurvesPrices, SupplyOfferCurvesQuantity, DemandOfferCurvesPrices,DemandOfferCurvesQuantity, quantitiesSupplyPeer, quantitiesDemandPeer, maxPricesSupply, minPricesDemand, marketClearingPrices, quantitiesPeers] = Euphemia_15092019(T,B,I)
fprintf('\n');
% T=period
% B=blocks in bidding curve
% I=peers
% ACCEPT [0;1]=Acceptance variables

%supply and demand variables
SupplyOfferCurvesPrices = zeros(T, B, I);
SupplyOfferCurvesQuantity = zeros(T, B, I);

DemandOfferCurvesPrices = zeros(T, B, I);
DemandOfferCurvesQuantity = zeros(T, B, I);

    %Data import
    for i = 1:I-2       
        SupplyOfferCurvesPrices(1:T,1:B,i)=readmatrix(join(["Load_profile",string(i),"edited.xlsm"],"_"),'Sheet','SP','Range','D98:W121');
        SupplyOfferCurvesQuantity(1:T,1:B,i)=readmatrix(join(["Load_profile",string(i),"edited.xlsm"],"_"),'Sheet','SQ','Range','D98:W121');
        DemandOfferCurvesPrices(1:T,1:B,i)=readmatrix(join(["Load_profile",string(i),"edited.xlsm"],"_"),'Sheet','DP','Range','D98:W121');
        DemandOfferCurvesQuantity(1:T,1:B,i)=readmatrix(join(["Load_profile",string(i),"edited.xlsm"],"_"),'Sheet','DQ','Range','D98:W121'); 
    end

     %Feeder
     SupplyOfferCurvesPrices(1:T,1:B,I-1)=0.100; % EUR/kWh Electricity price for final consumers without DSO tarif
     SupplyOfferCurvesPrices(1:T,1:B,I)=0.100;
     
     SupplyOfferCurvesQuantity(1:T,1:B,I-1)=-0.5;
     SupplyOfferCurvesQuantity(1:T,1:B,I)=0;
     
     DemandOfferCurvesPrices(1:T,1:B,I-1)=0.050; % EUR/kWh energy price*0.9
     DemandOfferCurvesPrices(1:T,1:B,I)=0.050;
     
     DemandOfferCurvesQuantity(1:T,1:B,I-1)=0;
     DemandOfferCurvesQuantity(1:T,1:B,I)=0.5;
     
%Definition of problem and variables
Euphemia = optimproblem('ObjectiveSense','max','Description','Calculating UC and MCP');
ACCEPT = optimvar('ACCEPT',T,B,2*I,'LowerBound',0, 'UpperBound',1,'Type','integer');

%Objective
obj = optimexpr;
for i = 1:I
    for b = 1:B
        for t = 1:T
            obj = obj+ACCEPT(t,b,i)*SupplyOfferCurvesPrices(t,b,i)*SupplyOfferCurvesQuantity(t,b,i)...
            +ACCEPT(t,b,I+i)*DemandOfferCurvesPrices(t,b,i)*DemandOfferCurvesQuantity(t,b,i);
        end
    end
end
showexpr(obj)

Euphemia.Objective = obj;

%Constraints
supply = optimexpr(T);
demand = optimexpr(T);
for t = 1:T
    for b=1:B        
        for i=1:I   
            supply(t) = supply(t) - ACCEPT(t,b,i)*SupplyOfferCurvesQuantity(t,b,i); 
            demand(t)=demand(t)+ACCEPT(t,b,I+i)*DemandOfferCurvesQuantity(t,b,i);
        end
    end
end

balance=optimconstr(T);
for t = 1:T
 balance(t)=supply(t)>=demand(t);
end

Euphemia.Constraints.Balance1=balance;
showproblem(Euphemia);
solEuphemia = solve(Euphemia);

    solACCEPT = solEuphemia.ACCEPT;

% finding equilibrium prices and quantities
% reading cell values to array
  
    %Accepted offers
    pricesSupply=solACCEPT(1:T,1:B,1:I).*SupplyOfferCurvesPrices;  
    pricesDemand=solACCEPT(1:T,1:B,(I+1):(2*I)).*DemandOfferCurvesPrices; 
    
    quantitiesSupply=solACCEPT(1:T,1:B,1:I).*SupplyOfferCurvesQuantity;   
    quantitiesDemand=solACCEPT(1:T,1:B,(I+1):(2*I)).*DemandOfferCurvesQuantity;  
    
    % finding max accepted supply offer and min accepted demand offer (bigger than supply)
    for t = 1:T
        for b=1:B
            for i=1:I
                if solACCEPT(t,b,i) == 0;
                    pricesSupply(t,b,i) = NaN;
                end
                if solACCEPT(t,b,I+i) == 0;
                    pricesDemand(t,b,i) = NaN;
                end    
            end
        end
        maxPricesSupply(t)=max(max(pricesSupply(t,:,:)));
        minPricesDemand(t)=min(min(pricesDemand(t,:,:)));
      end
    
    % average of max supply and min demand offer for each hour
    marketClearingPrices = (maxPricesSupply + minPricesDemand)/2;
    
    % finding accepted demand and suppy quantities for period t and peer i
    for t = 1:T
        for i = 1:I
            quantitiesSupplyPeer(t,i)=sum(quantitiesSupply(t,:,i));
            quantitiesDemandPeer(t,i)=sum(quantitiesDemand(t,:,i));
        end
    end
    quantitiesPeers=quantitiesSupplyPeer+quantitiesDemandPeer;
  
    
end

