SetDirectory[NotebookDirectory[]];

(* Find flow of cost corresponding to a given PV using 3% and 20 years, then discount that flow using 4% and 30 years. *)
change320to430[claimedPV_]:=Sum[(claimedPV/Sum[1/(1+.03)^t,{t,1,20}])/(1+.04)^t,{t,1,30}]
powercostadjustment[claimedPV_]:=-claimedPV+change320to430[claimedPV]

(* Each reservoir has a capacity, "AF" for acre-feet, and a cost. *)
(* "Position" refers to position in the data structure *)
AFPosition=1;CostPosition=2;

(* RESERVOIRS *)
(* Fielding Reservoir *)
Fielding0={0,0};FieldingPosition=1; (* Fielding is the first reservoir in the data structure. *)
Fielding40={40000,137705000};
Fielding70={70000,169527000};
(* Whites Valley Reservoir *)
WhitesV0={0,0};  WhitesVPosition=2 ;WhitesV170={170000,360672000};WhitesV305={305000,381913000};WhitesV309={309000,384842000};WhitesV319={319000,385763000};WhitesV330={330000,389651000};WhitesV333={333000,388140000};(* 330k vs. 333k anomalous but not by much *)
WhitesV360={360000,397645000};WhitesV400={400000,414726000};WhitesV540={540000,483385000};WhitesV610={610000,533953000};
(* South Willard Reservoir *)
SWillard0={0,0};  SWillardPosition=3;SWillard={55000,427793000};
(* Cub River Reservoir *)
CubR0={0,0};  CubRPosition=4;CubR={27000,113461000};
(* Above Cutler Reservoir *)
AboveCutler0={0,0};  AboveCutlerPosition=5;AboveCutler={51000,285506000};
(* Temple Fork Reservoir *)
TempleFork0={0,0};  TempleForkPosition=6;TempleFork={41000,100009000};
(* PUMPS AND PIPELINES *)
FieldingPump=187554000;
FieldingPumpAdjustment=114196000;
(* Subtract this if Fielding but not WhitesV and not PipeFieldingCutler *)
(*  This adjustment comes from the State's scenario "I". *)
PipeFieldingWHaven=399420000;
PipeFieldingCutlerShort=37175000; (* with Fielding40 *)
PipeFieldingCutlerLong=50195000; (* with Fielding70 or Fielding0 *)
(* Cache needs either PipeFieldingCutler (short or long) or one of: Cub River, Above Cutler, Temple Fork. *)
BearRDiversion=11686600; (* If (no Fielding) and (Box Elder or Weber or Jordan or (Cache and PipeFieldingCutler)) *)

(* ACRE-FEET OF WATER REQUIRED TO BE DELIVERED *)
CacheAF=60000;
BoxElderAF=60000;
WeberAF=50000;
JordanAF=50000;

FieldingPump=FieldingPump+powercostadjustment[106835000]
WhitesV170={WhitesV170[[1]],WhitesV170[[2]]+powercostadjustment[36197000]}
WhitesV305={WhitesV305[[1]],WhitesV305[[2]]+powercostadjustment[43502000]}
WhitesV309={WhitesV309[[1]],WhitesV309[[2]]+powercostadjustment[45711000]}
WhitesV319={WhitesV319[[1]],WhitesV319[[2]]+powercostadjustment[44722000]}
WhitesV330={WhitesV330[[1]],WhitesV330[[2]]+powercostadjustment[46180000]}
WhitesV333={WhitesV333[[1]],WhitesV333[[2]]+powercostadjustment[43889000]}
WhitesV360={WhitesV360[[1]],WhitesV360[[2]]+powercostadjustment[47184000]}
WhitesV400={WhitesV400[[1]],WhitesV400[[2]]+powercostadjustment[48255000]}
WhitesV540={WhitesV540[[1]],WhitesV540[[2]]+powercostadjustment[51454000]}
WhitesV610={WhitesV610[[1]],WhitesV610[[2]]+powercostadjustment[52622000]}
TempleFork={TempleFork[[1]],TempleFork[[2]]+powercostadjustment[2008000]}
SWillard={SWillard[[1]],SWillard[[2]]+powercostadjustment[12117500]}
FieldingPumpAdjustment=187554000-(73358000+powercostadjustment[34010000])

Volume 1 of 2019 Report, page 47:  “To develop a reliable annual supply of 220,000 acre-feet during four similar years, reservoir storage of more than 400,000 acre-feet would be needed.”

ConvertAFFlowToAFStorage=400000/220000;

CacheAF=             CacheAF*ConvertAFFlowToAFStorage;
BoxElderAF=BoxElderAF*ConvertAFFlowToAFStorage;
WeberAF=             WeberAF*ConvertAFFlowToAFStorage;
JordanAF=          JordanAF*ConvertAFFlowToAFStorage;

AddToCost[additionalcost_,{{af1_,c1_},{af2_,c2_},{af3_,c3_},{af4_,c4_},{af5_,c5_},{af6_,c6_},{aftotal_,costtotal_}}]:=
{{af1,c1},{af2,c2},{af3,c3},{af4,c4},{af5,c5},{af6,c6},{aftotal,costtotal+additionalcost}}

LeastCostOptions[CacheTrueFalse_,BoxElderTrueFalse_,WeberTrueFalse_,JordanTrueFalse_]:=Module[{NeededAF,thetuples,thetotals,Reservoirs,ReservoirsAndPumpsPipes},
(* begin module *)
Cache=CacheTrueFalse;BoxElder=BoxElderTrueFalse;
Weber=WeberTrueFalse;Jordan=JordanTrueFalse;
NeededAF=If[Cache,CacheAF,0]+If[BoxElder,BoxElderAF,0]+If[Weber,WeberAF,0]+If[Jordan,JordanAF,0];
thetuples=Tuples[{{Fielding0,Fielding40,Fielding70},{WhitesV0,WhitesV170,WhitesV305,WhitesV309,WhitesV319,WhitesV330,WhitesV333,WhitesV360,WhitesV400,WhitesV540,WhitesV610},
{SWillard0,SWillard},{CubR0,CubR},{AboveCutler0,AboveCutler},
{TempleFork0,TempleFork}}];
thetotals=Map[Apply[Plus,#]&,thetuples];TuplesAndTotals=Diagonal[Outer[Append,thetuples,thetotals,1]];
TotalsPosition=7;
Reservoirs=Select[TuplesAndTotals,#[[TotalsPosition,AFPosition]]≥NeededAF&];
Export["OutputNewBear0.dat",Reservoirs];
Print["Number of tuples: ",Length[thetuples]," and number satisfying the acre-feet requirement: ",Length[Reservoirs]];
ReservoirsAndPumpsPipes=
Map[If[Cache==True&&BoxElder==False&&Weber==False&&Jordan==False&&(#[[CubRPosition,AFPosition]]>0||#[[AboveCutlerPosition,AFPosition]]>0||#[[TempleForkPosition,AFPosition]]>0) (*then Fielding Pump is unneeded*),
#, (* else Fielding Pump is needed *)
If[(#[[CubRPosition,AFPosition]]>0||#[[AboveCutlerPosition,AFPosition]]>0||#[[TempleForkPosition,AFPosition]]>0)&&#[[WhitesVPosition,AFPosition]]==0&&#[[FieldingPosition,AFPosition]]>0,
AddToCost[FieldingPump-FieldingPumpAdjustment,#],(*else*)
AddToCost[FieldingPump,#]
]]&,Reservoirs];
Export["OutputNewBear1.dat",ReservoirsAndPumpsPipes];
ReservoirsAndPumpsPipes=Map[
If[Cache==False||(#[[CubRPosition,AFPosition]]>0&&#[[AboveCutlerPosition,AFPosition]]>0&&#[[TempleForkPosition,AFPosition]]>0),#,(* else Pipeline Fielding/Cutler is needed *)
If[#[[FieldingPosition,AFPosition]]==40000,
AddToCost[PipeFieldingCutlerShort,#],
AddToCost[PipeFieldingCutlerLong,#]]
]&,
ReservoirsAndPumpsPipes];
Export["OutputNewBear2.dat",ReservoirsAndPumpsPipes];
ReservoirsAndPumpsPipes=
Map[If[Weber==True||Jordan==True,
AddToCost[PipeFieldingWHaven,#],#]&,ReservoirsAndPumpsPipes];
Export["OutputNewBear3.dat",ReservoirsAndPumpsPipes];
ReservoirsAndPumpsPipes=
Map[If[#[[FieldingPosition,AFPosition]]==0&&
(BoxElder||Weber||Jordan||(Cache&&(#[[CubRPosition,AFPosition]]==0&&#[[AboveCutlerPosition,AFPosition]]==0&&#[[TempleForkPosition,AFPosition]]==0)))
,
AddToCost[BearRDiversion,#],#]&,ReservoirsAndPumpsPipes];
Export["OutputNewBear4.dat",ReservoirsAndPumpsPipes];
ReservoirsAndPumpsPipes=Sort[ReservoirsAndPumpsPipes,#1[[TotalsPosition,CostPosition]]<#2[[TotalsPosition,CostPosition]]&];
Export["OutputNewBear5.dat",ReservoirsAndPumpsPipes];
ReservoirsAndPumpsPipes[[1(*;;10*)]]//TableForm
(* Fielding WhitesV SWillard CubR AboveCutler TempleFork *)
](* end module *)

(* Scenario 1. *)
(* CacheTrueFalse, BoxElderTrueFalse,W eberTrueFalse, JordanTrueFalse *)
LeastCostOptions[True,True,True,True]
SummaryTable={{1,%[[TotalsPosition, AFPosition]],%[[TotalsPosition,CostPosition]]}}
(* Fielding WhitesV SWillard CubR AboveCutler TempleFork *)

(* Scenario 2. *)
(* CacheTrueFalse, BoxElderTrueFalse, WeberTrueFalse, JordanTrueFalse *)
LeastCostOptions[False,True,True,True]
SummaryTable=AppendTo[SummaryTable,{2,%[[TotalsPosition, AFPosition]],%[[TotalsPosition,CostPosition]]}]
(* Fielding WhitesV SWillard CubR AboveCutler TempleFork *)

(* Scenario 3. *)
(* CacheTrueFalse, BoxElderTrueFalse, WeberTrueFalse, JordanTrueFalse *)
LeastCostOptions[True,False,True,True]
SummaryTable=AppendTo[SummaryTable,{3,%[[TotalsPosition, AFPosition]],%[[TotalsPosition,CostPosition]]}];
(* Fielding WhitesV SWillard CubR AboveCutler TempleFork *)

(* Scenario 4. *)
(* CacheTrueFalse, BoxElderTrueFalse, WeberTrueFalse, JordanTrueFalse *)
LeastCostOptions[True,True,False,True]
SummaryTable=AppendTo[SummaryTable,{4,%[[TotalsPosition, AFPosition]],%[[TotalsPosition,CostPosition]]}];
(* Fielding WhitesV SWillard CubR AboveCutler TempleFork *)

(* Scenario 5. *)
(* CacheTrueFalse, BoxElderTrueFalse, WeberTrueFalse, JordanTrueFalse *)
LeastCostOptions[True,True,True,False]
SummaryTable=AppendTo[SummaryTable,{5,%[[TotalsPosition, AFPosition]],%[[TotalsPosition,CostPosition]]}];
(* Fielding WhitesV SWillard CubR AboveCutler TempleFork *)

(* Scenario 6. *)
(* CacheTrueFalse, BoxElderTrueFalse, WeberTrueFalse, JordanTrueFalse *)
LeastCostOptions[False,False,True,True]
SummaryTable=AppendTo[SummaryTable,{6,%[[TotalsPosition, AFPosition]],%[[TotalsPosition,CostPosition]]}];
(* Fielding WhitesV SWillard CubR AboveCutler TempleFork *)

(* Scenario 7. *)
(* CacheTrueFalse, BoxElderTrueFalse, WeberTrueFalse, JordanTrueFalse *)
LeastCostOptions[False,True,False,True]
SummaryTable=AppendTo[SummaryTable,{7,%[[TotalsPosition, AFPosition]],%[[TotalsPosition,CostPosition]]}];
(* Fielding WhitesV SWillard CubR AboveCutler TempleFork *)

(* Scenario 8. *)
(* CacheTrueFalse, BoxElderTrueFalse, WeberTrueFalse, JordanTrueFalse *)
LeastCostOptions[False,True,True,False]
SummaryTable=AppendTo[SummaryTable,{8,%[[TotalsPosition, AFPosition]],%[[TotalsPosition,CostPosition]]}];
(* Fielding WhitesV SWillard CubR AboveCutler TempleFork *)

(* Scenario 9. *)
(* CacheTrueFalse, BoxElderTrueFalse, WeberTrueFalse, JordanTrueFalse *)
LeastCostOptions[True,False,False,True]
SummaryTable=AppendTo[SummaryTable,{9,%[[TotalsPosition, AFPosition]],%[[TotalsPosition,CostPosition]]}];
(* Fielding WhitesV SWillard CubR AboveCutler TempleFork *)

(* Scenario 10. *)
(* CacheTrueFalse, BoxElderTrueFalse, WeberTrueFalse, JordanTrueFalse *)
LeastCostOptions[True,False,True,False]
SummaryTable=AppendTo[SummaryTable,{10,%[[TotalsPosition, AFPosition]],%[[TotalsPosition,CostPosition]]}];
(* Fielding WhitesV SWillard CubR AboveCutler TempleFork *)

(* Scenario 11. *)
(* CacheTrueFalse, BoxElderTrueFalse, WeberTrueFalse, JordanTrueFalse *)
LeastCostOptions[True,True,False,False]
SummaryTable=AppendTo[SummaryTable,{11,%[[TotalsPosition, AFPosition]],%[[TotalsPosition,CostPosition]]}];
(* Fielding WhitesV SWillard CubR AboveCutler TempleFork *)

(* Scenario 12. *)
(* CacheTrueFalse, BoxElderTrueFalse, WeberTrueFalse, JordanTrueFalse *)
LeastCostOptions[False,False,False,True]
SummaryTable=AppendTo[SummaryTable,{12,%[[TotalsPosition, AFPosition]],%[[TotalsPosition,CostPosition]]}];
(* Fielding WhitesV SWillard CubR AboveCutler TempleFork *)

(* Scenario 13. *)
(* CacheTrueFalse, BoxElderTrueFalse, WeberTrueFalse, JordanTrueFalse *)
LeastCostOptions[False,False,True,False]
SummaryTable=AppendTo[SummaryTable,{13,%[[TotalsPosition, AFPosition]],%[[TotalsPosition,CostPosition]]}];
(* Fielding WhitesV SWillard CubR AboveCutler TempleFork *)

(* Scenario 14. *)
(* CacheTrueFalse, BoxElderTrueFalse, WeberTrueFalse, JordanTrueFalse *)
LeastCostOptions[False,True,False,False]
SummaryTable=AppendTo[SummaryTable,{14,%[[TotalsPosition, AFPosition]],%[[TotalsPosition,CostPosition]]}];
(* Fielding WhitesV SWillard CubR AboveCutler TempleFork *)

(* Scenario 15. *)
(* CacheTrueFalse, BoxElderTrueFalse, WeberTrueFalse, JordanTrueFalse *)
LeastCostOptions[True,False,False,False]
SummaryTable=AppendTo[SummaryTable,{15,%[[TotalsPosition, AFPosition]],%[[TotalsPosition,CostPosition]]}];
(* Fielding WhitesV SWillard CubR AboveCutler TempleFork *)

writeWithoutExponent[number_]:=ToString[NumberForm[number,ExponentFunction->(If[-20<#<20,Null,#]&)]]
Transpose[MapAt[Map[writeWithoutExponent,#]&,Transpose[SummaryTable],3]];
TableForm[%]
Export["OutputNewBear6.dat",%]