Exemplo n.º 1
0
    def test_pyomo_command(self):
        results = os.path.join(currdir, 'result.yml')
        args = [
            os.path.join(currdir, 't1.py'),
            '--solver-manager=neos',
            '--solver=cbc',
            '--symbolic-solver-labels',
            '--save-results=%s' % results,
            '-c',
        ]
        try:
            output = main.run(args)
            self.assertEqual(output.errorcode, 0)

            with open(results) as FILE:
                data = yaml.load(FILE, **yaml_load_args)
            self.assertEqual(data['Solver'][0]['Status'], 'ok')
            self.assertAlmostEqual(data['Solution'][1]['Status'], 'optimal')
            self.assertAlmostEqual(
                data['Solution'][1]['Objective']['o']['Value'], 1)
            self.assertAlmostEqual(
                data['Solution'][1]['Variable']['x']['Value'], 0.5)
        finally:
            cleanup()
            os.remove(results)
Exemplo n.º 2
0
 def pyomo(self, cmd, **kwds):
     import pyomo.scripting.pyomo_command as main
     sys.stdout.flush()
     sys.stderr.flush()
     print("Running: pyomo "+cmd)
     args = re.split('[ ]+',cmd.strip())
     return main.run(list(args))
Exemplo n.º 3
0
 def pyomo(self, cmd, **kwds):
     import pyomo.scripting.pyomo_command as main
     sys.stdout.flush()
     sys.stderr.flush()
     print("Running: pyomo " + cmd)
     args = re.split('[ ]+', cmd.strip())
     return main.run(list(args))
Exemplo n.º 4
0
    def test_pyomo_command(self):
        results = os.path.join(currdir, 'result.yml')
        args = [
            os.path.join(currdir,'t1.py'),
            '--solver-manager=neos',
            '--solver=cbc',
            '--symbolic-solver-labels',
            '--save-results=%s' % results,
            '-c',
            ]
        try:
            output = main.run(args)
            self.assertEqual(output.errorcode, 0)

            with open(results) as FILE:
                data = yaml.load(FILE)
            self.assertEqual(
                data['Solver'][0]['Status'], 'ok')
            self.assertAlmostEqual(
                data['Solution'][1]['Status'], 'optimal')
            self.assertAlmostEqual(
                data['Solution'][1]['Objective']['o']['Value'], 1)
            self.assertAlmostEqual(
                data['Solution'][1]['Variable']['x']['Value'], 0.5)
        finally:
            cleanup()
            os.remove(results)
Exemplo n.º 5
0
def lp_with_cplex_solve_test(self, name):
    root = name.split('_')[-1]
    options = self.get_options(name)
    if os.path.exists(join(datadir,root+'.dat')):
        options.append(join(datadir,root+'.dat'))
    res=main.run(['--solver=cplex','-c'] + options)
    if res.errorcode:
        raise ValueError("pyomo returned nonzero return code (%s)" % res.errorcode)
    if not res.retval.local.max_memory is None:
        self.recordTestData('maximum memory used', res.retval.local.max_memory)
Exemplo n.º 6
0
def lp_with_cplex_solve_test(self, name):
    root = name.split('_')[-1]
    options = self.get_options(name)
    if os.path.exists(datadir + root + '.dat'):
        options.append(datadir + root + '.dat')
    res = main.run(['--solver=cplex', '-c'] + options)
    if res.errorcode:
        raise ValueError("pyomo returned nonzero return code (%s)" %
                         res.errorcode)
    if not res.retval.local.max_memory is None:
        self.recordTestData('maximum memory used', res.retval.local.max_memory)
Exemplo n.º 7
0
 def pyomo(self, cmd, **kwargs):
     args = re.split('[ ]+', cmd)
     out = kwargs.get('file', None)
     if out is None:
         out = StringIO()
     with capture_output(out):
         os.chdir(currdir)
         output = main.run(args)
     if not 'file' in kwargs:
         return output.getvalue()
     return output
Exemplo n.º 8
0
 def pyomo(self, cmd, **kwargs):
     args = re.split('[ ]+', cmd)
     out = kwargs.get('file', None)
     if out is None:
         out = StringIO()
     setup_redirect(out)
     os.chdir(currdir)
     output = main.run(args)
     reset_redirect()
     if not 'file' in kwargs:
         return OUTPUT.getvalue()
     return output
Exemplo n.º 9
0
 def pyomo ( self, cmd, **kwargs):
     args = re.split('[ ]+', cmd )
     out = kwargs.get( 'file', None )
     if out is None:
         out = StringIO()
     setup_redirect( out )
     os.chdir( currdir )
     output = main.run( args )
     reset_redirect()
     if not 'file' in kwargs:
         return OUTPUT.getvalue()
     return output
Exemplo n.º 10
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 = [
            os.path.join(currdir, filename),
            '--solver-manager=neos',
            '--solver=%s' % opt,
            '--logging=quiet',
            '--save-results=%s' % results,
            '--results-format=json',
            '-c'
            ]
        try:
            output = main.run(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)
Exemplo n.º 11
0
    def pyomo(self, *args, **kwds):
        args=list(args)
        args.append('--solver-manager=neos')
        args.append('-c')
        if 'solver' in kwds:
            args.append('--solver='+kwds['solver'])
        args.append('--symbolic-solver-labels')
        args.append('--save-results=result.yml')
        #args.append('--tempdir='+currdir)
        #args.append('--keepfiles')
        #args.append('--debug')
        #args.append('--verbose')
        os.chdir(currdir)

        print('***')
        print(' '.join(args))
        try:
            output = main.run(args)
        except:
            output = None
        cleanup()
        print('***')
        return output
Exemplo n.º 12
0
    def pyomo(self, *args, **kwds):
        args=list(args)
        args.append('--solver-manager=neos')
        args.append('-c')
        if 'solver' in kwds:
            args.append('--solver='+kwds['solver'])
        args.append('--symbolic-solver-labels')
        args.append('--save-results=result.yml')
        #args.append('--tempdir='+currdir)
        #args.append('--keepfiles')
        #args.append('--debug')
        #args.append('--verbose')
        os.chdir(currdir)

        print('***')
        print(' '.join(args))
        try:
            output = main.run(args)
        except:
            output = None
        cleanup()
        print('***')
        return output