Note
Go to the end to download the full example code.
Creating Model TemplatesΒΆ
creating wrapper with func <function Class.test at 0x7f725708cae0> on <__main__.instancemethod object at 0x7f7256544560>
returning self <__main__.instancemethod object at 0x7f7256544560> with <function Class.test at 0x7f725708cae0> <__main__.Class object at 0x7f72565444a0> <class '__main__.Class'>
calling self <__main__.instancemethod object at 0x7f7256544560> with <function Class.test at 0x7f725708cae0> <__main__.Class object at 0x7f72565444a0> 3.0
5.0
<Sys: x=1.2, y=3.4> SysOutput(w=array([13.]), z=array([4.84]))
This is Ipopt version 3.14.11, running with linear solver MUMPS 5.4.1.
Number of nonzeros in equality constraint Jacobian...: 0
Number of nonzeros in inequality constraint Jacobian.: 0
Number of nonzeros in Lagrangian Hessian.............: 3
Total number of variables............................: 2
variables with only lower bounds: 0
variables with lower and upper bounds: 0
variables with only upper bounds: 0
Total number of equality constraints.................: 0
Total number of inequality constraints...............: 0
inequality constraints with only lower bounds: 0
inequality constraints with lower and upper bounds: 0
inequality constraints with only upper bounds: 0
iter objective inf_pr inf_du lg(mu) ||d|| lg(rg) alpha_du alpha_pr ls
0 5.6300000e+02 0.00e+00 1.00e+02 -1.0 0.00e+00 - 0.00e+00 0.00e+00 0
1 1.0309551e+02 0.00e+00 2.93e+01 -1.0 1.31e+00 - 1.00e+00 1.00e+00f 1
2 1.6353181e+01 0.00e+00 8.40e+00 -1.0 8.54e-01 - 1.00e+00 1.00e+00f 1
3 1.0743000e+00 0.00e+00 2.30e+00 -1.0 5.45e-01 - 1.00e+00 1.00e+00f 1
4 -1.1859781e+00 0.00e+00 5.63e-01 -1.0 3.39e-01 - 1.00e+00 1.00e+00f 1
5 -1.4553424e+00 0.00e+00 1.40e-01 -1.7 2.50e-01 - 1.00e+00 1.00e+00f 1
6 -1.4978741e+00 0.00e+00 4.73e-02 -2.5 1.84e-01 - 1.00e+00 1.00e+00f 1
7 -1.4999974e+00 0.00e+00 1.15e-03 -2.5 2.11e-02 - 1.00e+00 1.00e+00f 1
8 -1.5000000e+00 0.00e+00 2.21e-06 -5.7 1.40e-03 - 1.00e+00 1.00e+00f 1
9 -1.5000000e+00 0.00e+00 7.65e-12 -8.6 2.41e-06 - 1.00e+00 1.00e+00f 1
Number of Iterations....: 9
(scaled) (unscaled)
Objective...............: -3.9164490861618806e-01 -1.5000000000000000e+00
Dual infeasibility......: 7.6542195848125813e-12 2.9315661009832183e-11
Constraint violation....: 0.0000000000000000e+00 0.0000000000000000e+00
Variable bound violation: 0.0000000000000000e+00 0.0000000000000000e+00
Complementarity.........: 0.0000000000000000e+00 0.0000000000000000e+00
Overall NLP error.......: 7.6542195848125813e-12 2.9315661009832183e-11
Number of objective function evaluations = 10
Number of objective gradient evaluations = 10
Number of equality constraint evaluations = 0
Number of inequality constraint evaluations = 0
Number of equality constraint Jacobian evaluations = 0
Number of inequality constraint Jacobian evaluations = 0
Number of Lagrangian Hessian evaluations = 9
Total seconds in IPOPT = 0.004
EXIT: Optimal Solution Found.
import functools
import numpy as np
import condor as co
rng = np.random.default_rng(123)
class instancemethod: # noqa: N801
def __init__(self, func):
print("creating wrapper with func", func, "on", self)
self.func = func
def __get__(self, obj, cls):
print("returning self", self, " with", self.func, obj, cls)
if obj is None:
return self
else:
return functools.partial(self, obj)
def __call__(self, *args, **kwargs):
print("calling self", self, " with", self.func, *args, **kwargs)
return self.func(*args, **kwargs)
class Class:
def __init__(self, x):
self.x = x
@instancemethod
def test(self, y):
return self.x + y
cls = Class(2.0)
print(cls.test(3.0))
class ComponentRaw(co.models.ModelTemplate):
"""Raw Component base"""
input = co.FreeField(co.Direction.input)
output = co.AssignedField(co.Direction.output)
x = placeholder(default=2.0)
y = placeholder(default=1.0)
output.z = x**2 + y
def hello(self):
print("world", self.z, self.x, self.y, self.input, self.output)
class ComponentImplementation(co.implementations.ExplicitSystem):
pass
co.implementations.ComponentRaw = ComponentImplementation
class ComponentAT(co.ExplicitSystem, as_template=True):
"""AT component base"""
x = placeholder(default=2.0)
y = placeholder(default=1.0)
output.z = x**2 + y
def hello(self):
print("world", self.x, self.y, self.z)
class MyComponentR(ComponentRaw):
"""my component R"""
u = input()
output.w = z + u
def hello2(self):
print("world", self.z)
class MyComponentA(ComponentAT):
"""my component A"""
u = input()
output.w = z + u
assert MyComponentR(u=1.23).z == MyComponentA(u=1.23).z # noqa
# comp = MyComponentA(u=1., z=5.)
class MyComponent1(ComponentRaw):
pass
comp1 = MyComponent1()
class MyComponent2(ComponentAT):
u = input()
# output.xx = z+u
output.x = u + 2.0
# output.x = z+u # this should produce an error because it's overwriting x but didnt
comp2 = MyComponent2(u=1.0)
class MatSys(co.ExplicitSystem):
A = input(shape=(3, 4))
B = input(shape=(4, 2))
output.C = A @ B
ms = MatSys(rng.random(size=(3, 4)), rng.random(size=(4, 2)))
class SymMatSys(co.ExplicitSystem):
A = input(shape=(3, 3), symmetric=True)
B = input(shape=(3, 3))
output.C = A @ B + B.T @ A
a = rng.random(size=(3, 3))
sms = SymMatSys(a + a.T, rng.random(size=(3, 3)))
class Sys(co.ExplicitSystem):
x = input()
y = input()
v = y**2
output.w = x**2 + y**2
output.z = x**2 + y
sys = Sys(1.2, 3.4)
print(sys, sys.output)
class Opt(co.OptimizationProblem):
x = variable()
y = variable()
sys = Sys(x=x, y=y)
objective = (sys.w - 1) ** 2 - sys.z
Opt.set_initial(x=3.0, y=4.0)
opt = Opt()
Total running time of the script: (0 minutes 0.036 seconds)