def __init__(self, **kwds): """ Constructor """ OptSolver.__init__(self, **kwds) self._assert_available = True self._valid_problem_formats = [ProblemFormat.colin_optproblem] self._valid_result_formats = {} self._valid_result_formats[ProblemFormat.colin_optproblem] = [ ResultsFormat.osrl, ResultsFormat.results ]
def __init__(self, **kwds): # set persistent config options tmp_kwds = {'type': kwds.pop('type', 'trustregion')} self.config = self.CONFIG(kwds, preserve_implicit=True) # # Call base class constructor # tmp_kwds['solver'] = self.config.solver OptSolver.__init__(self, **tmp_kwds)
def __init__(self, **kwds): # set persistent config options tmp_kwds = {'type':kwds.pop('type','trustregion')} self.config = self.CONFIG(kwds) # # Call base class constructor # tmp_kwds['solver'] = 'ipopt' OptSolver.__init__(self, **tmp_kwds)
def __init__(self, **kwds): # set persistent config options tmp_kwds = {'type':kwds.pop('type','trustregion')} self.config = self.CONFIG(kwds, preserve_implicit=True) # # Call base class constructor # tmp_kwds['solver'] = self.config.solver OptSolver.__init__(self, **tmp_kwds)
def __init__(self, **kwds): # # Call base class constructor # kwds['type'] = 'glpk_direct' OptSolver.__init__(self, **kwds) # NOTE: eventually both of the following attributes should be migrated # to a common base class. Is the current solve warm-started? A # transient data member to communicate state information across the # _presolve, _apply_solver, and _postsolve methods. self.warm_start_solve = False self._timelimit = None # Note: Undefined capabilities default to 'None' self._capabilities = Bunch() self._capabilities.linear = True self._capabilities.integer = True
def _perform_queue(self, ah, *args, **kwds): """ Perform the queue operation. This method returns the ActionHandle, and the ActionHandle status indicates whether the queue was successful. """ solver = kwds.pop('solver', kwds.pop('opt', None)) if solver is None: raise ActionManagerError( "No solver passed to %s, use keyword option 'solver'" % (type(self).__name__)) if not isinstance(solver, six.string_types): solver = solver.name # # Handle ephemeral solvers options here. These # will override whatever is currently in the options # dictionary, but we will reset these options to # their original value at the end of this method. # ephemeral_solver_options = {} ephemeral_solver_options.update(kwds.pop('options', {})) ephemeral_solver_options.update( OptSolver._options_string_to_dict(kwds.pop('options_string', ''))) opt = SolverFactory('_neos') opt._presolve(*args, **kwds) # # Map NEOS name, using lowercase convention in Pyomo # if len(self._solvers) == 0: for name in self.kestrel.solvers(): if name.endswith('AMPL'): self._solvers[name[:-5].lower()] = name[:-5] if not solver in self._solvers: raise ActionManagerError("Solver '%s' is not recognized by NEOS" % solver) # # Apply kestrel # os.environ['kestrel_options'] = 'solver=%s' % self._solvers[solver] solver_options = {} for key in opt.options: solver_options[key] = opt.options[key] solver_options.update(ephemeral_solver_options) options = opt._get_options_string(solver_options) if not options == "": os.environ[self._solvers[solver].lower() + '_options'] = opt._get_options_string() xml = self.kestrel.formXML(opt._problem_files[0]) (jobNumber, password) = self.kestrel.submit(xml) ah.job = jobNumber ah.password = password # # Store action handle, and return # self._ah[jobNumber] = ah self._neos_log[jobNumber] = (0, "") self._opt_data[jobNumber] = (opt, opt._smap_id, opt._load_solutions, opt._select_index, opt._default_variable_value) self._args[jobNumber] = args return ah
def _convert_problem(self, args, problem_format, valid_problem_formats, **kwds): # Baron needs all solver options and file redirections # inside the input file, so we need to input those # here through io_options before calling the baron writer # # Define log file # if self._log_file is None: self._log_file = TempfileManager.\ create_tempfile(suffix = '.baron.log') # # Define solution file # if self._soln_file is None: self._soln_file = TempfileManager.\ create_tempfile(suffix = '.baron.soln') self._tim_file = TempfileManager.\ create_tempfile(suffix = '.baron.tim') # # Create options to send through as io_options # containing all relevent info needed in the Baron file # solver_options = {} solver_options['ResName'] = self._soln_file solver_options['TimName'] = self._tim_file for key in self.options: lower_key = key.lower() if lower_key == 'resname': logger.warning( 'Ignoring user-specified option "%s=%s". This ' 'option is set to %s, and can be overridden using ' 'the "solnfile" argument to the solve() method.' % (key, self.options[key], self._soln_file)) elif lower_key == 'timname': logger.warning( 'Ignoring user-specified option "%s=%s". This ' 'option is set to %s.' % (key, self.options[key], self._tim_file)) else: solver_options[key] = self.options[key] for suffix in self._suffixes: if re.match(suffix, 'dual') or re.match(suffix, 'rc'): solver_options['WantDual'] = 1 break if 'solver_options' in kwds: raise ValueError("Baron solver options should be set " "using the options object on this " "solver plugin. The solver_options " "I/O options dict for the Baron writer " "will be populated by this plugin's " "options object") kwds['solver_options'] = solver_options return OptSolver._convert_problem(self, args, problem_format, valid_problem_formats, **kwds)
def _get_task_data(self, ah, *args, **kwds): opt = kwds.pop('solver', kwds.pop('opt', None)) if opt is None: raise ActionManagerError( "No solver passed to %s, use keyword option 'solver'" % (type(self).__name__)) if isinstance(opt, str): opt = SolverFactory(opt, solver_io=kwds.pop('solver_io', None)) # # The following block of code is taken from the OptSolver.solve() # method, which we do not directly invoke with this interface # # # If the inputs are models, then validate that they have been # constructed! Collect suffix names to try and import from solution. # for arg in args: if isinstance(arg, (Block, IBlock)): if isinstance(arg, Block): if not arg.is_constructed(): raise RuntimeError( "Attempting to solve model=%s with unconstructed " "component(s)" % (arg.name)) # import suffixes must be on the top-level model if isinstance(arg, Block): model_suffixes = list(name for (name,comp) \ in pyomo.core.base.suffix.\ active_import_suffix_generator(arg)) else: assert isinstance(arg, IBlock) model_suffixes = list(comp.storage_key for comp \ in pyomo.core.base.suffix.\ import_suffix_generator(arg, active=True, descend_into=False)) if len(model_suffixes) > 0: kwds_suffixes = kwds.setdefault('suffixes', []) for name in model_suffixes: if name not in kwds_suffixes: kwds_suffixes.append(name) # # Handle ephemeral solvers options here. These # will override whatever is currently in the options # dictionary, but we will reset these options to # their original value at the end of this method. # ephemeral_solver_options = {} ephemeral_solver_options.update(kwds.pop('options', {})) ephemeral_solver_options.update( OptSolver._options_string_to_dict(kwds.pop('options_string', ''))) # # Force pyomo.opt to ignore tests for availability, at least locally. # del_available = bool('available' not in kwds) kwds['available'] = True opt._presolve(*args, **kwds) problem_file_string = None with open(opt._problem_files[0], 'r') as f: problem_file_string = f.read() # # Delete this option, to ensure that the remote worker does the check for # availability. # if del_available: del kwds['available'] # # We can't pickle the options object itself - so extract a simple # dictionary of solver options and re-construct it on the other end. # solver_options = {} for key in opt.options: solver_options[key] = opt.options[key] solver_options.update(ephemeral_solver_options) # # NOTE: let the distributed node deal with the warm-start # pick up the warm-start file, if available. # warm_start_file_string = None warm_start_file_name = None if hasattr(opt, "_warm_start_solve"): if opt._warm_start_solve and \ (opt._warm_start_file_name is not None): warm_start_file_name = opt._warm_start_file_name with open(warm_start_file_name, 'r') as f: warm_start_file_string = f.read() data = Bunch(opt=opt.type, \ file=problem_file_string, \ filename=opt._problem_files[0], \ warmstart_file=warm_start_file_string, \ warmstart_filename=warm_start_file_name, \ kwds=kwds, \ solver_options=solver_options, \ suffixes=opt._suffixes) self._args[ah.id] = args self._opt_data[ah.id] = (opt._smap_id, opt._load_solutions, opt._select_index, opt._default_variable_value) return data
def _perform_queue(self, ah, *args, **kwds): """ Perform the queue operation. This method returns the ActionHandle, and the ActionHandle status indicates whether the queue was successful. """ solver = kwds.pop('solver', kwds.pop('opt', None)) if solver is None: raise ActionManagerError( "No solver passed to %s, use keyword option 'solver'" % (type(self).__name__)) if not isinstance(solver, six.string_types): solver_name = solver.name if solver_name == 'asl': solver_name = \ os.path.basename(solver.executable()) else: solver_name = solver solver = None # # Handle ephemeral solvers options here. These # will override whatever is currently in the options # dictionary, but we will reset these options to # their original value at the end of this method. # user_solver_options = {} # make sure to transfer the options dict on the # solver plugin if the user does not use a string # to identify the neos solver. The ephemeral # options must also go after these. if solver is not None: user_solver_options.update(solver.options) user_solver_options.update(kwds.pop('options', {})) user_solver_options.update( OptSolver._options_string_to_dict(kwds.pop('options_string', ''))) opt = SolverFactory('_neos') opt._presolve(*args, **kwds) # # Map NEOS name, using lowercase convention in Pyomo # if len(self._solvers) == 0: for name in self.kestrel.solvers(): if name.endswith('AMPL'): self._solvers[name[:-5].lower()] = name[:-5] if solver_name not in self._solvers: raise ActionManagerError( "Solver '%s' is not recognized by NEOS. " "Solver names recognized:\n%s" % (solver_name, str(sorted(self._solvers.keys())))) # # Apply kestrel # os.environ[ 'kestrel_options'] = 'solver=%s' % self._solvers[solver_name] solver_options = {} for key in opt.options: solver_options[key] = opt.options[key] solver_options.update(user_solver_options) options = opt._get_options_string(solver_options) # GH: Should we really be modifying the environment # for this manager (knowing that we are not # executing locally) if not options == "": os.environ[self._solvers[solver_name].lower()+'_options'] = \ opt._get_options_string() xml = self.kestrel.formXML(opt._problem_files[0]) (jobNumber, password) = self.kestrel.submit(xml) ah.job = jobNumber ah.password = password # # Store action handle, and return # self._ah[jobNumber] = ah self._neos_log[jobNumber] = (0, "") self._opt_data[jobNumber] = (opt, opt._smap_id, opt._load_solutions, opt._select_index, opt._default_variable_value) self._args[jobNumber] = args return ah
def _perform_queue(self, ah, *args, **kwds): """ Perform the queue operation. This method returns the ActionHandle, and the ActionHandle status indicates whether the queue was successful. """ solver = kwds.pop('solver', kwds.pop('opt', None)) if solver is None: raise ActionManagerError( "No solver passed to %s, use keyword option 'solver'" % (type(self).__name__) ) if not isinstance(solver, six.string_types): solver_name = solver.name if solver_name == 'asl': solver_name = \ os.path.basename(solver.executable()) else: solver_name = solver solver = None # # Handle ephemeral solvers options here. These # will override whatever is currently in the options # dictionary, but we will reset these options to # their original value at the end of this method. # user_solver_options = {} # make sure to transfer the options dict on the # solver plugin if the user does not use a string # to identify the neos solver. The ephemeral # options must also go after these. if solver is not None: user_solver_options.update(solver.options) _options = kwds.pop('options', {}) if isinstance(_options, six.string_types): _options = OptSolver._options_string_to_dict(_options) user_solver_options.update(_options) user_solver_options.update( OptSolver._options_string_to_dict(kwds.pop('options_string', ''))) # JDS: [5/13/17] The following is a HACK. This timeout flag is # set by pyomo/scripting/util.py:apply_optimizer. If we do not # remove it, it will get passed to the NEOS solver. For solvers # like CPLEX 12.7.0, this will cause a fatal error as it is not # a known option. if user_solver_options.get('timelimit',0) is None: del user_solver_options['timelimit'] opt = SolverFactory('_neos') opt._presolve(*args, **kwds) # # Map NEOS name, using lowercase convention in Pyomo # if len(self._solvers) == 0: for name in self.kestrel.solvers(): if name.endswith('AMPL'): self._solvers[ name[:-5].lower() ] = name[:-5] if solver_name not in self._solvers: raise ActionManagerError( "Solver '%s' is not recognized by NEOS. " "Solver names recognized:\n%s" % (solver_name, str(sorted(self._solvers.keys())))) # # Apply kestrel # # Set the kestrel_options environment # neos_sname = self._solvers[solver_name].lower() os.environ['kestrel_options'] = 'solver=%s' % self._solvers[solver_name] # # Set the <solver>_options environment # solver_options = {} for key in opt.options: solver_options[key]=opt.options[key] solver_options.update(user_solver_options) options = opt._get_options_string(solver_options) if not options == "": os.environ[neos_sname+'_options'] = options # # Generate an XML string using these two environment variables # xml = self.kestrel.formXML(opt._problem_files[0]) (jobNumber, password) = self.kestrel.submit(xml) ah.job = jobNumber ah.password = password # # Cleanup # del os.environ['kestrel_options'] try: del os.environ[neos_sname+"_options"] except: pass # # Store action handle, and return # self._ah[jobNumber] = ah self._neos_log[jobNumber] = (0, "") self._opt_data[jobNumber] = (opt, opt._smap_id, opt._load_solutions, opt._select_index, opt._default_variable_value) self._args[jobNumber] = args return ah
def __init__(self, **kwds): # # Call base class constructor # kwds['type'] = 'trustregion' OptSolver.__init__(self, **kwds)
def _postsolve(self): lp = self._glpk_instance num_variables = glp_get_num_cols(lp) bin_variables = glp_get_num_bin(lp) int_variables = glp_get_num_int(lp) # check suffixes for suffix in self._suffixes: if True: raise RuntimeError( "***The glpk_direct solver plugin cannot extract solution suffix=" + suffix) tpeak = glp_long() glp_mem_usage(None, None, None, tpeak) # black magic trickery, thanks to Python's lack of pointers and SWIG's # automatic API conversion peak_mem = tpeak.lo results = SolverResults() soln = Solution() prob = results.problem solv = results.solver solv.name = "GLPK " + glp_version() solv.status = self._glpk_get_solver_status() solv.return_code = self.solve_return_code solv.message = self._glpk_return_code_to_message() solv.algorithm = self.algo solv.memory_used = "%d bytes, (%d KiB)" % (peak_mem, peak_mem / 1024) # solv.user_time = None # solv.system_time = None solv.wallclock_time = self._glpk_solve_time # solv.termination_condition = None # solv.termination_message = None prob.name = glp_get_prob_name(lp) prob.number_of_constraints = glp_get_num_rows(lp) prob.number_of_nonzeros = glp_get_num_nz(lp) prob.number_of_variables = num_variables prob.number_of_binary_variables = bin_variables prob.number_of_integer_variables = int_variables prob.number_of_continuous_variables = num_variables - int_variables prob.number_of_objectives = 1 prob.sense = ProblemSense.minimize if GLP_MAX == glp_get_obj_dir(lp): prob.sense = ProblemSense.maximize soln.status = self._glpk_get_solution_status() if soln.status in (SolutionStatus.optimal, SolutionStatus.feasible): get_col_prim = glp_get_col_prim get_row_prim = glp_get_row_prim get_obj_val = glp_get_obj_val if self.is_integer: get_col_prim = glp_mip_col_val get_row_prim = glp_mip_row_val get_obj_val = glp_mip_obj_val obj_val = get_obj_val(lp) if prob.sense == ProblemSense.minimize: prob.lower_bound = obj_val else: prob.upper_bound = obj_val objective_name = lp.objective_name soln.objective[objective_name] = {'Value': obj_val} colvar_map = self._glpk_colvar_map rowvar_map = self._glpk_rowvar_map for var_label in colvar_map: col = colvar_map[var_label] soln.variable[var_label] = {"Value": get_col_prim(lp, col)} for row_label in rowvar_map: row = rowvar_map[row_label] soln.constraint[row_label] = {"Value": get_row_prim(lp, row)} results.solution.insert(soln) self.results = results # All done with the GLPK object, so free up some memory. glp_free(lp) del self._glpk_instance, lp # let the base class deal with returning results. return OptSolver._postsolve(self)
def _get_task_data(self, ah, *args, **kwds): opt = kwds.pop('solver', kwds.pop('opt', None)) if opt is None: raise ActionManagerError( "No solver passed to %s, use keyword option 'solver'" % (type(self).__name__) ) deactivate_opt = False if isinstance(opt, six.string_types): deactivate_opt = True opt = SolverFactory(opt, solver_io=kwds.pop('solver_io', None)) # # The following block of code is taken from the OptSolver.solve() # method, which we do not directly invoke with this interface # # # If the inputs are models, then validate that they have been # constructed! Collect suffix names to try and import from solution. # for arg in args: if isinstance(arg, Block): if not arg.is_constructed(): raise RuntimeError( "Attempting to solve model=%s with unconstructed " "component(s)" % (arg.name,) ) model_suffixes = list(name for (name,comp) \ in active_import_suffix_generator(arg)) if len(model_suffixes) > 0: kwds_suffixes = kwds.setdefault('suffixes',[]) for name in model_suffixes: if name not in kwds_suffixes: kwds_suffixes.append(name) # # Handle ephemeral solvers options here. These # will override whatever is currently in the options # dictionary, but we will reset these options to # their original value at the end of this method. # ephemeral_solver_options = {} ephemeral_solver_options.update(kwds.pop('options', {})) ephemeral_solver_options.update( OptSolver._options_string_to_dict(kwds.pop('options_string', ''))) # # Force pyomo.opt to ignore tests for availability, at least locally. # del_available = bool('available' not in kwds) kwds['available'] = True opt._presolve(*args, **kwds) problem_file_string = None with open(opt._problem_files[0], 'r') as f: problem_file_string = f.read() # # Delete this option, to ensure that the remote worker does the check for # availability. # if del_available: del kwds['available'] # # We can't pickle the options object itself - so extract a simple # dictionary of solver options and re-construct it on the other end. # solver_options = {} for key in opt.options: solver_options[key]=opt.options[key] solver_options.update(ephemeral_solver_options) # # NOTE: let the distributed node deal with the warm-start # pick up the warm-start file, if available. # warm_start_file_string = None warm_start_file_name = None if hasattr(opt, "_warm_start_solve"): if opt._warm_start_solve and \ (opt._warm_start_file_name is not None): warm_start_file_name = opt._warm_start_file_name with open(warm_start_file_name, 'r') as f: warm_start_file_string = f.read() data = pyutilib.misc.Bunch(opt=opt.type, \ file=problem_file_string, \ filename=opt._problem_files[0], \ warmstart_file=warm_start_file_string, \ warmstart_filename=warm_start_file_name, \ kwds=kwds, \ solver_options=solver_options, \ suffixes=opt._suffixes) self._args[ah.id] = args self._opt_data[ah.id] = (opt._smap_id, opt._load_solutions, opt._select_index, opt._default_variable_value) if deactivate_opt: opt.deactivate() return data
def _perform_queue(self, ah, *args, **kwds): """ Perform the queue operation. This method returns the ActionHandle, and the ActionHandle status indicates whether the queue was successful. """ solver = kwds.pop('solver', kwds.pop('opt', None)) if solver is None: raise ActionManagerError( "No solver passed to %s, use keyword option 'solver'" % (type(self).__name__) ) if not isinstance(solver, six.string_types): solver = solver.name # # Handle ephemeral solvers options here. These # will override whatever is currently in the options # dictionary, but we will reset these options to # their original value at the end of this method. # ephemeral_solver_options = {} ephemeral_solver_options.update(kwds.pop('options', {})) ephemeral_solver_options.update( OptSolver._options_string_to_dict(kwds.pop('options_string', ''))) opt = SolverFactory('_neos') opt._presolve(*args, **kwds) # # Map NEOS name, using lowercase convention in Pyomo # if len(self._solvers) == 0: for name in self.kestrel.solvers(): if name.endswith('AMPL'): self._solvers[ name[:-5].lower() ] = name[:-5] if not solver in self._solvers: raise ActionManagerError("Solver '%s' is not recognized by NEOS" % solver) # # Apply kestrel # os.environ['kestrel_options'] = 'solver=%s' % self._solvers[solver] solver_options = {} for key in opt.options: solver_options[key]=opt.options[key] solver_options.update(ephemeral_solver_options) options = opt._get_options_string(solver_options) if not options == "": os.environ[self._solvers[solver].lower()+'_options'] = opt._get_options_string() xml = self.kestrel.formXML(opt._problem_files[0]) (jobNumber, password) = self.kestrel.submit(xml) ah.job = jobNumber ah.password = password # # Store action handle, and return # self._ah[jobNumber] = ah self._neos_log[jobNumber] = (0, "") self._opt_data[jobNumber] = (opt, opt._smap_id, opt._load_solutions, opt._select_index, opt._default_variable_value) self._args[jobNumber] = args return ah
def _perform_queue(self, ah, *args, **kwds): """ Perform the queue operation. This method returns the ActionHandle, and the ActionHandle status indicates whether the queue was successful. """ solver = kwds.pop('solver', kwds.pop('opt', None)) if solver is None: raise ActionManagerError( "No solver passed to %s, use keyword option 'solver'" % (type(self).__name__) ) if not isinstance(solver, six.string_types): solver_name = solver.name if solver_name == 'asl': solver_name = \ os.path.basename(solver.executable()) else: solver_name = solver solver = None # # Handle ephemeral solvers options here. These # will override whatever is currently in the options # dictionary, but we will reset these options to # their original value at the end of this method. # user_solver_options = {} # make sure to transfer the options dict on the # solver plugin if the user does not use a string # to identify the neos solver. The ephemeral # options must also go after these. if solver is not None: user_solver_options.update(solver.options) user_solver_options.update( kwds.pop('options', {})) user_solver_options.update( OptSolver._options_string_to_dict(kwds.pop('options_string', ''))) opt = SolverFactory('_neos') opt._presolve(*args, **kwds) # # Map NEOS name, using lowercase convention in Pyomo # if len(self._solvers) == 0: for name in self.kestrel.solvers(): if name.endswith('AMPL'): self._solvers[ name[:-5].lower() ] = name[:-5] if solver_name not in self._solvers: raise ActionManagerError( "Solver '%s' is not recognized by NEOS. " "Solver names recognized:\n%s" % (solver_name, str(sorted(self._solvers.keys())))) # # Apply kestrel # # Set the kestrel_options environment # neos_sname = self._solvers[solver_name].lower() os.environ['kestrel_options'] = 'solver=%s' % self._solvers[solver_name] # # Set the <solver>_options environment # solver_options = {} for key in opt.options: solver_options[key]=opt.options[key] solver_options.update(user_solver_options) options = opt._get_options_string(solver_options) if not options == "": os.environ[neos_sname+'_options'] = options # # Generate an XML string using these two environment variables # xml = self.kestrel.formXML(opt._problem_files[0]) (jobNumber, password) = self.kestrel.submit(xml) ah.job = jobNumber ah.password = password # # Cleanup # del os.environ['kestrel_options'] try: del os.environ[neos_sname+"_options"] except: pass # # Store action handle, and return # self._ah[jobNumber] = ah self._neos_log[jobNumber] = (0, "") self._opt_data[jobNumber] = (opt, opt._smap_id, opt._load_solutions, opt._select_index, opt._default_variable_value) self._args[jobNumber] = args return ah