Ejemplo n.º 1
0
 def pyomo(self, cmd, **kwds):
     if 'root' in kwds:
         OUTPUT = kwds['root'] + '.out'
         results = kwds['root'] + '.jsn'
         self.ofile = OUTPUT
     else:
         OUTPUT = StringIO()
         results = 'results.jsn'
     setup_redirect(OUTPUT)
     os.chdir(currdir)
     if type(cmd) is list:
         output = main.main([
             'solve', '--solver=glpk', '--results-format=json',
             '--save-results=%s' % results
         ] + cmd,
                            get_return=True)
     elif cmd.endswith('json') or cmd.endswith('yaml'):
         output = main.main([
             'solve', '--results-format=json',
             '--save-results=%s' % results
         ] + [cmd],
                            get_return=True)
     else:
         args = re.split('[ ]+', cmd)
         output = main.main([
             'solve', '--solver=glpk', '--results-format=json',
             '--save-results=%s' % results
         ] + list(args),
                            get_return=True)
     reset_redirect()
     if not 'root' in kwds:
         return OUTPUT.getvalue()
     return output
Ejemplo n.º 2
0
    def run_bilevel(self, *_args, **kwds):
        if self.solve:
            args = ['solve']
            _solver = kwds.get('solver','glpk')
            args.append('--solver=%s' % _solver)
            args.append('--save-results=result.yml')
            args.append('--results-format=json')
        else:
            args = ['convert']
        if 'transform' in kwds:
            args.append('--transform=%s' % kwds['transform'])
        args.append('-c')
        args.append('--symbolic-solver-labels')
        args.append('--file-determinism=2')

        if False:
            args.append('--stream-solver')
            args.append('--tempdir='+currdir)
            args.append('--keepfiles')
            args.append('--debug')
            args.append('--logging=verbose')

        args = args + list(_args)
        os.chdir(currdir)

        print('***')
        #print(' '.join(args))
        output = main.main(args)
        try:
            output = main.main(args)
        except:
            output = None
        cleanup()
        print('***')
        return output
Ejemplo n.º 3
0
    def run_bilevel(self, *_args, **kwds):
        if self.solve:
            args = ['solve']
            _solver = kwds.get('solver','glpk')
            args.append('--solver=%s' % _solver)
            args.append('--save-results=result.yml')
            args.append('--results-format=json')
        else:
            args = ['convert']
        if 'transform' in kwds:
            args.append('--transform=%s' % kwds['transform'])
        args.append('-c')
        args.append('--symbolic-solver-labels')
        args.append('--file-determinism=2')

        if False:
            args.append('--stream-solver')
            args.append('--tempdir='+currdir)
            args.append('--keepfiles')
            args.append('--debug')
            args.append('--logging=verbose')

        args = args + list(_args)
        os.chdir(currdir)

        print('***')
        #print(' '.join(args))
        output = main.main(args)
        try:
            output = main.main(args)
        except:
            output = None
        cleanup()
        print('***')
        return output
Ejemplo n.º 4
0
    def run_solver(self, *_args, **kwds):
        if self.solve:
            args = ['solve']
            args.append('--solver='+self.solver)
            args.append('--save-results=result.yml')
            args.append('--results-format=yaml')
            args.append('--solver-options="lemke_start=automatic output_options=yes"')
        else:
            args = ['convert']
        args.append('-c')
        args.append('--symbolic-solver-labels')
        args.append('--file-determinism=2')

        if False:
            args.append('--stream-solver')
            args.append('--tempdir='+currdir)
            args.append('--keepfiles')
            args.append('--logging=debug')

        args = args + list(_args)
        os.chdir(currdir)

        print('***')
        print(' '.join(args))
        try:
            output = pyomo_main.main(args)
        except SystemExit:
            output = None
        except:
            output = None
            raise
        cleanup()
        print('***')
        return output
Ejemplo n.º 5
0
    def run_solver(self, *_args, **kwds):
        if self.solve:
            args = ["solve"]
            args.append("--solver=" + self.solver)
            args.append("--save-results=result.yml")
            args.append("--results-format=yaml")
            args.append('--solver-options="lemke_start=automatic output_options=yes"')
        else:
            args = ["convert"]
        args.append("-c")
        args.append("--symbolic-solver-labels")
        args.append("--file-determinism=2")

        if False:
            args.append("--stream-solver")
            args.append("--tempdir=" + currdir)
            args.append("--keepfiles")
            args.append("--logging=debug")

        args = args + list(_args)
        os.chdir(currdir)

        print("***")
        print(" ".join(args))
        try:
            output = pyomo_main.main(args)
        except SystemExit:
            output = None
        except:
            output = None
            raise
        cleanup()
        print("***")
        return output
