# The optimize Method

The optimize method for the PID model is shown below. The objective function in this case is the weighted sum of R1, R2 and R3, the deviation of displacement, velocity and acceleration; R4 is the inequality constraint, which imposes an upper limit on the gains.

def optimize(self):
"""
Run an optimization
"""
obj = [self.R1,self.R2,self.R3]
wt  = [1,1,1]
self.opt = Optimizer (
label            = "Optimize RMS2",
objective        = obj,
weights          = wt,
ineqConstraints  = self.R4,
plot             = False,
dsa              = 'FD',
simFunction      = simulate_function_pid,
outputMode       = 'FileOnly',
)

return self.opt.optimize()


Here we pass in a simulate function instead of specifying end, dtout, type in the optimizer. This deactivates the force during the simulation.


def  simulate_function_pid(model):
run = model.simulate(end=10, dtout=0.01, returnResults=True, output="Off")
model.force.active = False
run = model.simulate(end=100, dtout=0.01, returnResults=True,output="Off")
model.force.active = True

return run