clc
LoadDemand=[0.1 0.5 0.4 3 5 3 3.5 0.1 0.1 1 3 2 2.3 2.3 2 2.5 5 6 7 6.5...
4 2.5 2.3 0.1];
SolarIrradiation=[0 0 0 0 0.1 0.3 0.5 0.65 0.8 0.9 1 0.95 0.9 0.8 0.65...
0.4 0.3 0.08 0 0 0 0 0 0];
WindVelocity=repmat(5,1,24);
Cgrid=[0.033 0.027 0.020 0.017 0.017 0.029 0.033 0.054 0.215...
0.572 0.572 0.572 0.215 0.572 0.286 0.279 0.086 0.059 0.050 0.061...
0.181 0.077 0.043 0.037];
OPcostPV=0.01; OPcostW=0.01; OPcostEVch=0.01; OPcostEVdisch=0.01;
OPcostBch=0.01; OPcostBdisch=0.01; SellingCost=0.29;
dt=1;
NOMb=10;
NOMbInit=6;
SOCminB=0.2;
NOMev=24;
NOMevInit=16;
SOCminev=0.2
ec=1.1;
ed=0.9;
PVeffi=0.186;
A=25;
Vci=4;
Vco=24;
Vr=14;
PbChmax=1;
PbDischmax=1;
PevChmax=3.3;
PevDischmax=3.3;
DevDrive=2;
nHours=numel(LoadDemand);
Time=(1:nHours)';
idxHr2Toend=2:nHours;
DriveTime=Time(9:18);
powerprob=optimproblem;
Pgrid=optimvar('Pgrid',nHours,1,'LowerBound',0,'UpperBound',5);
Ppv=optimvar('Ppv',nHours,1,'LowerBound',0,'UpperBound',3.5);
Pwind=optimvar('Pwind',nHours,1,'LowerBound',0,'UpperBound',2.4);
PevCh=optimvar('PevCh',nHours,1,'LowerBound',0,'UpperBound',3.3);
PevDisch=optimvar('PevDisch',nHours,1,'LowerBound',0,'UpperBound',3.3);
PbCh=optimvar('PbCh',nHours,1,'LowerBound',0,'UpperBound',1);
PbDisch=optimvar('PbDisch',nHours,1,'LowerBound',0,'UpperBound',1);
Psell=optimvar('Psell',nHours,1,'LowerBound',0,'UpperBound',inf);
SOCb=optimvar('SOCb',nHours,1,'LowerBound',0.2,'UpperBound',1);
SOCev=optimvar('SOCev',nHours,1,'LowerBound',0.2,'UpperBound',1);
isONevCh=optimvar('isONevCh',nHours,1,'Type','integer','LowerBound',0,'UpperBound',1);
isONevDisch=optimvar('isONevDisch',nHours,1,'Type','integer','LowerBound',0,'UpperBound',1);
isONbCh=optimvar('isONbCh',nHours,1,'Type','integer','LowerBound',0,'UpperBound',1);
isONbDisch=optimvar('isONbDisch',nHours,1,'Type','integer','LowerBound',0,'UpperBound',1);
startupBdisch=optimvar('startupBdisch',nHours,1,'Type','integer','LowerBound',0,'UpperBound',1);
startupBch=optimvar('startupBch',nHours,1,'Type','integer','LowerBound',0,'UpperBound',1);
startupPevdisCh=optimvar('startupPEVdisch',nHours,1,'Type','integer','LowerBound',0,'UpperBound',1);
startupPevCh=optimvar('startupPEVch',nHours,1,'Type','integer','LowerBound',0,'UpperBound',1);
gridCost = sum(((Pgrid*dt).*Cgrid'),1);
PVGenCost = sum(((Ppv*dt)*OPcostPV),1);
WindgenCost = sum(((Pwind*dt)*OPcostW),1);
EVdischCost = sum(((PevDisch*dt)*OPcostEVdisch),1);
BatterydischCost = sum(((PbDisch*dt)*OPcostBdisch),1);
SellCost = sum(((Psell*dt)*SellingCost),1);
powerprob.Objective= gridCost + PVGenCost + WindgenCost + EVdischCost + BatterydischCost - SellCost;
powerprob.Constraints.PowerBalance = Pgrid + Ppv + Pwind + PevDisch + PbDisch - LoadDemand' - PevCh - PbCh - Psell==0;
powerprob.Constraints.BatStoredElectricity=(NOMb*SOCb(idxHr2Toend,:))-(NOMb*SOCb(idxHr2Toend-1,:))-(((PbCh(idxHr2Toend,:)*dt)/ec)-(ed*PbDisch(idxHr2Toend,:)*dt))==0;
powerprob.Constraints.InitBatStoredElectricity=(NOMb*SOCb(1))-NOMbInit-(((PbCh(idxHr2Toend,:)*dt)/ec)-(ed*PbDisch(idxHr2Toend,:)*dt))==0;
powerprob.Constraints.EVBatStoredElectricity=(NOMev*SOCev(idxHr2Toend,:))-(NOMev*SOCev(idxHr2Toend-1,:))-(((PevCh(idxHr2Toend,:)*dt)/ec)-(ed*PevDisch(idxHr2Toend,:)*dt))==0;
powerprob.Constraints.EVInitBatStoredElectricity=(NOMev*SOCev(1))-NOMevInit-(((PevCh(idxHr2Toend,:)*dt)/ec)-(ed*PevDisch(idxHr2Toend,:)*dt))==0;
if WindVelocity(1:nHours)<Vci
Pwind=0
elseif WindVelocity(1:nHours)>Vco
Pwind=0
elseif Vr<WindVelocity(1:nHours)<Vco
Pwind=2.1
elseif Vci<WindVelocity(1:nHours)<Vr
Pwind=2.1*((WindVelocity(1:nHours)-Vci)/(Vr-Vci))
end
powerprob.Constraints.PVout=Ppv<=A*PVeffi*SolarIrradiation';
powerprob.Constraints.PbCharge=PbCh<=PbChmax*isONbCh;
powerprob.Constraints.PbDischarge=PbDisch<=PbDischmax*isONbDisch;
powerprob.Constraints.MaxBatCh=((PbCh(idxHr2Toend,:)*dt)/ec)+(NOMb*SOCb(idxHr2Toend-1,:))<=NOMb;
powerprob.Constraints.NoBChDischtog=isONbCh+isONbDisch<=1
powerprob.Constraints.PevCharge=PevCh<=PevChmax*isONevCh;
powerprob.Constraints.PevDischarge=PevDisch<=PevDischmax*isONevDisch;
powerprob.Constraints.MaxEVBatCh=((PevCh(idxHr2Toend,:)*dt)/ec)+(NOMev*SOCev(idxHr2Toend-1,:))<=NOMev;
powerprob.Constraints.NoBEVChDischtog=isONevCh+isONevDisch<=1
powerprob.Constraints.NOevchout=PevCh(DriveTime)==0;
powerprob.Constraints.EVdrive=(PevDisch(DriveTime))*dt==2;
powerprob.Constraints.startupConstbCh= -isONbCh(idxHr2Toend-1,:)+isONbCh(idxHr2Toend,:)-startupBch(idxHr2Toend,:)<=0;
powerprob.Constraints.startupConstbDisch= -isONbDisch(idxHr2Toend-1,:)+isONbDisch(idxHr2Toend,:)-startupBdisch(idxHr2Toend,:)<=0;
powerprob.Constraints.startupConstEVch= -isONevCh(idxHr2Toend-1,:)+isONevCh(idxHr2Toend,:)-startupPevCh(idxHr2Toend,:)<=0;
powerprob.Constraints.startupConstEVdisch= -isONevDisch(idxHr2Toend-1,:)+isONevDisch(idxHr2Toend,:)-startupPevdisCh(idxHr2Toend,:)<=0;
options=optimoptions('intlinprog','Maxtime',1000);
[sol,TotalCost,exitflag,output]=solve(powerprob,'options',options);