Ejemplo n.º 6
0
    def pyomo(self, *args, **kwds):
        if self.solve:
            args=['solve']+list(args)
            if 'solver' in kwds:
                args.append('--solver='+kwds['solver'])
            else:
                args.append('--solver=glpk')
            args.append('--save-results=result.yml')
        else:
            args=['convert']+list(args)
        if 'preprocess' in kwds:
            pp = kwds['preprocess']
            if pp == 'bigm':
                args.append('--transform=gdp.bigm')
            elif pp == 'chull':
                args.append('--transform=gdp.chull')
        args.append('-c')
        args.append('--symbolic-solver-labels')
        os.chdir(currdir)

        print('***')
        #if pproc is not None:
        #    pproc.activate()
        #    print("Activating " + kwds['preprocess'])
        #else:
        #    print("ERROR: no transformation activated: " + pp)
        print(' '.join(args))
        output = main.main(args)
        #if pproc is not None:
        #    pproc = None
        print('***')
        return output
Ejemplo n.º 7
0
    def run_pyomo(self, *args, **kwargs):
        """
        Run Pyomo with the given arguments. `args` should be a list with
        one argument token per string item.
        """
        if not 'glpk' in solvers:
            self.skipTest("GLPK is not installed")

        if isinstance(args, str):
            args = args.split()
        args = list(map(str, args))

        outputpath = kwargs.pop('outputpath',
                                os.path.join(exdir, 'results.jsn'))
        args = [
            'solve', '--solver=glpk', '--results-format=json', '-c',
            '--logging=quiet', '--save-results', outputpath
        ] + args

        old_path = os.getcwd()
        os.chdir(exdir)
        output = main.main(args)
        os.chdir(old_path)

        return outputpath
Ejemplo n.º 8
0
    def pyomo(self, *args, **kwds):
        if self.solve:
            args=['solve']+list(args)
            if 'solver' in kwds:
                args.append('--solver='+kwds['solver'])
            else:
                args.append('--solver=glpk')
            args.append('--save-results=result.yml')
        else:
            args=['convert']+list(args)
        if 'preprocess' in kwds:
            pp = kwds['preprocess']
            if pp == 'bigm':
                args.append('--transform=gdp.bigm')
            elif pp == 'chull':
                args.append('--transform=gdp.chull')
        args.append('-c')
        args.append('--symbolic-solver-labels')
        os.chdir(currdir)

        print('***')
        #if pproc is not None:
        #    pproc.activate()
        #    print("Activating " + kwds['preprocess'])
        #else:
        #    print("ERROR: no transformation activated: " + pp)
        print(' '.join(args))
        output = main.main(args)
        #if pproc is not None:
        #    pproc = None
        print('***')
        return output
Ejemplo n.º 9
0
    def run_solver(self, *_args, **kwds):
        if self.solve:
            args = ['solve']
            args.append('--solver=' + self.solver)
            args.append('--save-results=result.yml')
            args.append('--results-format=yaml')
            args.append(
                '--solver-options="lemke_start=automatic output_options=yes"')
        else:
            args = ['convert']
        args.append('-c')
        args.append('--symbolic-solver-labels')
        args.append('--file-determinism=2')

        if False:
            args.append('--stream-solver')
            args.append('--tempdir=' + currdir)
            args.append('--keepfiles')
            args.append('--logging=debug')

        args = args + list(_args)
        os.chdir(currdir)

        print('***')
        print(' '.join(args))
        try:
            output = pyomo_main.main(args)
        except SystemExit:
            output = None
        except:
            output = None
            raise
        cleanup()
        print('***')
        return output
