Esempio n. 1
0
    def test_sum(self):

        r = np.random.randn(3, 2)

        x = optmod.VariableScalar('x', value=4.)
        y = optmod.VariableMatrix('y', value=r)

        self.assertTupleEqual(y.shape, (3, 2))
        self.assertTrue(np.all(y.get_value() == r))

        # scalar
        f = optmod.sum(x)
        self.assertTrue(f is x)

        self.assertTrue(optmod.sum(x, axis=0) is x)
        self.assertRaises(Exception, optmod.sum, x, 1)

        # matrix
        f = optmod.sum(y)
        self.assertTrue(isinstance(f, optmod.expression.Expression))
        self.assertTrue(f.is_function())
        self.assertEqual(len(f.arguments), 6)
        self.assertEqual(
            str(f), 'y[0,0] + y[0,1] + y[1,0] + y[1,1] + y[2,0] + y[2,1]')

        # matrix axis
        f = optmod.sum(y, axis=0)
        self.assertTrue(isinstance(f, optmod.expression.ExpressionMatrix))
        self.assertTupleEqual(f.shape, (1, 2))
        self.assertEqual(str(f), ('[[ y[0,0] + y[1,0] + y[2,0],' +
                                  ' y[0,1] + y[1,1] + y[2,1] ]]\n'))

        # matrix axis
        f = optmod.sum(y, axis=1)
        self.assertTrue(isinstance(f, optmod.expression.ExpressionMatrix))
        self.assertTupleEqual(f.shape, (3, 1))
        self.assertEqual(
            str(f), ('[[ y[0,0] + y[0,1] ],\n' + ' [ y[1,0] + y[1,1] ],\n' +
                     ' [ y[2,0] + y[2,1] ]]\n'))

        self.assertRaises(Exception, optmod.sum, x, 2)
Esempio n. 2
0
    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.)
Esempio n. 3
0
    def test_evaluator_eval_multi_output(self):

        x = optmod.VariableScalar(name='x', value=3.)
        y = optmod.VariableScalar(name='y', value=4.)

        # var
        f1 = 3 * (x + optmod.sin(y))
        f2 = optmod.sum([x, y])
        e = optmod.coptmod.Evaluator(2, 2)
        f1.__fill_evaluator__(e)
        f2.__fill_evaluator__(e)
        e.set_input_var(0, id(x))
        e.set_input_var(1, id(y))
        e.set_output_node(0, id(f1))
        e.set_output_node(1, id(f2))
        val = e.get_value()
        self.assertTupleEqual(val.shape, (1, 2))
        self.assertTrue(isinstance(val, np.matrix))
        e.eval([5., 8.])
        val = e.get_value()
        self.assertAlmostEqual(val[0, 0], 3. * (5. + np.sin(8.)))
        self.assertAlmostEqual(val[0, 1], 5. + 8.)
Esempio n. 4
0
# 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)
Esempio n. 5
0
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))