Skip to content

Commit a64719c

Browse files
James CraigJames Craig
authored andcommitted
v481 : fixed management indexing bug
in DemandOptimization.cpp when mixing goals/constraints
1 parent 9d82ff8 commit a64719c

File tree

3 files changed

+26
-30
lines changed

3 files changed

+26
-30
lines changed

src/DemandOptimization.cpp

Lines changed: 25 additions & 24 deletions
Original file line numberDiff line numberDiff line change
@@ -1497,11 +1497,14 @@ void CDemandOptimizer::SolveDemandProblem(CModel *pModel, const optStruct &Optio
14971497
}
14981498
}
14991499

1500-
//ensure slack counters are properly incremented
1501-
s++;
1502-
if (_pGoals[j]->pOperRegimes[0]->pExpression->compare == COMPARE_IS_EQUAL) //two slack variables
1500+
//ensure slack counters are properly incremented (only goals have slack vars)
1501+
if (_pGoals[j]->is_goal)
15031502
{
15041503
s++;
1504+
if (_pGoals[j]->pOperRegimes[0]->pExpression->compare == COMPARE_IS_EQUAL) //two slack variables
1505+
{
1506+
s++;
1507+
}
15051508
}
15061509
}
15071510

@@ -1804,22 +1807,22 @@ void CDemandOptimizer::SolveDemandProblem(CModel *pModel, const optStruct &Optio
18041807
IncrementAndSetRowName(pLinProg,rowcount,"reserv_Q_B"+to_string(pSB->GetID()));
18051808

18061809
//------------------------------------------------------------------------
1807-
// Eqns 3 & 4 : Q^n+1 >= -M(1-b)
1810+
// Eqns 3 & 4 : Q^n+1 >= -M(1-b) # THIS EQUATION IS REDUNDANT - ALWAYS TRUE IF Q>0, regardless of B
18081811
// Q^n+1 <= M(1-b)
18091812
// M must be >> Q
18101813
//------------------------------------------------------------------------
18111814

1812-
col_ind[0]=GetDVColumnInd(DV_QOUTRES,_aResIndices[p]); row_val[0]=1.0;
1815+
/*col_ind[0] = GetDVColumnInd(DV_QOUTRES, _aResIndices[p]); row_val[0] = 1.0;
18131816
col_ind[1]=GetDVColumnInd(DV_BINRES ,_aResIndices[p]); row_val[1]=-LARGE_NUMBER;
18141817
RHS =-LARGE_NUMBER;
18151818
18161819
retval = lp_lib::add_constraintex(pLinProg,2,row_val,col_ind,ROWTYPE_GE,RHS);
18171820
ExitGracefullyIf(retval==0,"SolveDemandProblem::Error adding stage discharge constraint C",RUNTIME_ERR);
18181821
IncrementAndSetRowName(pLinProg,rowcount,"reserv_Q_C"+to_string(pSB->GetID()));
1819-
1820-
col_ind[0]=GetDVColumnInd(DV_QOUTRES,_aResIndices[p]); row_val[0]=1.0;
1821-
col_ind[1]=GetDVColumnInd(DV_BINRES ,_aResIndices[p]); row_val[1]=+LARGE_NUMBER;
1822-
RHS =+LARGE_NUMBER;
1822+
*/
1823+
col_ind[0]=GetDVColumnInd(DV_QOUTRES,_aResIndices[p]); row_val[0]=1.0/sqrt(LARGE_NUMBER);
1824+
col_ind[1]=GetDVColumnInd(DV_BINRES ,_aResIndices[p]); row_val[1]=+sqrt(LARGE_NUMBER);
1825+
RHS =+sqrt(LARGE_NUMBER);
18231826

18241827
retval = lp_lib::add_constraintex(pLinProg,2,row_val,col_ind,ROWTYPE_LE,RHS);
18251828
ExitGracefullyIf(retval==0,"SolveDemandProblem::Error adding stage discharge constraint D",RUNTIME_ERR);
@@ -1871,8 +1874,8 @@ void CDemandOptimizer::SolveDemandProblem(CModel *pModel, const optStruct &Optio
18711874
IncrementAndSetRowName(pLinProg,rowcount,"reserv_Q_A"+to_string(pSB->GetID()));
18721875
retval = lp_lib::add_constraintex(pLinProg,1,row_val,col_ind,ROWTYPE_LE,RHS);
18731876
IncrementAndSetRowName(pLinProg,rowcount,"reserv_Q_B"+to_string(pSB->GetID()));
1874-
retval = lp_lib::add_constraintex(pLinProg,1,row_val,col_ind,ROWTYPE_LE,RHS);
1875-
IncrementAndSetRowName(pLinProg,rowcount,"reserv_Q_C"+to_string(pSB->GetID()));
1877+
//retval = lp_lib::add_constraintex(pLinProg,1,row_val,col_ind,ROWTYPE_LE,RHS);
1878+
//IncrementAndSetRowName(pLinProg,rowcount,"reserv_Q_C"+to_string(pSB->GetID()));
18761879
retval = lp_lib::add_constraintex(pLinProg,1,row_val,col_ind,ROWTYPE_LE,RHS);
18771880
IncrementAndSetRowName(pLinProg,rowcount,"reserv_Q_D"+to_string(pSB->GetID()));
18781881
retval = lp_lib::add_constraintex(pLinProg,1,row_val,col_ind,ROWTYPE_LE,RHS);
@@ -1969,7 +1972,7 @@ void CDemandOptimizer::SolveDemandProblem(CModel *pModel, const optStruct &Optio
19691972
// ----------------------------------------------------------------
19701973
// ITERATIVELY SOLVE OPTIMIZATION PROBLEM WITH LP_SOLVE
19711974
// ----------------------------------------------------------------
1972-
const int NUM_ITERATIONS=7;
1975+
const int NUM_ITERATIONS=5;
19731976

19741977
int ctyp;
19751978
int nrows =lp_lib::get_Nrows(pLinProg);
@@ -1996,7 +1999,7 @@ void CDemandOptimizer::SolveDemandProblem(CModel *pModel, const optStruct &Optio
19961999
// ----------------------------------------------------------------
19972000
// Solve the LP problem!
19982001
// ----------------------------------------------------------------
1999-
//lp_lib::set_scaling (pLinProg, CURTISREIDSCALE); //May wish to look for scaling improvements
2002+
//lp_lib::set_scaling(pLinProg, SCALE_CURTISREID); //May wish to look for scaling improvements
20002003
//lp_lib::set_scaling(pLinProg, SCALE_CURTISREID+SCALE_EQUILIBRATE+SCALE_INTEGERS);
20012004
//lp_lib::set_scaling(pLinProg, SCALE_GEOMETRIC + SCALE_EQUILIBRATE + SCALE_INTEGERS +SCALE_DYNUPDATE);
20022005
//lp_lib::set_break_numeric_accuracy(pLinProg, 1e-6);
@@ -2021,7 +2024,7 @@ void CDemandOptimizer::SolveDemandProblem(CModel *pModel, const optStruct &Optio
20212024

20222025
// assign decision variables
20232026
// ----------------------------------------------------------------
2024-
lp_lib::get_variables (pLinProg,soln);
2027+
lp_lib::get_variables (pLinProg,soln );
20252028
lp_lib::get_constraints(pLinProg,constr); //constraint matrix * soln
20262029

20272030
for (int i=0;i<_nDecisionVars;i++){
@@ -2032,13 +2035,12 @@ void CDemandOptimizer::SolveDemandProblem(CModel *pModel, const optStruct &Optio
20322035
// ----------------------------------------------------------------
20332036
for (int j=0;j<nrows;j++)
20342037
{
2035-
if (j<_nSolverResiduals){ //tmp debug to avoid memory leak
2036-
_aSolverRowNames [j]=to_string(lp_lib::get_row_name(pLinProg,j+1));
2037-
_aSolverResiduals[j]=constr[j]-lp_lib::get_rh(pLinProg,j+1); // LHS-RHS for each goal/constraint
2038-
ctyp=lp_lib::get_constr_type(pLinProg,j+1);
2039-
if (ctyp==LE){upperswap(_aSolverResiduals[j],0.0); }
2040-
if (ctyp==GE){lowerswap(_aSolverResiduals[j],0.0); _aSolverResiduals[j]*=-1; }
2041-
}
2038+
_aSolverRowNames [j]=to_string(lp_lib::get_row_name(pLinProg,j+1));
2039+
_aSolverResiduals[j]=constr[j]-lp_lib::get_rh(pLinProg,j+1); // LHS-RHS for each goal/constraint
2040+
2041+
ctyp=lp_lib::get_constr_type(pLinProg,j+1);
2042+
if (ctyp==LE){upperswap(_aSolverResiduals[j],0.0); }
2043+
if (ctyp==GE){lowerswap(_aSolverResiduals[j],0.0); _aSolverResiduals[j]*=-1; }
20422044
}
20432045

20442046
// grab and store improved estimate of reservoir stage + flow diversion
@@ -2259,7 +2261,6 @@ void CDemandOptimizer::WriteOutputFileHeaders(const optStruct &Options)
22592261
}
22602262
_GOALSAT<<endl;
22612263

2262-
22632264
}
22642265
//////////////////////////////////////////////////////////////////
22652266
/// \brief Writes minor output to file at the end of each timestep (or multiple thereof)
@@ -2374,7 +2375,7 @@ void CDemandOptimizer::WriteMinorOutput(const optStruct &Options,const time_stru
23742375
//
23752376
void CDemandOptimizer::CloseOutputStreams()
23762377
{
2377-
if (_MANOPT.is_open()){_MANOPT.close();}
2378+
if ( _MANOPT.is_open()){ _MANOPT.close();}
23782379
if ( _GOALSAT.is_open()){ _GOALSAT.close();}
23792380
if ( _RESID.is_open()){ _RESID.close();}
23802381
}
@@ -2393,7 +2394,7 @@ void CDemandOptimizer::WriteLPSubMatrix(lp_lib::lprec* pLinProg,string file, con
23932394
ExitGracefully(("CConstituentModel::WriteOutputFileHeaders: Unable to open output file "+filename+" for writing.").c_str(),FILE_OPEN_ERR);
23942395
}
23952396
int nrows,ncols;
2396-
nrows=lp_lib::get_Nrows(pLinProg);
2397+
nrows=lp_lib::get_Nrows (pLinProg);
23972398
ncols=lp_lib::get_Ncolumns(pLinProg);
23982399

23992400
LPMAT<<"rowname,";

src/ParseHRUFile.cpp

Lines changed: 0 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1177,7 +1177,6 @@ CReservoir *ReservoirParse(CParser *p,string name,const CModel *pModel,long long
11771177
:Reservoir ExampleReservoir
11781178
:SubBasinID 23
11791179
:HRUID 234
1180-
:Type RESROUTE_STANDARD
11811180
:VolumeStageRelation POWER_LAW
11821181
0.1 2.3
11831182
:EndVolumeStageRelation
@@ -1192,7 +1191,6 @@ CReservoir *ReservoirParse(CParser *p,string name,const CModel *pModel,long long
11921191
:Reservoir ExampleReservoir
11931192
:SubBasinID 23
11941193
:HRUID 234
1195-
:Type RESROUTE_STANDARD
11961194
:StageRelations
11971195
21 # number of points
11981196
0.09 0 0 0.0 (h [m], Q [m3/s], V [m3], A [m2])
@@ -1206,7 +1204,6 @@ CReservoir *ReservoirParse(CParser *p,string name,const CModel *pModel,long long
12061204
:Reservoir ExampleReservoir
12071205
:SubBasinID 23
12081206
:HRUID 234
1209-
:Type RESROUTE_TIMEVARYING
12101207
:VaryingStageRelations
12111208
21 # number of points
12121209
[jul day1] [jul day2] [jul day3] ...
@@ -1221,7 +1218,6 @@ CReservoir *ReservoirParse(CParser *p,string name,const CModel *pModel,long long
12211218
:Reservoir ExampleReservoir # 'lake type format'
12221219
:SubBasinID 23
12231220
:HRUID 234
1224-
:Type RESROUTE_STANDARD
12251221
:WeirCoefficient 0.6
12261222
:CrestWidth 10
12271223
:MaxDepth 5.5 # relative to minimum weir crest elevation
@@ -1232,7 +1228,6 @@ CReservoir *ReservoirParse(CParser *p,string name,const CModel *pModel,long long
12321228
:Reservoir ExampleReservoir # 'multiple control structure format'
12331229
:SubBasinID [ID]
12341230
:HRUID [ID]
1235-
:Type RESROUTE_STANDARD
12361231
:StageRelations
12371232
# these provide the base stage-storage-area-discharge curves for reservoir.
12381233
# The 'main outflow' is still controlled as before and can only drain to the downstream basin.

src/Solvers.cpp

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -490,7 +490,7 @@ void MassEnergyBalance( CModel *pModel,
490490
pModel->GetSubBasin(p)->GetReservoir()->SetPrecip(SWvol);//[SW is treated as precip on reservoir]
491491
}
492492
else{
493-
aRouted[p]+=SWvol; //surface water moved instantaneously from HRU to basin reach/channel storage
493+
aRouted[p]+=SWvol; //surface water moved from HRU to in-catchment routing
494494
}
495495
aPhinew[k][iRO]=aPhinew[k][iSW]; //track net runoff [mm]
496496
aPhinew[k][iSW]=0.0; //zero out surface water storage

0 commit comments

Comments
 (0)