Example #1
0
def make_node_scenario_dict_balanced(BFs, leaf_nodes=True, start=1):
    """ creates a node-scenario dictionary for aircond problem
    Args: 
        BFs (list of ints): branching factors for each stage 
        leaf_nodes (bool): include/exclude leaf nodes (default True)
        start (int): starting value for scenario number (default 1)

    Returns:
        node_scenario_dict (dict): dictionary of node-scenario key-value pairs

    """
    nodenames = make_nodenames_balanced(BFs, leaf_nodes)
    num_scens = np.prod(BFs)
    scenario_names = aircond.scenario_names_creator(num_scens, start)

    num_stages = len(BFs) + 1
    node_scenario_dict = {"ROOT": scenario_names[0]}

    for i in range(BFs[0]):
        node_scenario_dict[nodenames[i]] = scenario_names[i]
        if num_stages >= 3:
            for stage in range(2, num_stages + leaf_nodes):
                node_scenario_dict = _recursive_node_dict(
                    node_scenario_dict, nodenames, scenario_names, BFs, stage)

    return node_scenario_dict
Example #2
0
def main():
    solution_files = {"first_stage_solution":"aircond_first_stage.csv",
                      "tree_solution":"aircond_full_solution" 
                      }
    options = {"mstage": True,   # 2stage vs. mstage
                   "cylinders": ['ph','xhatshuffle','lagrangian'],
                   "write_solution": solution_files
                   }
    ama_options = amalgamator.Amalgamator_parser(options, aircond.inparser_adder)
    
    #Here, we need to change something specified by the command line
    #That's why we cannot use amalgamator.from_module
    if ama_options['num_scens'] is not None:
        raise RuntimeError("Do not use num_scens here, we want to solve the problem for the whole sample scenario tree")
    
    num_scens = np.prod(ama_options['branching_factors'])
    scenario_names = aircond.scenario_names_creator(num_scens,0)
    
    ama =amalgamator.Amalgamator(ama_options, 
                                 scenario_names, 
                                 aircond.scenario_creator, 
                                 aircond.kw_creator)
    ama.run()
    if ama.on_hub:
        print("first_stage_solution=", ama.first_stage_solution)
        print("inner bound=", ama.best_inner_bound)
        print("outer bound=", ama.best_outer_bound)
 def _get_xhat_gen_options(self, BFs):
     num_scens = np.prod(BFs)
     scenario_names = aircond.scenario_names_creator(num_scens)
     xhat_gen_options = {"scenario_names": scenario_names,
                         "solvername": solvername,
                         "solver_options": None,
                         "branching_factors": BFs,
                         "mudev": 0,
                         "sigmadev": 40,
                         "start_ups": False,
                         "start_seed": 0,
                         }
     return xhat_gen_options
    def test_xhat_eval_evaluate_one(self):
        ev = self._eval_creator()
        options = self._get_base_options()
        branching_factors= options['kwoptions']['branching_factors']
        full_xhat = self._make_full_xhat(branching_factors)

        num_scens = np.prod(branching_factors)
        scenario_creator_kwargs = options['kwargs']
        scenario_creator_kwargs['num_scens'] = num_scens
        all_scenario_names = aircond.scenario_names_creator(num_scens)

        k = all_scenario_names[0]
        obj = ev.evaluate_one(full_xhat,k,ev.local_scenarios[k])
        obj = round_pos_sig(obj,2)
        self.assertEqual(obj, 990.0) # rebaselined 28 Dec 2021
 def _eval_creator(self):
     options = self._get_xhatEval_options()
     
     MMW_options = self._get_base_options()
     branching_factors= MMW_options['kwoptions']['branching_factors']
     scen_count = np.prod(branching_factors)
     scenario_creator_kwargs = MMW_options['kwargs']
     ###scenario_creator_kwargs['num_scens'] = MMW_options['batch_size']
     all_scenario_names = aircond.scenario_names_creator(scen_count)
     all_nodenames = sputils.create_nodenames_from_branching_factors(branching_factors)
     ev = Xhat_Eval(options,
                    all_scenario_names,
                    aircond.scenario_creator,
                    scenario_denouement=None,
                    all_nodenames=all_nodenames,
                    scenario_creator_kwargs=scenario_creator_kwargs
                    )
     return ev
 def test_gap_estimators(self):
     options = self._get_base_options()
     branching_factors= options['kwoptions']['branching_factors']
     scen_count = np.prod(branching_factors)
     scenario_names = aircond.scenario_names_creator(scen_count, start=1000)
     sample_options = {"seed": 0,
                       "branching_factors": options['kwoptions']['branching_factors']}
     estim = ciutils.gap_estimators(self.xhat,
                                    self.refmodelname,
                                    solving_type="EF-mstage",
                                    sample_options=sample_options,
                                    scenario_creator_kwargs = options['kwargs'],
                                    solvername=solvername,
                                    scenario_names=scenario_names,
                                    )
     G = estim['G']
     s = estim['s']
     G,s = round_pos_sig(G,3),round_pos_sig(s,3)
     self.assertEqual((G,s), (64.5, 31.8))  # rebaselined 28 Dec 2021 (see also 5 dec)