Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
144 changes: 72 additions & 72 deletions src/DemandExpressionHandling.cpp

Large diffs are not rendered by default.

340 changes: 170 additions & 170 deletions src/DemandOptimization.cpp

Large diffs are not rendered by default.

144 changes: 72 additions & 72 deletions src/DemandOptimization.h
Original file line number Diff line number Diff line change
Expand Up @@ -15,12 +15,12 @@
namespace lp_lib {
#include "../lib/lp_solve/lp_lib.h"
}
#endif
#endif

///////////////////////////////////////////////////////////////////
/// \brief Data abstraction for general lookup table y(x)
/// \brief Data abstraction for general lookup table y(x)
//
class CLookupTable
class CLookupTable
{
private:
string _name;
Expand All @@ -38,79 +38,79 @@ class CLookupTable
};

///////////////////////////////////////////////////////////////////
/// \brief different expression types
/// \brief different expression types
//
enum exptype
enum exptype
{
EXP, //< default expression
EXP_OP, //< operator
EXP_INV //< inverse operator
EXP_INV //< inverse operator
};
///////////////////////////////////////////////////////////////////
/// \brief different expression term types
/// \brief different expression term types
//
enum termtype
{
TERM_DV, //< decision variable
TERM_TS, //< time series @ts(name,n)
TERM_TS, //< time series @ts(name,n)
TERM_LT, //< lookup table @lookup(x)
TERM_CONST, //< constant
TERM_HISTORY, //< bracketed - !Q123[-2]
TERM_MAX, //< @max(x,y)
TERM_CONST, //< constant
TERM_HISTORY, //< bracketed - !Q123[-2]
TERM_MAX, //< @max(x,y)
TERM_MIN, //< @min(x,y)
TERM_CONVERT, //< @convert(x,units)
TERM_UNKNOWN //< unknown
TERM_UNKNOWN //< unknown
};
///////////////////////////////////////////////////////////////////
/// \brief decision variable types
/// \brief decision variable types
//
enum dv_type
enum dv_type
{
DV_QOUT, //< outflow from reach
DV_QOUTRES, //< outflow from reservoir
DV_QOUTRES, //< outflow from reservoir
DV_STAGE, //< reservoir stage
DV_DELIVERY,//< delivery of water demand
DV_DELIVERY,//< delivery of water demand
DV_SLACK, //< slack variable for goal satisfaction
DV_USER //< user specified decision variable
DV_USER //< user specified decision variable
};
// data structures used by CDemandOptimizer class
// -------------------------------------------------------------------

//////////////////////////////////////////////////////////////////
/// expression term
/// expression term
/// individual term in expression
//
struct expressionTerm
//
struct expressionTerm
{
termtype type; //< type of expression
termtype type; //< type of expression
double mult; //< multiplier of expression (+/- 1, depending upon operator and location in exp)
bool reciprocal; //< true if term is in denominator
bool reciprocal; //< true if term is in denominator

double value; //< constant value or conversion multiplier
CTimeSeries *pTS; //< pointer to time series, if this is a named time series
CLookupTable *pLT; //< pointer to lookup table, if this is a named time series
bool is_nested; //< true if nested within (i.e., an argument to) another term
int timeshift; //< for time series (+ or -) or lookback value (+)
int DV_ind; //< index of decision variable
bool is_nested; //< true if nested within (i.e., an argument to) another term
int timeshift; //< for time series (+ or -) or lookback value (+)
int DV_ind; //< index of decision variable
int nested_ind1; //< index k of first argument (e.g., for lookup table with term entry)
int nested_ind2; //< index k of second argument (e.g., for min/max functions)
string nested_exp1; //< contents of first argument to function - can be expression
string nested_exp2; //< contents of second argument to function

string origexp; //< original string expression

expressionTerm();
string origexp; //< original string expression

expressionTerm();
};