Ejemplo n.º 10
0
 def pyomo(self, cmd, **kwds):
     if 'root' in kwds:
         OUTPUT = kwds['root'] + '.out'
         results = kwds['root'] + '.jsn'
         TempfileManager.add_tempfile(OUTPUT, exists=False)
         TempfileManager.add_tempfile(results, exists=False)
     else:
         OUTPUT = StringIO()
         results = 'results.jsn'
         TempfileManager.create_tempfile(suffix='results.jsn')
     with capture_output(OUTPUT):
         try:
             _dir = os.getcwd()
             os.chdir(currdir)
             args = ['solve', '--solver=glpk', '--results-format=json',
                     '--save-results=%s' % results]
             if type(cmd) is list:
                 args.extend(cmd)
             elif cmd.endswith('json') or cmd.endswith('yaml'):
                 args.append(cmd)
             else:
                 args.extend(re.split('[ ]+', cmd))
             output = main.main(args)
         finally:
             os.chdir(_dir)
     if not 'root' in kwds:
         return OUTPUT.getvalue()
     return output
Ejemplo n.º 11
0
    def run_pyomo(self, *args, **kwargs):
        """
        Run Pyomo with the given arguments. `args` should be a list with
        one argument token per string item.
        """
        if not "glpk" in solvers:
            self.skipTest("GLPK is not installed")

        if isinstance(args, str):
            args = args.split()
        args = list(map(str, args))

        outputpath = kwargs.pop("outputpath", os.path.join(exdir, "results.jsn"))
        args = [
            "solve",
            "--solver=glpk",
            "--results-format=json",
            "-c",
            "--logging=quiet",
            "--save-results",
            outputpath,
        ] + args

        old_path = os.getcwd()
        os.chdir(exdir)
        output = main.main(args)
        os.chdir(old_path)

        return outputpath
Ejemplo n.º 12
0
 def pyomo ( self, cmd, **kwargs):
     args = ['solve'] + re.split('[ ]+', cmd )
     out = kwargs.get( 'file', None )
     if out is None:
         out = StringIO()
     with capture_output(out):
         os.chdir( currdir )
         output = main( args )
     if not 'file' in kwargs:
         return output.getvalue()
     return output
Ejemplo n.º 13
0
 def pyomo(self, cmd, **kwds):
     if 'root' in kwds:
         OUTPUT=kwds['root']+'.out'
         results=kwds['root']+'.jsn'
         self.ofile = OUTPUT
     else:
         OUTPUT=StringIO()
         results='results.jsn'
     setup_redirect(OUTPUT)
     os.chdir(currdir)
     if type(cmd) is list:
         output = main.main(['solve', '--solver=glpk', '--results-format=json', '--save-results=%s' % results] + cmd)
     elif cmd.endswith('json') or cmd.endswith('yaml'):
         output = main.main(['solve', '--results-format=json', '--save-results=%s' % results] + [cmd])
     else:
         args=re.split('[ ]+',cmd)
         output = main.main(['solve', '--solver=glpk', '--results-format=json', '--save-results=%s' % results] + list(args))
     reset_redirect()
     if not 'root' in kwds:
         return OUTPUT.getvalue()
     return output
Ejemplo n.º 14
0
    def run_bilevel(self, *_args, **kwds):
        if self.solve:
            args = ['solve']
            if 'solver' in kwds:
                _solver = kwds.get('solver', 'glpk')
                args.append('--solver=bilevel_ld')
                args.append('--solver-options="solver=%s"' % _solver)
            args.append('--save-results=result.yml')
            args.append('--results-format=yaml')
        else:
            args = ['convert']
        if 'preprocess' in kwds:
            # args.append('--solver=glpk')
            pp = kwds['preprocess']
            if pp == 'linear_dual':
                args.append('--transform=bilevel.linear_dual')
        args.append('-c')

        # These were being ignored by the solvers for this package,
        # which now causes a helpful error message.
        # I've manually inserted them into those tests that need them to pass
        # (which is where they also get used)
        # args.append('--symbolic-solver-labels')
        # args.append('--file-determinism=2')

        if False:
            args.append('--stream-solver')
            args.append('--tempdir=' + currdir)
            args.append('--keepfiles')
            args.append('--logging=debug')

        args = args + list(_args)
        _cwd = os.getcwd()

        print('***')
        # print(' '.join(args))
        try:
            os.chdir(currdir)
            output = pyomo_main.main(args)
        except SystemExit:
            output = None
        except:
            output = None
            raise
        finally:
            os.chdir(_cwd)
        cleanup()
        print('***')
        return output
