def test_analyze(self): x = optmod.variable.VariableScalar(name='x') y = optmod.variable.VariableScalar(name='y') f = optmod.cos(x) prop = f.__analyze__() self.assertFalse(prop['affine']) self.assertTrue(prop['b'] is np.NaN) self.assertEqual(len(prop['a']), 1.) self.assertTrue(x in prop['a']) f = optmod.cos(-4. * (-y + 3 * x - 2)) prop = f.__analyze__() self.assertFalse(prop['affine']) self.assertTrue(prop['b'] is np.NaN) self.assertEqual(len(prop['a']), 2.) self.assertTrue(x in prop['a']) self.assertTrue(y in prop['a']) f = optmod.cos((4. + x) * (-y + 3 * x - 2)) prop = f.__analyze__() self.assertFalse(prop['affine']) self.assertTrue(prop['b'] is np.NaN) self.assertEqual(len(prop['a']), 2.) self.assertTrue(x in prop['a']) self.assertTrue(y in prop['a'])
def test_derivative(self): x = optmod.variable.VariableScalar(name='x', value=2.) y = optmod.variable.VariableScalar(name='y', value=3.) f = optmod.cos(x) fx = f.get_derivative(x) fy = f.get_derivative(y) self.assertTrue(isinstance(fx, optmod.function.Function)) self.assertEqual(fx.get_value(), -np.sin(2.)) self.assertTrue(isinstance(fy, optmod.constant.Constant)) self.assertEqual(fy.get_value(), 0.) f = optmod.cos(x + optmod.cos(x * y)) fx = f.get_derivative(x) fy = f.get_derivative(y) self.assertTrue(fx.is_function()) self.assertAlmostEqual( fx.get_value(), -np.sin(2. + np.cos(2. * 3.)) * (1. - np.sin(2. * 3.) * 3.)) self.assertTrue(fy.is_function()) self.assertAlmostEqual( fy.get_value(), -np.sin(2. + np.cos(2. * 3.)) * (0. - np.sin(2. * 3.) * 2.)) f1 = optmod.cos(x + y * x) f1x = f1.get_derivative(x) f1y = f1.get_derivative(y) self.assertTrue(f1x.is_function()) self.assertAlmostEqual(f1x.get_value(), -np.sin(2. + 3. * 2.) * (1. + 3.)) self.assertAlmostEqual(f1y.get_value(), -np.sin(2. + 3. * 2.) * (0. + 2.)) f2 = f1 * y f2x = f2.get_derivative(x) f2y = f2.get_derivative(y) self.assertTrue(f2x.is_function()) self.assertAlmostEqual(f2x.get_value(), -np.sin(2. + 3. * 2.) * (1. + 3.) * 3.) self.assertAlmostEqual( f2y.get_value(), -np.sin(2. + 3. * 2.) * 3. * (0. + 2.) + np.cos(2. + 2. * 3.)) f3 = f1 + f2 f3x = f3.get_derivative(x) self.assertAlmostEqual(f3.get_value(), f1.get_value() + f2.get_value()) self.assertAlmostEqual(f3x.get_value(), f1x.get_value() + f2x.get_value())
def test_solve_ipopt_duals_J(self): x = optmod.VariableScalar('x') c1 = optmod.cos(x) == 0.75 c2 = x >= -np.pi c3 = x <= np.pi p = optmod.Problem(objective=minimize(5 * x), constraints=[c1, c2, c3]) try: info = p.solve(solver=optalg.opt_solver.OptSolverIpopt(), parameters={'quiet': True}) except ImportError: raise unittest.SkipTest('ipopt not available') self.assertEqual(info['status'], 'solved') self.assertAlmostEqual(x.get_value(), -np.abs(np.arccos(0.75)), places=5) self.assertAlmostEqual(np.cos(x.get_value()), 0.75, places=5) self.assertGreater(x.get_value(), -np.pi) self.assertLess(x.get_value(), np.pi) self.assertEqual(c2.get_dual(), 0.) self.assertEqual(c3.get_dual(), 0.) self.assertLess(np.abs(5. - (-np.sin(x.get_value())) * c1.get_dual()), 1e-7)
def test_constant(self): a = optmod.constant.Constant(4.) f = optmod.cos(a) self.assertTrue(f.is_constant()) self.assertEqual(f.get_value(), np.cos(4))
def test_function(self): rn = optmod.utils.repr_number x = optmod.variable.VariableScalar(name='x', value=3.) f = optmod.cos(3 * x) self.assertEqual(f.get_value(), np.cos(3. * 3.)) self.assertEqual(str(f), 'cos(x*%s)' % rn(3.)) f = optmod.cos(x + 1) self.assertEqual(f.get_value(), np.cos(3. + 1.)) self.assertEqual(str(f), 'cos(x + %s)' % rn(1.)) f = optmod.cos(optmod.cos(x)) self.assertEqual(f.get_value(), np.cos(np.cos(3.))) self.assertEqual(str(f), 'cos(cos(x))')
def test_contruction(self): x = optmod.variable.VariableScalar(name='x') f = optmod.cos(x) self.assertTrue(isinstance(f, optmod.cos)) self.assertEqual(f.name, 'cos') self.assertEqual(len(f.arguments), 1) self.assertTrue(f.arguments[0] is x) self.assertRaises(TypeError, optmod.cos, [x, 1., 2.])
def test_scalar(self): x = optmod.variable.VariableScalar(name='x', value=2.) f = optmod.cos(x) self.assertTrue(isinstance(f, optmod.cos)) self.assertEqual(f.name, 'cos') self.assertEqual(len(f.arguments), 1) self.assertTrue(f.arguments[0] is x) self.assertEqual(f.get_value(), np.cos(2.)) self.assertEqual(str(f), 'cos(x)')
def test_get_variables(self): x = optmod.VariableMatrix(name='x', shape=(2, 3)) y = optmod.VariableScalar(name='y') f = optmod.sin(x * 3) + optmod.cos(y + 10.) * x vars = f.get_variables() self.assertEqual(len(vars), 7) self.assertSetEqual( f.get_variables(), set([x[i, j] for i in range(2) for j in range(3)] + [y]))
def test_get_derivatives(self): x = optmod.VariableScalar(name='x', value=5.) y = optmod.VariableScalar(name='y', value=3.) z = optmod.VariableScalar(name='z', value=10.) f = 2 * x + optmod.cos(x * y) d = f.get_derivatives([x, y, z]) self.assertEqual(d[x].get_value(), 2 - np.sin(5 * 3.) * 3) self.assertEqual(d[y].get_value(), -np.sin(5 * 3.) * 5) self.assertEqual(d[z].get_value(), 0.)
def test_solve_NLP_beam(self): N = 500 h = 1. / N alpha = 350. t = optmod.VariableMatrix('t', shape=(N + 1, 1)) x = optmod.VariableMatrix('x', shape=(N + 1, 1)) u = optmod.VariableMatrix('u', shape=(N + 1, 1)) f = sum([ 0.5 * h * (u[i, 0] * u[i, 0] + u[i + 1, 0] * u[i + 1, 0]) + 0.5 * alpha * h * (cos(t[i, 0]) + cos(t[i + 1, 0])) for i in range(N) ]) constraints = [] for i in range(N): constraints.append(x[i + 1, 0] - x[i, 0] - 0.5 * h * (sin(t[i + 1, 0]) + sin(t[i, 0])) == 0) constraints.append(t[i + 1, 0] - t[i, 0] - 0.5 * h * (u[i + 1, 0] - u[i, 0]) == 0) constraints.append(t <= 1) constraints.append(t >= -1) constraints.append(-0.05 <= x) constraints.append(x <= 0.05) p = optmod.Problem(minimize(f), constraints) try: info = p.solve(solver=optalg.opt_solver.OptSolverIpopt(), parameters={'quiet': True}) except ImportError: raise unittest.SkipTest('ipopt not available') self.assertEqual(info['status'], 'solved') self.assertAlmostEqual(f.get_value(), 350.)
def test_matrix(self): value = np.random.randn(2, 3) x = optmod.variable.VariableMatrix(name='x', value=value) f = optmod.cos(x) self.assertTrue(isinstance(f, optmod.expression.ExpressionMatrix)) for i in range(2): for j in range(3): fij = f[i, j] self.assertTrue(isinstance(fij, optmod.cos)) self.assertEqual(len(fij.arguments), 1) self.assertTrue(fij.arguments[0] is x[i, j]) self.assertEqual(fij.get_value(), np.cos(value[i, j])) self.assertEqual(str(fij), 'cos(x[%d,%d])' % (i, j))
def test_solve_feasibility(self): x = optmod.VariableScalar('x', value=1.) constr = [x * optmod.cos(x) - x * x == 0] p = optmod.Problem(EmptyObjective(), constr) # std prob std_prob = p.__get_std_problem__() self.assertListEqual(std_prob.properties, ['nonlinear', 'continuous', 'feasibility']) try: self.assertRaises(TypeError, p.solve, optalg.opt_solver.OptSolverClp(), {'quiet': True}) except ImportError: raise unittest.SkipTest('clp not available') try: self.assertRaises(TypeError, p.solve, optalg.opt_solver.OptSolverCbc(), {'quiet': True}) except ImportError: raise unittest.SkipTest('cbc not available') info = p.solve(optalg.opt_solver.OptSolverNR(), parameters={ 'quiet': True, 'feastol': 1e-10 }) self.assertEqual(info['status'], 'solved') self.assertAlmostEqual(x.get_value(), 0.739085133215161, places=7) x.value = 1. self.assertNotAlmostEqual(x.get_value(), 0.739085133215161, places=7) info = p.solve(optalg.opt_solver.OptSolverNR(), parameters={ 'quiet': True, 'feastol': 1e-10 }, fast_evaluator=False) self.assertEqual(info['status'], 'solved') self.assertAlmostEqual(x.get_value(), 0.739085133215161, places=7)
def test_matrix_get_fast_evaluator(self): xval = np.random.randn(4, 3) x = optmod.VariableMatrix(name='x', value=xval) y = optmod.VariableScalar(name='y', value=10.) self.assertTupleEqual(x.shape, (4, 3)) f = optmod.sin(3 * x + 10.) * optmod.cos(y - optmod.sum(x * y)) self.assertTupleEqual(f.shape, (4, 3)) variables = list(f.get_variables()) self.assertEqual(len(variables), 13) e = f.get_fast_evaluator(variables) val = e.get_value() self.assertTrue(isinstance(val, np.matrix)) self.assertTupleEqual(val.shape, (4, 3)) self.assertTrue(np.all(val == 0)) e.eval(np.array([x.get_value() for x in variables])) val = e.get_value() val1 = np.sin(3 * xval + 10.) * np.cos(10. - np.sum(xval * 10.)) self.assertTupleEqual(val.shape, (4, 3)) self.assertLess(np.linalg.norm(val - val1), 1e-10) x = np.array([v.get_value() for v in variables]) e.eval(x) self.assertLess(np.max(np.abs(e.get_value() - f.get_value())), 1e-10) t0 = time.time() for i in range(500): f.get_value() t1 = time.time() for i in range(500): e.eval(x) t2 = time.time() self.assertGreater((t1 - t0) / (t2 - t1), 400.)
def test_std_components(self): x = optmod.variable.VariableScalar('x', value=2.) y = optmod.variable.VariableScalar('y', value=3.) f = optmod.cos(x + y * x) comp = f.__get_std_components__() phi = comp['phi'] gphi_list = comp['gphi_list'] Hphi_list = comp['Hphi_list'] self.assertTrue(phi is f) self.assertEqual(len(gphi_list), 2) v, exp = gphi_list[0] self.assertTrue(v is x) self.assertTrue(exp.is_function()) self.assertEqual(exp.get_value(), -np.sin(2. + 2. * 3.) * (1. + 3.)) v, exp = gphi_list[1] self.assertTrue(v is y) self.assertTrue(exp.is_function()) self.assertEqual(exp.get_value(), -np.sin(2. + 2. * 3.) * (0. + 2.))
import time import numpy as np from optmod import VariableMatrix, VariableScalar, sin, cos, sum x = VariableMatrix(name='x', value=np.random.randn(4, 3)) y = VariableScalar(name='y', value=10.) f = sin(3 * x + 10.) * cos(y - sum(x * y)) vars = list(f.get_variables()) e = f.get_fast_evaluator(vars) var_values = np.array([v.get_value() for v in vars]) e.eval(var_values) print('same value:', np.all(e.get_value() == f.get_value())) t0 = time.time() for i in range(500): f.get_value() t1 = time.time() for i in range(500): e.eval(var_values) t2 = time.time() print('speedup:', (t1 - t0) / (t2 - t1))
# Ported from https://github.com/JuliaOpt/JuMP.jl/blob/master/examples/clnlbeam.jl import optmod import optalg from optmod import sum, cos, sin, minimize N = 1000 h = 1./N alpha = 350. t = optmod.VariableMatrix('t', shape=(N+1,1)) x = optmod.VariableMatrix('x', shape=(N+1,1)) u = optmod.VariableMatrix('u', shape=(N+1,1)) f = sum([0.5*h*(u[i,0]*u[i,0]+u[i+1,0]*u[i+1,0]) + 0.5*alpha*h*(cos(t[i,0]) + cos(t[i+1,0])) for i in range(N)]) constraints = [] for i in range(N): constraints.append(x[i+1,0] - x[i,0] - 0.5*h*(sin(t[i+1,0])+sin(t[i,0])) == 0) constraints.append(t[i+1,0] - t[i,0] - 0.5*h*(u[i+1,0] - u[i,0]) == 0) constraints.append(t <= 1) constraints.append(t >= -1) constraints.append(-0.05 <= x) constraints.append(x <= 0.05) p = optmod.Problem(minimize(f), constraints) info = p.solve(solver=optalg.opt_solver.OptSolverIpopt(), fast_evaluator=True)
def test_std_problem(self): np.random.seed(0) x = optmod.variable.VariableScalar('x', value=3.) y = optmod.variable.VariableScalar('y', value=4.) f = x * x + 2 * x * y + y * y c0 = x <= 10 c1 = x + y == 3 c2 = 3 * x <= 10 # slack s1 c3 = y >= 19. c4 = x * y == 20. c5 = optmod.sin(x) <= 3 # slack s2 c6 = optmod.cos(y) >= 4. # slack s3 p = optmod.Problem(minimize(f), [c0, c1, c2, c3, c4, c5, c6]) std_prob = p.__get_std_problem__() # vars self.assertEqual(len(std_prob.var2index), 5) self.assertEqual(len(std_prob.index2var), 5) index_x = std_prob.var2index[x] index_y = std_prob.var2index[y] index_s1 = std_prob.var2index[c2.slack] index_s2 = std_prob.var2index[c5.slack] index_s3 = std_prob.var2index[c6.slack] # x self.assertEqual(std_prob.x.size, 5) temp = np.zeros(5) temp[index_x] = 3. temp[index_y] = 4. self.assertTrue(np.all(std_prob.x == temp)) # A, b self.assertTupleEqual(std_prob.A.shape, (2, 5)) self.assertEqual(std_prob.A.nnz, 4) self.assertTrue(np.all(std_prob.A.row == np.array([0, 0, 1, 1]))) self.assertTrue( np.all(std_prob.A.col == np.array( [index_x, index_y, index_x, index_s1]))) self.assertTrue(np.all(std_prob.A.data == np.array([1, 1, 3, -1]))) self.assertTrue(np.all(std_prob.b == np.array([3., 10.]))) # u, l inf = 1e8 self.assertEqual(std_prob.u.size, 5) self.assertEqual(std_prob.l.size, 5) temp = np.zeros(5) temp[index_x] = 10. temp[index_y] = inf temp[index_s1] = 0 temp[index_s2] = 0 temp[index_s3] = inf self.assertTrue(np.all(std_prob.u == temp)) temp = np.zeros(5) temp[index_x] = -inf temp[index_y] = 19 temp[index_s1] = -inf temp[index_s2] = -inf temp[index_s3] = 0 self.assertTrue(np.all(std_prob.l == temp)) # integer flags self.assertTrue(isinstance(std_prob.P, np.ndarray)) self.assertEqual(std_prob.P.size, 5) self.assertNotEqual(std_prob.P.dtype, int) self.assertEqual(std_prob.P.dtype, bool) self.assertTrue(np.all(std_prob.P == False)) self.assertFalse(np.any(std_prob.P == True)) # phi, gphi, Hphi before eval self.assertEqual(std_prob.phi, 0.) self.assertTrue(np.all(std_prob.gphi == np.zeros(5))) self.assertTupleEqual(std_prob.Hphi.shape, (5, 5)) self.assertEqual(std_prob.Hphi.nnz, 3) if index_x <= index_y: self.assertTrue( np.all(std_prob.Hphi.row == np.array( [index_x, index_y, index_y]))) self.assertTrue( np.all(std_prob.Hphi.col == np.array( [index_x, index_x, index_y]))) else: self.assertTrue( np.all(std_prob.Hphi.row == np.array( [index_x, index_x, index_y]))) self.assertTrue( np.all(std_prob.Hphi.col == np.array( [index_x, index_y, index_y]))) self.assertTrue(np.all(std_prob.Hphi.data == np.zeros(3))) # f, J, H_combined before eval self.assertEqual(std_prob.f.size, 3) self.assertTrue(np.all(std_prob.f == np.zeros(3))) self.assertTupleEqual(std_prob.J.shape, (3, 5)) self.assertEqual(std_prob.J.nnz, 6) self.assertTrue(np.all(std_prob.J.row == np.array([0, 0, 1, 1, 2, 2]))) self.assertTrue( np.all(std_prob.J.col == np.array( [index_x, index_y, index_x, index_s2, index_y, index_s3]))) self.assertTrue(np.all(std_prob.J.data == np.zeros(6))) self.assertEqual(std_prob.H_combined.nnz, 3) if index_x <= index_y: self.assertTrue( np.all(std_prob.H_combined.row == np.array( [index_y, index_x, index_y]))) self.assertTrue( np.all(std_prob.H_combined.col == np.array( [index_x, index_x, index_y]))) else: self.assertTrue( np.all(std_prob.H_combined.row == np.array( [index_x, index_x, index_y]))) self.assertTrue( np.all(std_prob.H_combined.col == np.array( [index_y, index_x, index_y]))) self.assertTrue(np.all(std_prob.H_combined.data == np.zeros(3))) var = np.random.randn(5) # Eval std_prob.eval(var) # phi, gphi, Hphi after eval self.assertAlmostEqual( std_prob.phi, (var[index_x] * var[index_x] + 2. * var[index_x] * var[index_y] + var[index_y] * var[index_y])) temp = np.zeros(5) temp[index_x] = 2. * var[index_x] + 2. * var[index_y] temp[index_y] = 2. * var[index_x] + 2. * var[index_y] self.assertTrue(norm(std_prob.gphi - temp) < 1e-8) self.assertTrue(np.all(std_prob.Hphi.data == np.array([2., 2., 2.]))) # f, J after eval self.assertTrue( norm(std_prob.f - np.array([ var[index_x] * var[index_y] - 20., np.sin(var[index_x]) - var[index_s2] - 3., np.cos(var[index_y]) - var[index_s3] - 4. ])) < 1e-8) self.assertTrue( np.all(std_prob.J.data == np.array([ var[index_y], var[index_x], np.cos(var[index_x]), -1., -np.sin(var[index_y]), -1. ]))) # Combine H - ones std_prob.combine_H(np.ones(3)) self.assertTrue( np.all(std_prob.H_combined.data == np.array( [1., -np.sin(var[index_x]), -np.cos(var[index_y])]))) # Combine H - rand lam = np.random.randn(3) std_prob.combine_H(lam) self.assertTrue( np.all(std_prob.H_combined.data == np.array([ 1. * lam[0], -np.sin(var[index_x]) * lam[1], -np.cos(var[index_y]) * lam[2] ]))) # Properties self.assertTrue(len(std_prob.properties), 3) self.assertTrue('continuous' in std_prob.properties) self.assertTrue('optimization' in std_prob.properties) self.assertTrue('nonlinear' in std_prob.properties)
def test_std_components(self): x = optmod.variable.VariableScalar('x') y = optmod.variable.VariableScalar('y') f = x * x + 2 * x * y + y * y c0 = x <= 10 c1 = x + y == 3 c2 = 3 * x <= 10 c3 = y >= 19. c4 = x * y == 20. c5 = optmod.sin(x) <= 3 c6 = optmod.cos(y) >= 4. p = optmod.Problem(minimize(f), [c0, c1, c2, c3, c4, c5, c6]) comp = p.__get_std_components__() self.assertEqual(len(comp), 14) phi = comp['phi'] gphi_list = comp['gphi_list'] Hphi_list = comp['Hphi_list'] phi_prop = comp['phi_prop'] cA_list = comp['cA_list'] cJ_list = comp['cJ_list'] A_list = comp['A_list'] b_list = comp['b_list'] f_list = comp['f_list'] J_list = comp['J_list'] H_list = comp['H_list'] u_list = comp['u_list'] l_list = comp['l_list'] prop_list = comp['prop_list'] self.assertEqual(len(cA_list), len(b_list)) self.assertEqual(len(cJ_list), len(f_list)) self.assertEqual(str(phi), 'x*x + x*2.00e+00*y + y*y') self.assertEqual(str(gphi_list), '[(x, x + x + y*2.00e+00), (y, x*2.00e+00 + y + y)]') self.assertEqual( str(Hphi_list), '[(x, x, 2.00e+00), (x, y, 2.00e+00), (y, y, 2.00e+00)]') self.assertEqual( str(A_list), '[(0, x, 1.0), (0, y, 1.0), (1, x, 3.0), (1, s, -1.0)]') self.assertEqual(str(b_list), '[3.0, 10.0]') self.assertEqual( str(f_list), '[x*y + -2.00e+01, sin(x) + -3.00e+00 + s*-1.00e+00, cos(y) + -4.00e+00 + s*-1.00e+00]' ) self.assertEqual( str(J_list), '[(0, x, y), (0, y, x), (1, x, cos(x)), (1, s, -1.00e+00), (2, y, sin(y)*-1.00e+00), (2, s, -1.00e+00)]' ) self.assertEqual( str(H_list), '[[(x, y, 1.00e+00)], [(x, x, sin(x)*-1.00e+00)], [(y, y, -1.00e+00*cos(y))]]' ) self.assertEqual(str([(x[0], x[1]) for x in u_list]), '[(x, 10.0), (s, 0), (s, 0)]') self.assertEqual(str([(x[0], x[1]) for x in l_list]), '[(y, 19.0), (s, 0)]')
from optalg.opt_solver import OptSolverNR from optmod import VariableScalar, Problem, EmptyObjective, cos x = VariableScalar('x', value=1.) constraints = [x * cos(x) - x * x == 0] p = Problem(EmptyObjective(), constraints) info = p.solve(OptSolverNR(), parameters={'quiet': False, 'feastol': 1e-10}) print(x, x.get_value())
dQ = 0. for gen in bus.generators: dP += P[gen.index] dQ += Q[gen.index] for load in bus.loads: dP -= load.P dQ -= load.Q for shunt in bus.shunts: dP -= shunt.g * v[bus.index] * v[bus.index] dQ += shunt.b * v[bus.index] * v[bus.index] for br in bus.branches_k: vk, vm = v[br.bus_k.index], v[br.bus_m.index] dw = w[br.bus_k.index] - w[br.bus_m.index] - br.phase dP -= (br.ratio** 2.) * vk * vk * (br.g_k + br.g) - br.ratio * vk * vm * ( br.g * cos(dw) + br.b * sin(dw)) dQ -= -(br.ratio**2.) * vk * vk * ( br.b_k + br.b) - br.ratio * vk * vm * (br.g * sin(dw) - br.b * cos(dw)) for br in bus.branches_m: vk, vm = v[br.bus_k.index], v[br.bus_m.index] dw = w[br.bus_m.index] - w[br.bus_k.index] + br.phase dP -= vm * vm * (br.g_m + br.g) - br.ratio * vm * vk * ( br.g * cos(dw) + br.b * sin(dw)) dQ -= -vm * vm * (br.b_m + br.b) - br.ratio * vm * vk * ( br.g * sin(dw) - br.b * cos(dw)) constraints.extend([dP == 0., dQ == 0.]) assert (abs(dP.get_value() - bus.P_mismatch) < 1e-8) assert (abs(dQ.get_value() - bus.Q_mismatch) < 1e-8) # Variable limits
def test_evaluator_eval_single_output(self): x = optmod.VariableScalar(name='x', value=3.) y = optmod.VariableScalar(name='y', value=4.) # var f = x e = optmod.coptmod.Evaluator(1, 1, scalar_output=True) f.__fill_evaluator__(e) e.set_input_var(0, id(x)) e.set_output_node(0, id(f)) self.assertEqual(e.get_value(), 0.) e.eval([5.]) self.assertEqual(e.get_value(), 5.) # constant f = optmod.constant.Constant(11.) e = optmod.coptmod.Evaluator(1, 1, scalar_output=True) f.__fill_evaluator__(e) e.set_input_var(0, id(x)) e.set_output_node(0, id(f)) self.assertEqual(e.get_value(), 0.) e.eval([5.]) self.assertEqual(e.get_value(), 11.) # add f = x + 3 e = optmod.coptmod.Evaluator(1, 1, scalar_output=True) f.__fill_evaluator__(e) e.set_input_var(0, id(x)) e.set_output_node(0, id(f)) self.assertEqual(e.get_value(), 0.) e.eval([9.]) self.assertEqual(e.get_value(), 12.) # sub f = x - 3 e = optmod.coptmod.Evaluator(1, 1, scalar_output=True) f.__fill_evaluator__(e) e.set_input_var(0, id(x)) e.set_output_node(0, id(f)) self.assertEqual(e.get_value(), 0.) e.eval([9.]) self.assertEqual(e.get_value(), 6.) # negate f = -(x + 10.) e = optmod.coptmod.Evaluator(1, 1, scalar_output=True) f.__fill_evaluator__(e) e.set_input_var(0, id(x)) e.set_output_node(0, id(f)) self.assertEqual(e.get_value(), 0.) e.eval([9.]) self.assertEqual(e.get_value(), -19.) # multiply f = y * (x + 3) e = optmod.coptmod.Evaluator(2, 1, scalar_output=True) f.__fill_evaluator__(e) e.set_input_var(0, id(x)) e.set_input_var(1, id(y)) e.set_output_node(0, id(f)) self.assertEqual(e.get_value(), 0.) e.eval([9., -20.]) self.assertEqual(e.get_value(), -20. * (9. + 3.)) # sin f = optmod.sin(x + 3) e = optmod.coptmod.Evaluator(1, 1, scalar_output=True) f.__fill_evaluator__(e) e.set_input_var(0, id(x)) e.set_output_node(0, id(f)) self.assertEqual(e.get_value(), 0.) e.eval([9.]) self.assertEqual(e.get_value(), np.sin(12.)) # cos f = optmod.cos(3 * y) e = optmod.coptmod.Evaluator(1, 1, scalar_output=True) f.__fill_evaluator__(e) e.set_input_var(0, id(y)) e.set_output_node(0, id(f)) self.assertEqual(e.get_value(), 0.) e.eval([5.]) self.assertEqual(e.get_value(), np.cos(15.))