bernardoct / WaterPaths

WaterPaths - A utility planning and management tool based off the NC Triangle model.
Apache License 2.0
18 stars 8 forks source link

InfrastructureManager::rearrangeInfraRofVector bug with both ROF and demand-triggered infrastructure #34

Open degorelick opened 4 years ago

degorelick commented 4 years ago

In the case where a utility has (a) projects triggered by both ROF and demand and (b) the number of ROF-triggered projects differs than the number of demand-triggered projects, this routine will always throw an error.

Suggested solution: the function argument infra_construction_triggers is passed from the Utility constructor. If ROF project triggers are always placed first in this vector when a utility object is created in the Problem class, followed by demand triggers, the process to build a standard infra_construction_triggers_new within the rearrangeInfraRofVector function can be changed, replacing lines 146-161 of InfrastructureManager.cpp of the master to:

vector<double> infra_construction_triggers_new((unsigned long) size, 1e10);
for (unsigned long i = 0; i < infra_construction_triggers.size(); ++i) {
    // first, check ROF triggers
    if (i < rof_infra_construction_order.size()) {
        auto ws = (unsigned long) rof_infra_construction_order.at(i);
        infra_construction_triggers_new.at(ws) = infra_construction_triggers.at(i);
    } else { // next, follow with demand triggers
        auto ws = (unsigned long) demand_infra_construction_order.at(i-rof_infra_construction_order.size());
        if (infra_construction_triggers_new.at(ws) != 1e10)
            throw invalid_argument("A source can be triggered only by "
                                   "either rof or by demand.");
        infra_construction_triggers_new.at(ws) = infra_construction_triggers.at(i);
    }
}
bernardoct commented 3 years ago

Hi Dave, thanks a lot for submitting the issue. I think that having one vector for each would make the input more organized, don't you think? I believe this would also simplify the code. Would you be able to create a unit test for this and submit a pull request with your bug fix?