Ejemplo n.º 15
0
    def _run(self, opt, constrained=True):
        expected_y = {
            (pyo.minimize, True): -1,
            (pyo.maximize, True):  1,
            (pyo.minimize, False): -10,
            (pyo.maximize, False): 10,
        }[self.sense, constrained]

        filename = 'model_min_lp.py' if self.sense == pyo.minimize \
                   else 'model_max_lp.py'

        results = os.path.join(currdir, 'result.json')
        args = [
            'solve',
            os.path.join(currdir, filename),
            '--solver-manager=neos',
            '--solver=%s' % opt,
            '--logging=quiet',
            '--save-results=%s' % results,
            '--results-format=json',
            '-c'
            ]
        try:
            output = main(args)
            self.assertEqual(output.errorcode, 0)

            with open(results) as FILE:
                data = json.load(FILE)
        finally:
            cleanup()
            if os.path.exists(results):
                os.remove(results)

        self.assertEqual(
            data['Solver'][0]['Status'], 'ok')
        self.assertEqual(
            data['Solution'][1]['Status'], 'optimal')
        self.assertAlmostEqual(
            data['Solution'][1]['Objective']['obj']['Value'],
            expected_y, delta=1e-5)
        if constrained:
            # If the solver ignores constraints, x is degenerate
            self.assertAlmostEqual(
                data['Solution'][1]['Variable']['x']['Value'],
                1, delta=1e-5)
        self.assertAlmostEqual(
            data['Solution'][1]['Variable']['y']['Value'],
            expected_y, delta=1e-5)
Ejemplo n.º 16
0
 def pyomo(self, cmd, **kwds):
     args=re.split('[ ]+',cmd)
     if 'root' in kwds:
         OUTPUT=kwds['root']+'.out'
         results=kwds['root']+'.jsn'
         self.ofile = OUTPUT
     else:
         OUTPUT=StringIO()
         results='results.jsn'
     setup_redirect(OUTPUT)
     os.chdir(currdir)
     output = main.main(['--results-format=json', '-c', '--stream-solver', '--save-results=%s' % results, '--solver=%s' % kwds['solver']] + list(args))
     reset_redirect()
     if not 'root' in kwds:
         return OUTPUT.getvalue()
     return output
Ejemplo n.º 17
0
    def run_bilevel(self, *_args, **kwds):
        if self.solve:
            args = ['solve']
            if 'solver' in kwds:
                _solver = kwds.get('solver','glpk')
                args.append('--solver=bilevel_ld')
                args.append('--solver-options="solver=%s"' % _solver)
            args.append('--save-results=result.yml')
            args.append('--results-format=yaml')
        else:
            args = ['convert']
        if 'preprocess' in kwds:
            #args.append('--solver=glpk')
            pp = kwds['preprocess']
            if pp == 'linear_dual':
                args.append('--transform=bilevel.linear_dual')
        args.append('-c')

        # These were being ignored by the solvers for this package,
        # which now causes a helpful error message.
        # I've manually inserted them into those tests that need them to pass
        # (which is where they also get used)
        #args.append('--symbolic-solver-labels')
        #args.append('--file-determinism=2')

        if False:
            args.append('--stream-solver')
            args.append('--tempdir='+currdir)
            args.append('--keepfiles')
            args.append('--logging=debug')

        args = args + list(_args)
        os.chdir(currdir)

        print('***')
        #print(' '.join(args))
        try:
            output = pyomo_main.main(args)
        except SystemExit:
            output = None
        except:
            output = None
            raise
        cleanup()
        print('***')
        return output
Ejemplo n.º 18
0
    def run_bilevel(self, *_args, **kwds):
        if self.solve:
            args = ['solve']
            if 'solver' in kwds:
                _solver = kwds.get('solver', 'glpk')
                args.append('--solver=' + self.solver)
                args.append('--solver-options="solver=%s"' % _solver)
            args.append('--save-results=result.yml')
            args.append('--results-format=yaml')
        else:
            args = ['convert']
        if 'preprocess' in kwds:
            pp = kwds['preprocess']
            if pp == 'linear_mpec':
                args.append('--transform=bilevel.linear_mpec')
        args.append('-c')

        # These were being ignored by the solvers for this package,
        # which now causes a helpful error message
        #args.append('--symbolic-solver-labels')
        #args.append('--file-determinism=2')

        if False:
            args.append('--stream-solver')
            args.append('--tempdir=' + currdir)
            args.append('--keepfiles')
            args.append('--logging=debug')

        args = args + list(_args)
        os.chdir(currdir)

        print('***')
        #print(' '.join(args))
        try:
            output = pyomo_main.main(args)
        except SystemExit:
            output = None
        except:
            output = None
            raise
        cleanup()
        print('***')
        return output
