簡體   English   中英

有沒有辦法將方程式實現為 Dymos 路徑約束?

[英]Is there a way to implement equations as Dymos path constraints?

例如,如果我有一個 function h_max(mach) 並且我希望高度在整個飛行包絡線中始終遵循這種預定義的高度 - 馬赫關系,我怎么能實現這一點?

我嘗試將限制數量(在本例中為 h_max)計算為它自己的 state,然后將另一個 state 計算為 h_max-h,然后通過路徑約束將其限制為大於 0。這種方法有效,但涉及兩個顯式組件,一組和大量額外編碼只是為了讓約束起作用。 我想知道是否有更好的方法?

非常感謝。

Dymos 的下一個版本 1.7.0 將很快發布並將支持此功能。

同時,您可以直接從 github 安裝最新開發版的 Dymos 以獲得此功能:

python -m pip install git+https://github.com/OpenMDAO/dymos.git

然后,您可以使用方程定義邊界和路徑約束。 請注意,方程式中必須有一個等號,然后lowerupperequals將應用於方程式的結果。

實際上,dymos 只是在幕后為您插入一個 OpenMDAO ExecComp,因此需要注意的是您的表達式必須與復雜步驟微分兼容。

下面是一個使用約束表達式將最終y值設置為特定值同時滿足用第二個方程定義的路徑約束的最速記時器示例。

import openmdao.api as om
import dymos as dm
from dymos.examples.plotting import plot_results
from dymos.examples.brachistochrone import BrachistochroneODE
import matplotlib.pyplot as plt

#
# Initialize the Problem and the optimization driver
#
p = om.Problem(model=om.Group())
p.driver = om.ScipyOptimizeDriver()
p.driver.declare_coloring()

#
# Create a trajectory and add a phase to it
#
traj = p.model.add_subsystem('traj', dm.Trajectory())

phase = traj.add_phase('phase0',
                       dm.Phase(ode_class=BrachistochroneODE,
                                transcription=dm.GaussLobatto(num_segments=10)))

#
# Set the variables
#
phase.set_time_options(fix_initial=True, duration_bounds=(.5, 10))

phase.add_state('x', fix_initial=True, fix_final=True)

phase.add_state('y', fix_initial=True, fix_final=False)

phase.add_state('v', fix_initial=True, fix_final=False)

phase.add_control('theta', continuity=True, rate_continuity=True,
                  units='deg', lower=0.01, upper=179.9)

phase.add_parameter('g', units='m/s**2', val=9.80665)

Y_FINAL = 5.0
Y_MIN = 5.0
phase.add_boundary_constraint(f'bcf_y = y - {Y_FINAL}', loc='final', equals=0.0)
phase.add_path_constraint(f'path_y = y - {Y_MIN}', lower=0.0)

#
# Minimize time at the end of the phase
#
phase.add_objective('time', loc='final', scaler=10)

p.model.linear_solver = om.DirectSolver()

#
# Setup the Problem
#
p.setup()

#
# Set the initial values
#
p['traj.phase0.t_initial'] = 0.0
p['traj.phase0.t_duration'] = 2.0

p.set_val('traj.phase0.states:x', phase.interp('x', ys=[0, 10]))
p.set_val('traj.phase0.states:y', phase.interp('y', ys=[10, 5]))
p.set_val('traj.phase0.states:v', phase.interp('v', ys=[0, 9.9]))
p.set_val('traj.phase0.controls:theta', phase.interp('theta', ys=[5, 100.5]))

#
# Solve for the optimal trajectory
#
dm.run_problem(p)

# Check the results
print('final time')
print(p.get_val('traj.phase0.timeseries.time')[-1])

p.list_problem_vars()

請注意來自timeseries_exec_complist_problem_vars()調用的約束 - 這是 Dymos 自動為您插入的 OpenMDAO ExecComp。


--- Constraint Report [traj] ---
    --- phase0 ---
        [final]   0.0000e+00 == bcf_y [None]
        [path]    0.0000e+00 <= path_y  [None]

/usr/local/lib/python3.8/dist-packages/openmdao/recorders/sqlite_recorder.py:227: UserWarning:The existing case recorder file, dymos_solution.db, is being overwritten.
Model viewer data has already been recorded for Driver.
Full total jacobian was computed 3 times, taking 0.057485 seconds.
Total jacobian shape: (71, 51) 


Jacobian shape: (71, 51)  (12.51% nonzero)
FWD solves: 12   REV solves: 0
Total colors vs. total size: 12 vs 51  (76.5% improvement)

Sparsity computed using tolerance: 1e-25
Time to compute sparsity: 0.057485 sec.
Time to compute coloring: 0.054118 sec.
Memory to compute coloring: 0.000000 MB.
/usr/local/lib/python3.8/dist-packages/openmdao/core/total_jac.py:1585: DerivativesWarning:Constraints or objectives [('traj.phases.phase0.timeseries.timeseries_exec_comp.path_y', inds=[(0, 0)])] cannot be impacted by the design variables of the problem.
Optimization terminated successfully    (Exit mode 0)
            Current function value: [18.02999766]
            Iterations: 14
            Function evaluations: 14
            Gradient evaluations: 14
Optimization Complete
-----------------------------------
final time
[1.80299977]
----------------
Design Variables
----------------
name                        val             size  indices                                        
--------------------------  --------------  ----  --------------------------------------------- 
traj.phase0.t_duration      [1.80299977]    1     None                                           
traj.phase0.states:x        |12.14992234|   9     [1 2 3 4 5 6 7 8 9]                            
traj.phase0.states:y        |22.69124774|   10    [ 1  2  3  4  5  6  7  8  9 10]                
traj.phase0.states:v        |24.46289861|   10    [ 1  2  3  4  5  6  7  8  9 10]                
traj.phase0.controls:theta  |266.48489386|  21    [ 0  1  2  3  4  5   ... 4 15 16 17 18 19 20]  

-----------
Constraints
-----------
name                                                         val            size  indices                                        alias                                                 
-----------------------------------------------------------  -------------  ----  ---------------------------------------------  ---------------------------------------------------- 
timeseries.timeseries_exec_comp.bcf_y                        [0.]           1     [29]                                           traj.phases.phase0->final_boundary_constraint->bcf_y  
timeseries.timeseries_exec_comp.path_y                       |15.73297378|  30    [ 0  1  2  3  4  5   ... 3 24 25 26 27 28 29]  traj.phases.phase0->path_constraint->path_y           
traj.phase0.collocation_constraint.defects:x                 |6e-08|        10    None                                           None                                                  
traj.phase0.collocation_constraint.defects:y                 |7e-08|        10    None                                           None                                                  
traj.phase0.collocation_constraint.defects:v                 |3e-08|        10    None                                           None                                                  
traj.phase0.continuity_comp.defect_control_rates:theta_rate  |0.0|          9     None                                           None                                                  

----------
Objectives
----------
name           val            size  indices  
-------------  -------------  ----  ------- 
traj.phase0.t  [18.02999766]  1     -1       

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM