def simulate(es=None, **arguments): """Creates the optimization model, solves it and writes back results to energy system object Parameters ---------- es : :class:`oemof.solph.network.EnergySystem` object Energy system holding nodes, grouping functions and other important information. **arguments : key word arguments Arguments passed from command line """ logging.info("Creating optimization model...") om = OperationalModel(es=es, constraint_groups=ADD_SOLPH_BLOCKS) if arguments['--loglevel'] == 'DEBUG': lppath = os.path.join(arguments['--output-directory'], 'lp-file') if not os.path.exists(lppath): os.mkdir(lppath) lpfilepath = os.path.join(lppath, ''.join([om.name, '_', es.timestamp, '.lp'])) logging.info("Writing lp-file to '{}'".format(lpfilepath)) om.write(lpfilepath, io_options={'symbolic_solver_labels': True}) logging.info('Solving optimization model...') om.solve(arguments['--solver'], solve_kwargs={'tee': arguments['--solver-output']}, cmdline_options={"mipgap": arguments.get('--mipgap', 0)}) om.results() return om
def compare_lp_files(self, filename, ignored=None): om = OperationalModel(self.energysystem, timeindex=self.energysystem.timeindex) tmp_filename = filename.replace('.lp', '') + '_tmp.lp' new_filename = ospath.join(self.tmppath, tmp_filename) om.write(new_filename, io_options={'symbolic_solver_labels': True}) logging.info("Comparing with file: {0}".format(filename)) with open(ospath.join(self.tmppath, tmp_filename)) as generated_file: with open(ospath.join(ospath.dirname(ospath.realpath(__file__)), "lp_files", filename)) as expected_file: def chop_trailing_whitespace(lines): return [re.sub("\s*$", '', l) for l in lines] def remove(pattern, lines): if not pattern: return lines return re.subn(pattern, "", "\n".join(lines))[0].split("\n") expected = remove(ignored, chop_trailing_whitespace( expected_file.readlines())) generated = remove(ignored, chop_trailing_whitespace( generated_file.readlines())) eq_(generated, expected, "Failed matching expected with generated lp file:\n" + "\n".join(unified_diff(expected, generated, fromfile=ospath.relpath( expected_file.name), tofile=ospath.basename( generated_file.name), lineterm="")))
def compare_lp_files(self, filename, ignored=None): om = OperationalModel(self.energysystem, timeindex=self.energysystem.timeindex) tmp_filename = filename.replace('.lp', '') + '_tmp.lp' new_filename = ospath.join(self.tmppath, tmp_filename) om.write(new_filename, io_options={'symbolic_solver_labels': True}) logging.info("Comparing with file: {0}".format(filename)) with open(ospath.join(self.tmppath, tmp_filename)) as generated_file: with open( ospath.join(ospath.dirname(ospath.realpath(__file__)), "lp_files", filename)) as expected_file: def chop_trailing_whitespace(lines): return [re.sub("\s*$", '', l) for l in lines] def remove(pattern, lines): if not pattern: return lines return re.subn(pattern, "", "\n".join(lines))[0].split("\n") expected = remove( ignored, chop_trailing_whitespace(expected_file.readlines())) generated = remove( ignored, chop_trailing_whitespace(generated_file.readlines())) eq_( generated, expected, "Failed matching expected with generated lp file:\n" + "\n".join( unified_diff( expected, generated, fromfile=ospath.relpath(expected_file.name), tofile=ospath.basename(generated_file.name), lineterm="")))