Ejemplo n.º 19
0
 def pyomo(self, cmd, **kwds):
     args = re.split('[ ]+', cmd)
     if 'root' in kwds:
         OUTPUT = kwds['root'] + '.out'
         results = kwds['root'] + '.jsn'
         self.ofile = OUTPUT
     else:
         OUTPUT = StringIO()
         results = 'results.jsn'
     setup_redirect(OUTPUT)
     os.chdir(currdir)
     output = main.main([
         '--results-format=json', '-c', '--stream-solver',
         '--save-results=%s' % results,
         '--solver=%s' % kwds['solver']
     ] + list(args))
     reset_redirect()
     if not 'root' in kwds:
         return OUTPUT.getvalue()
     return output
Ejemplo n.º 20
0
    def run_pyomo(self, *args, **kwargs):
        """
        Run Pyomo with the given arguments. `args` should be a list with
        one argument token per string item.
        """
        if solver['glpk'] is None:
            self.skipTest("GLPK is not installed")

        if isinstance(args, str):
            args = args.split()
        args = list(map(str, args))

        outputpath = kwargs.pop('outputpath', os.path.join(exdir, 'results.jsn'))
        args = ['solve', '--solver=glpk', '--results-format=json', '-c', '--logging=quiet', '--save-results', outputpath] + args

        old_path = os.getcwd()
        os.chdir(exdir)
        output = main.main(args)
        os.chdir(old_path)

        return outputpath
Ejemplo n.º 21
0
    def run_solver(self, *_args, **kwds):
        if self.solve:
            args = ['solve']
            if 'solver' in kwds:
                _solver = kwds.get('solver','glpk')
                args.append('--solver='+self.solver)
                args.append('--solver-options="solver=%s"' % _solver)
            args.append('--save-results=result.yml')
            args.append('--results-format=yaml')
        else:
            args = ['convert']
        args.append('-c')

        # These were being ignored by the solvers for this package,
        # which now causes a helpful error message.
        #args.append('--symbolic-solver-labels')
        #args.append('--file-determinism=2')

        if False:
            args.append('--stream-solver')
            args.append('--tempdir='+currdir)
            args.append('--keepfiles')
            args.append('--logging=debug')

        args = args + list(_args)
        os.chdir(currdir)

        print('***')
        #print(' '.join(args))
        try:
            output = pyomo_main.main(args)
        except SystemExit:
            output = None
        except:
            output = None
            raise
        cleanup()
        print('***')
        return output
Ejemplo n.º 22
0
def run(args=None):
    from pyomo.scripting.pyomo_main import main
    if args is None:
        return main()
    else:
        return main(['solve'] + args)
Ejemplo n.º 23
0
 def pyomo(self, cmd):
     os.chdir(currdir)
     output = main.main(['convert', '--logging=quiet', '-c'] + cmd)
     return output
Ejemplo n.º 24
0
def pyomo2bar(args=None):
    from pyomo.scripting.pyomo_main import main
    if args is None:
        return main()
    else:
        return main(['convert', '--format=bar']+args)
Ejemplo n.º 25
0
def pyomo2dakota(args=None):
    from pyomo.scripting.pyomo_main import main
    if args is None:
        return main()
    else:
        return main(['convert', '--format=dakota'] + args)
Ejemplo n.º 26
0
 def pyomo(self, cmd):
     os.chdir(currdir)
     output = main.main(['convert', '--logging=quiet', '-c']+cmd)
     return output
Ejemplo n.º 27
0
def pyomo2dakota(args=None):
    from pyomo.scripting.pyomo_main import main
    if args is None:
        return main()
    else:
        return main(['convert','--format=dakota']+args, get_return=True)
Ejemplo n.º 28
0
def pyomo2bar(args=None):
    from pyomo.scripting.pyomo_main import main
    if args is None:
        return main()
    else:
        return main(['convert', '--format=bar']+args, get_return=True)
Ejemplo n.º 29
0
def run(args=None):
    from pyomo.scripting.pyomo_main import main
    if args is None:
        return main()
    else:
        return main(['solve']+args, get_return=True)