1

我正在实施 Bryson-Denham 问题。目标是:

$J=\frac{1}{2}\int_{0}^{1}u^2\left(t\right)dt$

在 Dymos 的文档中,所有解释和示例都将目标值声明为 loc=initial 或 loc=final 处的标量。我找不到任何使用随时间积分作为目标函数的示例。这可能吗?我该如何实施?

仅供参考,Bryson-Denham 问题在此页面中得到了很好的解释: https ://www.gpops2.com/Examples/Bryson-Denham.html

4

1 回答 1

1

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)

v 的状态历史 x 的状态历史 你的控制历史

于 2021-12-16T21:18:15.393 回答