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 = 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 # 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 _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
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