Esempio n. 1
0
    def solve_serial_ph(self, subsolver, default_rho, phopts=None, sopts=None):
        # Solve the stochastic program given by this.scenario_tree using ph
        # subsolver: the solver to call (e.g., 'ipopt')
        # phopts: dictionary ph options
        # sopts: dictionary of subsolver options
        # Returns

        ph = None
        parser = phinit.construct_ph_options_parser("")
        options = parser.parse_args(['--default-rho', str(default_rho)])
        ###!!!! tbd get options from argument !!!!! and delete next line
        ###try:

        ###scenario_tree = \
        ###phinit.GenerateScenarioTreeForPH(options,
        ### scenario_instance_factory)

        ph = phinit.PHAlgorithmBuilder(options, self.scenario_tree)

        ###except:
        ###    print ("Internal error: ph construction failed."
        ###    if ph is not None:
        ###        ph.release_components()
        ###    raise

        retval = ph.solve()
        if retval is not None:
            raise RuntimeError("ph Failure Encountered=" + str(retval))
        print(
            "foobar of victory: HEY: get a solution writer; e.g., from phinit.py and/or return something"
        )
Esempio n. 2
0
    def solve_ph(self, subsolver, default_rho, phopts=None, sopts=None):
        """Solve the stochastic program given by this.scenario_tree using ph

        Args:
            subsolver (str): the solver to call (e.g., 'ipopt')
            default_rho (float): the rho value to use by default
            phopts: dictionary of ph options (optional)
            sopts: dictionary of subsolver options (optional)

        Returns: the ph object

        Note:
            Updates the scenario tree, populated with the xbar values; 
            however, you probably want to do
            obj, xhat = ph.compute_and_report_inner_bound_using_xhat()
            where ph is the return value.

        """

        ph = None

        # Build up the options for PH.
        parser = phinit.construct_ph_options_parser("")
        phargslist = ['--default-rho', str(default_rho)]
        phargslist.append('--solver')
        phargslist.append(str(subsolver))
        if phopts is not None:
            for key in phopts:
                phargslist.append(key)
                if phopts[key] is not None:
                    phargslist.append(phopts[key])

        # Subproblem options go to PH as space-delimited, equals-separated pairs.
        if sopts is not None:
            soptstring = ""
            for key in sopts:
                soptstring += key + '=' + str(sopts[key]) + ' '
            phargslist.append('--scenario-solver-options')
            phargslist.append(soptstring)
        phoptions = parser.parse_args(phargslist)

        # construct the PH solver object
        try:
            ph = phinit.PHAlgorithmBuilder(phoptions, self.scenario_tree)
        except:
            print("Internal error: ph construction failed.")
            if ph is not None:
                ph.release_components()
            raise

        retval = ph.solve()
        if retval is not None:
            raise RuntimeError("ph Failure Encountered=" + str(retval))
        # dlw May 2017: I am not sure if the next line is really needed
        ph.save_solution()

        return ph