def test_xhat_eval_evaluate(self):
        options = self._get_xhatEval_options()
        MMW_options = self._get_base_options()
        scenario_creator_kwargs = MMW_options['kwargs']
        scenario_creator_kwargs['num_scens'] = MMW_options['batch_size']
        ev = Xhat_Eval(options,
                       farmer.scenario_names_creator(100),
                       farmer.scenario_creator,
                       scenario_denouement=None,
                       scenario_creator_kwargs=scenario_creator_kwargs)

        xhat = ciutils.read_xhat(self.xhat_path)
        obj = round_pos_sig(ev.evaluate(xhat), 2)
        self.assertEqual(obj, -1300000.0)
 def test_xhat_eval_evaluate_one(self):
     options = self._get_xhatEval_options()
     MMW_options = self._get_base_options()
     xhat = ciutils.read_xhat(self.xhat_path)
     scenario_creator_kwargs = MMW_options['kwargs']
     scenario_creator_kwargs['num_scens'] = MMW_options['batch_size']
     scenario_names = farmer.scenario_names_creator(100)
     ev = Xhat_Eval(options,
                    scenario_names,
                    farmer.scenario_creator,
                    scenario_denouement=None,
                    scenario_creator_kwargs=scenario_creator_kwargs)
     k = scenario_names[0]
     obj = ev.evaluate_one(xhat, k, ev.local_scenarios[k])
     obj = round_pos_sig(obj, 2)
     self.assertEqual(obj, -48000.0)
    def test_xhat_eval_creator(self):
        options = self._get_xhatEval_options()

        MMW_options = self._get_base_options()
        scenario_creator_kwargs = MMW_options['kwargs']
        scenario_creator_kwargs['num_scens'] = MMW_options['batch_size']
        ev = Xhat_Eval(options,
                       farmer.scenario_names_creator(100),
                       farmer.scenario_creator,
                       scenario_denouement=None,
                       scenario_creator_kwargs=scenario_creator_kwargs)
 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
Beispiel #5
0
def evaluate_sample_trees(xhat_one,
                          num_samples,
                          ama_options,
                          InitSeed=0,
                          model_module=None):
    """ Create and evaluate multiple sampled trees.
    Args:
        xhat_one : list or np.array of float (*not* a dict)
            A feasible and nonanticipative first stage solution.
        num_samples (int): number of trees to sample
        ama_options (dict): options for the amalgamator
        InitSeed (int): starting seed (but might be used for a scenario name offset)
        model_modules: an imported module with the functions needed by, e.g., amalgamator
    Returns:
        zhats (list as np.array): the objective functions
        seed (int): the updated seed or offset for scenario name sampling        
    """
    ''' creates batch_size sample trees with first-stage solution xhat_one
    using SampleSubtree class from sample_tree
    used to approximate E_{xi_2} phi(x_1, xi_2) for confidence interval coverage experiments
    note: ama_options will include the command line args as 'args'
    '''
    mname = ama_options["args"].model_module_name
    seed = InitSeed
    zhats = list()
    bfs = ama_options["branching_factors"]
    scenario_count = np.prod(bfs)
    solvername = ama_options["EF_solver_name"]
    #sampling_bfs = ciutils.scalable_BFs(batch_size, bfs) # use for variance?
    xhat_eval_options = {
        "iter0_solver_options": None,
        "iterk_solver_options": None,
        "display_timing": False,
        "solvername": solvername,
        "verbose": False,
        "solver_options": {}
    }

    scenario_creator_kwargs = model_module.kw_creator(ama_options)
    for j in range(num_samples):  # number of sample trees to create
        samp_tree = sample_tree.SampleSubtree(mname,
                                              xhats=[],
                                              root_scen=None,
                                              starting_stage=1,
                                              branching_factors=bfs,
                                              seed=seed,
                                              options=scenario_creator_kwargs,
                                              solvername=solvername,
                                              solver_options={})
        samp_tree.run()
        ama_object = samp_tree.ama
        ama_options = ama_object.options
        ama_options['verbose'] = False
        scenario_creator_kwargs = ama_object.kwargs
        if len(samp_tree.ef._ef_scenario_names) > 1:
            local_scenarios = {
                sname: getattr(samp_tree.ef, sname)
                for sname in samp_tree.ef._ef_scenario_names
            }
        else:
            local_scenarios = {
                samp_tree.ef._ef_scenario_names[0]: samp_tree.ef
            }

        xhats, seed = sample_tree.walking_tree_xhats(mname,
                                                     local_scenarios,
                                                     xhat_one,
                                                     bfs,
                                                     seed,
                                                     ama_options,
                                                     solvername=solvername,
                                                     solver_options=None)
        # for Xhat_Eval
        # scenario_creator_kwargs = ama_object.kwargs
        scenario_names = ama_object.scenario_names
        all_nodenames = sputils.create_nodenames_from_branching_factors(bfs)

        # Evaluate objective value of feasible policy for this tree
        ev = Xhat_Eval(xhat_eval_options,
                       scenario_names,
                       ama_object.scenario_creator,
                       model_module.scenario_denouement,
                       scenario_creator_kwargs=scenario_creator_kwargs,
                       all_nodenames=all_nodenames)

        zhats.append(ev.evaluate(xhats))

        seed += scenario_count

    return np.array(zhats), seed