def test_fmincon_generator_logspace(self): "Test fmincon comparison tool (logspace)" x = Variable('x') y = Variable('y') m = Model(x, [x**3.2 >= 17 * y + y**-0.2, x >= 2, y == 4]) obj, c, ceq, _, _ = generate_mfiles(m, writefiles=False, logspace=True) self.assertEqual(c, [('log( + 17.0*exp( +-3.2 * x(1) +1 * x(2) )' ' + 1.0*exp( +-3.2 * x(1) +-0.2 * x(2) ) )'), 'log( + 2.0*exp( +-1 * x(1) ) )']) self.assertEqual(obj, 'log( + 1.0*exp( +1 * x(1) ) )') self.assertEqual(ceq, ['log( + 0.25*exp( +1 * x(2) ) )'])
def test_fmincon_generator(self): """Test fmincon comparison tool""" x = Variable('x') y = Variable('y') m = Model(x, [x**3.2 >= 17*y + y**-0.2, x >= 2, y == 4]) obj, c, ceq, DC, DCeq = generate_mfiles(m, writefiles=False) self.assertEqual(obj, 'x(2)') self.assertEqual(c, ['-x(2)**3.2 + 17*x(1) + x(1)**-0.2', '-x(2) + 2']) self.assertEqual(ceq, ['-x(1) + 4']) self.assertEqual(DC, ['-0.2*x(1).^-1.2 + 17,...\n ' + '-3.2*x(2).^2.2', '0,...\n -1']) self.assertEqual(DCeq, ['-1,...\n 0'])
with open('run_mfiles.m', 'w') as outfile: outfile.write('\n') for algorithm in algorithms: for guesstype in guesstypes: for gradobj in gradobjs: for gradconstr in gradconstrs: sf = simpleflight() directory = (algorithm + '/' + guesstype + '/' + gradobj + '/' + gradconstr) if not os.path.exists(directory): os.makedirs(directory) if guesstype == 'order-of-magnitude-mix': generate_mfiles(sf, logspace=True, algorithm=algorithm, guess=otherinitialguess) else: generate_mfiles(sf, logspace=True, algorithm=algorithm, guess=guesstype) os.rename('main.m', directory + '/main.m') os.rename('objfun.m', directory + '/objfun.m') os.rename('confun.m', directory + '/confun.m') os.rename('lookup.txt', directory + '/lookup.txt') with open('run_mfiles.m', 'a') as outfile: outfile.write('disp \'' + directory + '\'\n') outfile.write('cd ' + directory + '\n') outfile.write('main\n') outfile.write('cd ../../../../\n')
#'interior-point/ones/off/off'] with open('run_mfiles.m', 'w') as outfile: outfile.write('\n') for algorithm in algorithms: for guesstype in guesstypes: for gradobj in gradobjs: for gradconstr in gradconstrs: sf = simpleflight() directory = (algorithm + '/' + guesstype + '/' + gradobj + '/' + gradconstr) if not os.path.exists(directory): os.makedirs(directory) if guesstype == 'order-of-magnitude-mix': generate_mfiles(sf, False, algorithm, otherinitialguess, gradobj, gradconstr) else: generate_mfiles(sf, False, algorithm, guesstype, gradobj, gradconstr) os.rename('main.m', directory + '/main.m') os.rename('objfun.m', directory + '/objfun.m') os.rename('confun.m', directory + '/confun.m') os.rename('lookup.txt', directory + '/lookup.txt') if ((gradobj is 'on' and gradconstr is 'on') or (gradobj is 'off' and gradconstr is 'off')): with open('run_mfiles.m', 'a') as outfile: outfile.write('disp \'' + directory + '\'\n') outfile.write('cd ' + directory + '\n') if directory in excludedcases: outfile.write('%') outfile.write('main\n')
for gradconstr in gradconstrs: m = SimPleAC() sol = m.localsolve(x0=x0, modifylastgp=False) numgps = len(m.program.gps) for i in range(numgps): sa = SimPleAC() sol = sa.localsolve(x0=x0, modifylastgp=False) gp = sa.program.gps[i] m = Model(gp.cost, gp.flat(constraintsets=False), gp.substitutions) directory = (algorithm + '/' + guesstype + '/' + gradobj + '/' + gradconstr + '/iter_' + str(i + 1)) if not os.path.exists(directory): os.makedirs(directory) if i == 0: generate_mfiles(m, True, algorithm, guesstype, gradobj, gradconstr) else: prev_sol = sa.program.gps[i - 1].result['freevariables'] generate_mfiles(m, True, algorithm, prev_sol, gradobj, gradconstr) os.rename('main.m', directory + '/main.m') os.rename('objfun.m', directory + '/objfun.m') os.rename('confun.m', directory + '/confun.m') os.rename('lookup.txt', directory + '/lookup.txt') with open('run_mfiles.m', 'a') as outfile: outfile.write('clear\n') outfile.write('disp \'' + directory + '\'\n') outfile.write('cd ' + directory + '\n') outfile.write('main\n') outfile.write('cd ../../../../../\n')
# 'interior-point/ones/off/off' # ] with open('run_mfiles.m', 'w') as outfile: outfile.write('\n') for algorithm in algorithms: for guesstype in guesstypes: for gradobj in gradobjs: for gradconstr in gradconstrs: sa = SimPleAC() directory = (algorithm + '/' + guesstype + '/' + gradobj + '/' + gradconstr) if not os.path.exists(directory): os.makedirs(directory) generate_mfiles(sa, False, algorithm, guesstype, gradobj, gradconstr) os.rename('main.m', directory + '/main.m') os.rename('objfun.m', directory + '/objfun.m') os.rename('confun.m', directory + '/confun.m') os.rename('lookup.txt', directory + '/lookup.txt') if ((gradobj is 'on' and gradconstr is 'on') or (gradobj is 'off' and gradconstr is 'off')): with open('run_mfiles.m', 'a') as outfile: outfile.write('clear\n') outfile.write('disp \'' + directory + '\'\n') outfile.write('cd ' + directory + '\n') # if directory in excludedcases: # outfile.write('%') outfile.write('main\n') outfile.write('cd ../../../../\n')