//////////////////////////////////////////////////////////////////
/// expression structure
/// abstraction of (A*B*C)+(D*E)+(-F)+(G*H) <= 0
/// abstraction of (A*B*C)+(D*E)+(-F)+(G*H) <= 0
/// parenthetical collections are groups of terms -example has 4 groups with [3,2,1,2] Terms per group
//
struct expressionStruct //full expression
{
struct expressionStruct //full expression
{
expressionTerm ***pTerms; //< 2D irregular array of pointers to expression terms size:[nGroups][nExpPerGrp[j]]
int nGroups; //< total number of terms groups in expression
int nGroups; //< total number of terms groups in expression
int *nTermsPerGrp;//< number of terms per group [size: nGroups]
comparison compare; //< comparison operator (==, <, >)

Expand All @@ -120,34 +120,34 @@ struct expressionStruct //full expression


//////////////////////////////////////////////////////////////////
// decision variable
// decision variable
//
struct decision_var
{
string name; //< decision variable names: Qxxxx or Dxxxx where xxxx is SBID
string name; //< decision variable names: Qxxxx or Dxxxx where xxxx is SBID
dv_type dvar_type; //< decision variable type: e.g., DV_QOUT or DV_DELIVERY
int p_index; //< raw subbasin index p (not SBID) of decision variable (or DOESNT_EXIST if not linked to SB)
int p_index; //< raw subbasin index p (not SBID) of decision variable (or DOESNT_EXIST if not linked to SB)
int dem_index; //< demand index in subbasin p (or DOESNT_EXIST if type not DV_DELIVERY) (i..nDemands in SB p_index, usually <=1)
int loc_index; //< local index (rescount or subbasincount or demand count)
int loc_index; //< local index (rescount or subbasincount or demand count)
double value; //< solution value for decision variable
double min; //< minimum bound (default=0)
double min; //< minimum bound (default=0)
double max; //< maximum bound (default unbounded)

decision_var(string nam, int p, dv_type typ, int loc_ind)
{
name=nam;
p_index=p;
loc_index=loc_ind;
name=nam;
p_index=p;
loc_index=loc_ind;
value=0.0;min=0.0;max=ALMOST_INF;dvar_type=typ;dem_index=DOESNT_EXIST;
}
};
//////////////////////////////////////////////////////////////////
// decision variable constraint condition
// decision variable constraint condition
//
struct dv_condition
{
string dv_name; //< decision variable name (e.g., Q1023) or "MONTH"
double value; //< conditional value
double value; //< conditional value
double value2; //< second conditional (if COMPARE_BETWEEN)
comparison compare; //> comparison operator, e.g., COMPARE_IS_EQUAL

Expand All @@ -163,28 +163,28 @@ struct dv_condition
//
struct dv_constraint
{
string name; //< constraint name
expressionStruct *pExpression; //< constraint expression
string name; //< constraint name
expressionStruct *pExpression; //< constraint expression

bool is_goal; //< true if constraint is soft (goal rather than constraint)
int priority; //< priority (default==1, for goals only)
double penalty_under; //< penalty if under specified value (for goals only)
double penalty_over; //< penalty if over value (for goals only)
int slack_ind1; //< slack index (0.._nSlackVars) of under/over slack index for goal, or DOESNT_EXIST if constraint
int slack_ind2; //< slack index (0.._nSlackVars) of over slack index for target goal, or DOESNT_EXIST if constraint

double penalty_value; //< (from solution) penalty incurred by not satisfying goal (or zero for constraint)
double penalty_value; //< (from solution) penalty incurred by not satisfying goal (or zero for constraint)

int nConditions; //< number of conditional statments
dv_condition **pConditions; //< array of pointers to conditional statements
bool conditions_satisfied;
int nConditions; //< number of conditional statments
dv_condition **pConditions; //< array of pointers to conditional statements
bool conditions_satisfied;

dv_constraint();
~dv_constraint();
void AddCondition(dv_condition *pCondition);
};
///////////////////////////////////////////////////////////////////
/// \brief Data abstraction for demand optimization
/// \brief Data abstraction for demand optimization
//
class CDemandOptimizer
{
Expand All @@ -195,37 +195,37 @@ class CDemandOptimizer
int _nDecisionVars; //< total number of decision variables considered
decision_var **_pDecisionVars; //< array of pointers to decision variable structures [size:_nDecisionVars]

int _nConstraints; //< number of user-defined enforced constraints/goals in management model
int _nConstraints; //< number of user-defined enforced constraints/goals in management model
dv_constraint **_pConstraints; //< array of pointers to user-defined enforced constraints/goals in management model

int _nEnabledSubBasins; //< number of enabled subbasins in model
int _nEnabledSubBasins; //< number of enabled subbasins in model
int *_aSBIndices; //< local index of enabled subbasins (0:_nEnabledSubBasins) [size: _nSubBasins]

int _nReservoirs; //< local storage of number of simulated lakes/reservoirs
int *_aResIndices; //< storage of enabled reservoir indices (0:_nReservoirs or DOESNT_EXIST) [size:_nSubBasins]
int _nReservoirs; //< local storage of number of simulated lakes/reservoirs
int *_aResIndices; //< storage of enabled reservoir indices (0:_nReservoirs or DOESNT_EXIST) [size:_nSubBasins]

int _nDemands; //< local storage of number of demand locations (:IrrigationDemand/:WaterDemand + :ReservoirExtraction time series)
int *_aDemandIDs; //< local storage of demand IDs [size:_nDemands]
string *_aDemandAliases; //< list of demand aliases [size: _nDemands]
string *_aDemandAliases; //< list of demand aliases [size: _nDemands]
double *_aDemandPenalties; //< array of priority weights [size: _nDemands]
double *_aDelivery; //< array of delivered water for each demand [m3/s] [size: _nDemands]
double *_aCumDelivery; //< array of cumulative deliveries of demand since _aCumDivDate of this year [m3] [size: _nDemands]
double *_aCumDelivery; //< array of cumulative deliveries of demand since _aCumDivDate of this year [m3] [size: _nDemands]
int *_aCumDelDate; //< julian date to calculate cumulative deliveries from {default: Jan 1)[size: _nDemands]
//int _nDemandGroups; //< number of demand groups
//CDemandGroup **_pDemandGroups; //< array of pointers to demand groups

//int _nDemandGroups; //< number of demand groups
//CDemandGroup **_pDemandGroups; //< array of pointers to demand groups

double *_aSlackValues; //< array of slack variable values [size: _nSlackVars]
int _nSlackVars; //< number of slack variables
int _nSlackVars; //< number of slack variables

int _nUserDecisionVars; //< number of user-specified decision variables
int _nUserDecisionVars; //< number of user-specified decision variables
int _nUserConstants; //< number of user-specified named constants
string *_aUserConstNames; //< array of names of user-specified constants
double *_aUserConstants; //< array of values of user-specified constants
int _nUserTimeSeries; //< number of user variable time series
CTimeSeries **_pUserTimeSeries; //< array of pointers to user variable time series
int _nUserLookupTables; //< number of user variable lookup tables
CLookupTable **_pUserLookupTables; //< array of pointers to user variable lookup tables
int _nUserLookupTables; //< number of user variable lookup tables
CLookupTable **_pUserLookupTables; //< array of pointers to user variable lookup tables

int _nHistoryItems; //< size of flow/demand/stage history that needs to be stored (in time steps) (from :LookbackDuration)
double **_aQhist; //< history of subbasin discharge [size: nSBs * _nHistoryItems]
Expand All @@ -240,10 +240,10 @@ class CDemandOptimizer

#ifdef _LPSOLVE_
void AddConstraintToLP(const int i, lp_lib::lprec *pLinProg, const time_struct &tt,int *col_ind, double *row_val) const;
#endif
#endif
double EvaluateTerm(expressionTerm **pTerms,const int k, const int nn) const;

void CheckConditions(const int ii, const time_struct &tt);
void CheckConditions(const int ii, const time_struct &tt);

bool UserTimeSeriesExists(string TSname) const;
void AddReservoirConstraints();
Expand All @@ -253,20 +253,20 @@ class CDemandOptimizer
~CDemandOptimizer();

int GetDemandIndexFromName(const string dname) const;
double GetNamedConstant (const string s) const;
double GetNamedConstant (const string s) const;
double GetTotalWaterDemandDelivery(const int p) const;
int GetNumUserDVs () const;

void SetHistoryLength (const int n);
void SetCumulativeDate (const int julian_date, const string demandID);

void AddDecisionVar (const decision_var *pDV);
void SetDecisionVarBounds (const string name, const double &min, const double &max);
dv_constraint *AddConstraint (const string name, expressionStruct *exp, const bool soft_constraint);
void AddUserConstant (const string name, const double &val);
void AddUserTimeSeries (const CTimeSeries *pTS);
void AddUserLookupTable (const CLookupTable *pLUT);

//void MultiplyDemand (const string dname, const double &mult);
//void MultiplyGroupDemand (const string groupname, const double &mult);
void SetDemandPenalty (const string dname, const double &pen);
Expand All @@ -275,11 +275,11 @@ class CDemandOptimizer
expressionStruct *ParseExpression(const char **s, const int Len, const int lineno, const string filename) const;

void Initialize (CModel *pModel, const optStruct &Options);
void InitializePostRVMRead (CModel *pModel, const optStruct &Options);
void InitializePostRVMRead (CModel *pModel, const optStruct &Options);
void SolveDemandProblem (CModel *pModel, const optStruct &Options, const double *aSBrunoff, const time_struct &tt);

void WriteOutputFileHeaders(const optStruct &Options);
void WriteMinorOutput (const optStruct &Options,const time_struct &tt);
void CloseOutputStreams ();
};
#endif
#endif
4 changes: 2 additions & 2 deletions src/ForcingGrid.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -165,14 +165,14 @@ CForcingGrid::CForcingGrid( const CForcingGrid &grid )
_CellIDToIdx = new int [_nCells];
ExitGracefullyIf(_CellIDToIdx==NULL,"CForcingGrid::Copy Constructor(8)",OUT_OF_MEMORY);
for(int c=0; c<_nCells; c++) { // loop over non-zero weighted cells
_CellIDToIdx[c]=grid._CellIDToIdx[c];
_CellIDToIdx[c]=grid._CellIDToIdx[c];
}

_IdxNonZeroGridCells = NULL;
_IdxNonZeroGridCells = new int [_nNonZeroWeightedGridCells];
ExitGracefullyIf(_IdxNonZeroGridCells==NULL,"CForcingGrid::Copy Constructor(3)",OUT_OF_MEMORY);
for (int ic=0; ic<_nNonZeroWeightedGridCells; ic++) { // loop over non-zero weighted cells
_IdxNonZeroGridCells[ic]=grid._IdxNonZeroGridCells[ic];
_IdxNonZeroGridCells[ic]=grid._IdxNonZeroGridCells[ic];
}

_aLatitude=NULL;_aLongitude=NULL;_aElevation=NULL;_aStationIDs=NULL;
Expand Down
2 changes: 1 addition & 1 deletion src/Model.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -1615,7 +1615,7 @@ void CModel::SetEnsembleMode(CEnsemble *pEnsemble) {
_pEnsemble=pEnsemble;
}
//////////////////////////////////////////////////////////////////
/// \brief Sets demand optimizer
/// \brief Sets demand optimizer
///
/// \param *pDO [in] (assumed valid) pointer to optimizer instance
//
Expand Down
2 changes: 1 addition & 1 deletion src/Model.h
Original file line number Diff line number Diff line change
Expand Up @@ -111,7 +111,7 @@ class CModel: public CModelABC
CGroundwaterModel *_pGWModel; ///< pointer to corresponding groundwater model
CTransportModel *_pTransModel; ///< pointer to corresponding transport model
CEnsemble *_pEnsemble; ///< pointer to model ensemble
CDemandOptimizer *_pDO; ///< pointer to demand optimizer (NULL w/o optimization)
CDemandOptimizer *_pDO; ///< pointer to demand optimizer (NULL w/o optimization)

//For Diagnostics Calculation
CTimeSeriesABC **_pObservedTS; ///< array of pointers of observation time series [size: _nObservedTS]
Expand Down
2 changes: 1 addition & 1 deletion src/ParseHRUFile.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -830,7 +830,7 @@ bool ParseHRUPropsFile(CModel *&pModel, const optStruct &Options, bool terrain_r
int iter=0;
for(int p=0;p<pModel->GetNumSubBasins();p++)
{
if ((pModel->IsSubBasinUpstream(pModel->GetSubBasin(p)->GetID(),SBID)) ||
if ((pModel->IsSubBasinUpstream(pModel->GetSubBasin(p)->GetID(),SBID)) ||
(pModel->GetSubBasin(p)->GetID() == SBID) ) {
pSBGroup->AddSubbasin(pModel->GetSubBasin(p));
advice=advice+to_string(pModel->GetSubBasin(p)->GetID())+" ";
Expand Down
2 changes: 1 addition & 1 deletion src/ParseLib.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -54,7 +54,7 @@ string CParser::Peek()
string firstword = "";
if (Len > 0) {firstword=s[0];}
if (!eof){
INPUT->seekg(place ,std::ios_base::beg); // Return to position before peeked line
INPUT->seekg(place ,std::ios_base::beg); // Return to position before peeked line
}
return firstword;
}
Expand Down
Loading