如何在 Dymos 中将数量的积分指定为 objective
How to specify integration of a quantity as an objective in Dymos
我正在实施 Bryson-Denham 问题。 objective 是:
$J=\frac{1}{2}\int_{0}^{1}u^2\left(t\right)dt$
并且在 Dymos 的文档中,所有解释和示例都将 objective 值声明为 loc=initial 或 loc=final 处的标量。我找不到任何使用一段时间内某个数量的积分作为 objective 函数的示例。这可能吗?我该如何实施?
仅供参考,Bryson-Denham 问题在此页面中得到了很好的解释:
https://www.gpops2.com/Examples/Bryson-Denham.html
Dymos 将整合您提供的任何状态。在这种情况下,您需要为 J 添加状态,然后还为其计算状态率 --- J_dot.
import openmdao.api as om
import dymos as dm
class BrysonDedhamODE(om.ExplicitComponent):
def initialize(self):
self.options.declare('num_nodes', types=int)
def setup(self):
nn = self.options['num_nodes']
# static parameters
self.add_input('x', shape=nn)
self.add_input('v', shape=nn)
self.add_input('u', shape=nn)
self.add_input('J', shape=nn)
# state rates
self.add_output('x_dot', shape=nn, tags=['dymos.state_rate_source:x'])
self.add_output('v_dot', shape=nn, tags=['dymos.state_rate_source:v'])
self.add_output('J_dot', shape=nn, tags=['dymos.state_rate_source:J'])
# Ask OpenMDAO to compute the partial derivatives using complex-step
# with a partial coloring algorithm for improved performance, and use
# a graph coloring algorithm to automatically detect the sparsity pattern.
self.declare_coloring(wrt='*', method='cs')
def compute(self, inputs, outputs):
v, u, j = inputs["v"], inputs["u"], inputs["J"]
outputs['x_dot'] = v
outputs['v_dot'] = u
outputs['J_dot'] = 0.5*u**2
p = om.Problem()
p.driver = om.pyOptSparseDriver()
p.driver.options['optimizer'] = 'SLSQP'
p.driver.declare_coloring()
traj = p.model.add_subsystem('traj', dm.Trajectory())
transcription = dm.Radau(num_segments=10, order=3, compressed=True)
phase0 = dm.Phase(ode_class=BrysonDedhamODE, transcription=transcription)
traj.add_phase('phase0', phase0)
phase0.set_time_options(fix_initial=True, fix_duration=True)
phase0.set_state_options("x", fix_initial=True, fix_final=True, lower=0, upper=2)
phase0.set_state_options("v", fix_initial=True, fix_final=True, lower=-2, upper=2)
phase0.set_state_options("J", fix_initial=False, fix_final=False,lower=-10, upper=10)
phase0.add_control('u', lower=-10, upper=10)
phase0.add_path_constraint('x', upper=1/9.)
phase0.add_objective('J', loc="final")
p.setup()
#initial conditions
p['traj.phase0.states:x'] = phase0.interp('x', [0,0])
p['traj.phase0.states:x'] = phase0.interp('x', [0,0])
p['traj.phase0.states:v'] = phase0.interp('v', [1,-1])
p['traj.phase0.t_duration'] = 1
p['traj.phase0.t_initial'] = 0
dm.run_problem(p, make_plots=True)
我正在实施 Bryson-Denham 问题。 objective 是:
$J=\frac{1}{2}\int_{0}^{1}u^2\left(t\right)dt$
并且在 Dymos 的文档中,所有解释和示例都将 objective 值声明为 loc=initial 或 loc=final 处的标量。我找不到任何使用一段时间内某个数量的积分作为 objective 函数的示例。这可能吗?我该如何实施?
仅供参考,Bryson-Denham 问题在此页面中得到了很好的解释: https://www.gpops2.com/Examples/Bryson-Denham.html
Dymos 将整合您提供的任何状态。在这种情况下,您需要为 J 添加状态,然后还为其计算状态率 --- J_dot.
import openmdao.api as om
import dymos as dm
class BrysonDedhamODE(om.ExplicitComponent):
def initialize(self):
self.options.declare('num_nodes', types=int)
def setup(self):
nn = self.options['num_nodes']
# static parameters
self.add_input('x', shape=nn)
self.add_input('v', shape=nn)
self.add_input('u', shape=nn)
self.add_input('J', shape=nn)
# state rates
self.add_output('x_dot', shape=nn, tags=['dymos.state_rate_source:x'])
self.add_output('v_dot', shape=nn, tags=['dymos.state_rate_source:v'])
self.add_output('J_dot', shape=nn, tags=['dymos.state_rate_source:J'])
# Ask OpenMDAO to compute the partial derivatives using complex-step
# with a partial coloring algorithm for improved performance, and use
# a graph coloring algorithm to automatically detect the sparsity pattern.
self.declare_coloring(wrt='*', method='cs')
def compute(self, inputs, outputs):
v, u, j = inputs["v"], inputs["u"], inputs["J"]
outputs['x_dot'] = v
outputs['v_dot'] = u
outputs['J_dot'] = 0.5*u**2
p = om.Problem()
p.driver = om.pyOptSparseDriver()
p.driver.options['optimizer'] = 'SLSQP'
p.driver.declare_coloring()
traj = p.model.add_subsystem('traj', dm.Trajectory())
transcription = dm.Radau(num_segments=10, order=3, compressed=True)
phase0 = dm.Phase(ode_class=BrysonDedhamODE, transcription=transcription)
traj.add_phase('phase0', phase0)
phase0.set_time_options(fix_initial=True, fix_duration=True)
phase0.set_state_options("x", fix_initial=True, fix_final=True, lower=0, upper=2)
phase0.set_state_options("v", fix_initial=True, fix_final=True, lower=-2, upper=2)
phase0.set_state_options("J", fix_initial=False, fix_final=False,lower=-10, upper=10)
phase0.add_control('u', lower=-10, upper=10)
phase0.add_path_constraint('x', upper=1/9.)
phase0.add_objective('J', loc="final")
p.setup()
#initial conditions
p['traj.phase0.states:x'] = phase0.interp('x', [0,0])
p['traj.phase0.states:x'] = phase0.interp('x', [0,0])
p['traj.phase0.states:v'] = phase0.interp('v', [1,-1])
p['traj.phase0.t_duration'] = 1
p['traj.phase0.t_initial'] = 0
dm.run_problem(p, make_plots=True)