- html - 出于某种原因,IE8 对我的 Sass 文件中继承的 html5 CSS 不友好?
- JMeter 在响应断言中使用 span 标签的问题
- html - 在 :hover and :active? 上具有不同效果的 CSS 动画
- html - 相对于居中的 html 内容固定的 CSS 重复背景?
我想用Dymos解决最优控制问题:
服从动力系统:
我有两个问题:
(1)如何设置V(X(T))的目标函数(object1
),其中最终时间点的状态变量值是函数V的输入。
我觉得设置
phase.add_objective('V(X)', loc='final', scale=1)
将最小化速率为 V(X) 的变量从 t=0 到 t=T 的积分,对吗?
(2) 我想我可以设置一个时间变化率 L(X, u) 的输出 objectval2
然后设置
phase.add_objective('objectval2', loc='final', scale=1)
那么我如何让优化器知道我想要最小化 objectval1 + objectval2 的总和,而不是 objectval1*objectval2 或 objectval1 和 objectval2 的任何其他函数。
我阅读了教程,但没有找到与我的案例相似的示例。
#################
评论:谢谢。我不能在答案的评论中添加以下澄清问题,它说这些太长了。所以我在这里添加了它们。
我认为这里的关键代码是:
p.model.add_subsystem('other_calcs', om.ExecComp("f=a+b"))
p.model.connect('traj.phase0.timeseries.states:v', 'other_calcs.a', src_indices=[-1])
p.model.connect('traj.pahse0.timeseries.states:v', 'other_calcs.b', src_indices=[0])
我在教程中看到这个,我有 3 个问题:
(1)对于关键字输入src_indices
,我认为src_indices=[-1]
传递了v
的最后一个值(v[-1]
) 到 a
和 src_indices=[0]
传递 v
( v[0]
) 到 b
。我认为v[-1]
的时间点取决于节点数,节点数由参数num_segments
和order
决定。那么转录函数是否总是将最后一个节点位置设置为最后一个时间点?例如,如果我解决了从 t=0
到 t=2000
的控制问题,无论我如何设置 num_segments
和 顺序
,例如num_segments=2
和 order=3
或 num_segments=20
和 order=5
, v[- 1]
将始终是 t=2000
时 v
的值,而 v[0]
将始终是 v
在 t=0
?
(2) 我可以在节点上添加一个或多个特定的时间点吗?例如,如果我解决一个从 t=0 到 t=2000 的控制问题,我想最小化 t=500
时 v
的值之和,t=1000
和t=2000
,我可以要求节点必须包含时间点t=500
,t=1000
和 t=2000
,无论我如何设置 num_segments
和 order
?
(3) 最后一个问题是我第一次问的目标函数设置。在示例中,您注释掉了 # phase.add_objective('time', loc='final')
。如果我设置代码,优化器会做什么:
phase.add_objective('x', loc='final')
p.model.add_objective('other_calcs.f')
优化器是否最小化从t=0
到t=T
和v(T)<的
?如果不是,我如何设置优化器以最小化从 x
积分之和t=0
到 t=T
和 v 的
?x
积分总和(T)
最佳答案
使用标准的 Brachistochrone 问题,我对目标函数进行了玩具修改,以向您展示如何实现您的要求。我定义的新目标函数不是一个非常有意义的目标函数,但它明白了这一点。
这里的关键思想是,除了 traj
组之外,您还可以向模型添加额外的组件。这些可以在 traj
组之前或之后出现,但在您的情况下,您希望它们在之后。然后你可以将东西连接到你的其他组件和 traj
组。
我在这里制作了一个非常简单的 ExecComp
,它将状态变量 V 的初始条件和最终条件相加,并将其设置为目标。为此,我连接了该阶段的 timeseries
输出。显然,您可以构建一个对最终时间状态值进行运算的不同方程式。此示例中的连接语句向您展示了如何执行此操作。
import numpy as np
import openmdao.api as om
import dymos as dm
import matplotlib.pyplot as plt
# First define a system which computes the equations of motion
class BrachistochroneEOM(om.ExplicitComponent):
def initialize(self):
self.options.declare('num_nodes', types=int)
def setup(self):
nn = self.options['num_nodes']
# Inputs
self.add_input('v', val=np.zeros(nn), units='m/s', desc='velocity')
self.add_input('theta', val=np.zeros(nn), units='rad', desc='angle of wire')
self.add_output('xdot', val=np.zeros(nn), units='m/s', desc='x rate of change')
self.add_output('ydot', val=np.zeros(nn), units='m/s', desc='y rate of change')
self.add_output('vdot', val=np.zeros(nn), units='m/s**2', desc='v rate of change')
# Ask OpenMDAO to compute the partial derivatives using complex-step
# with a partial coloring algorithm for improved performance
self.declare_partials(of='*', wrt='*', method='cs')
self.declare_coloring(wrt='*', method='cs', show_summary=True)
def compute(self, inputs, outputs):
v, theta = inputs.values()
outputs['vdot'] = 9.80665 * np.cos(theta)
outputs['xdot'] = v * np.sin(theta)
outputs['ydot'] = -v * np.cos(theta)
p = om.Problem()
# Define a Trajectory object
traj = p.model.add_subsystem('traj', dm.Trajectory())
# Define a Dymos Phase object with GaussLobatto Transcription
tx = dm.GaussLobatto(num_segments=10, order=3)
phase = dm.Phase(ode_class=BrachistochroneEOM, transcription=tx)
traj.add_phase(name='phase0', phase=phase)
# Set the time options
phase.set_time_options(fix_initial=True,
duration_bounds=(0.5, 10.0))
# Set the state options
phase.add_state('x', rate_source='xdot',
fix_initial=True, fix_final=True)
phase.add_state('y', rate_source='ydot',
fix_initial=True, fix_final=True)
phase.add_state('v', rate_source='vdot',
fix_initial=True, fix_final=False)
# Define theta as a control.
phase.add_control(name='theta', units='rad',
lower=0, upper=np.pi)
######################################################
# Post trajectory calculations for composite objective
######################################################
p.model.add_subsystem('other_calcs', om.ExecComp("f=v[0]+v[1]", v={"shape":(2,)}))
p.model.connect('traj.phase0.timeseries.states:v', 'other_calcs.v', src_indices=[0,-1])
###################################################################
# Standard Dymos API for objective function - minimize final time.
###################################################################
# phase.add_objective('time', loc='final')
###################################################################
# OpenMDAO API for objective functions that require post-dymos calculations
###################################################################
p.model.add_objective('other_calcs.f')
# Set the driver.
p.driver = om.ScipyOptimizeDriver()
# Allow OpenMDAO to automatically determine total
# derivative sparsity pattern.
# This works in conjunction with partial derivative
# coloring to give a large speedup
p.driver.declare_coloring()
# Setup the problem
p.setup()
# Now that the OpenMDAO problem is setup, we can guess the
# values of time, states, and controls.
p.set_val('traj.phase0.t_duration', 2.0)
# States and controls here use a linearly interpolated
# initial guess along the trajectory.
p.set_val('traj.phase0.states:x',
phase.interp('x', ys=[0, 10]),
units='m')
p.set_val('traj.phase0.states:y',
phase.interp('y', ys=[10, 5]),
units='m')
p.set_val('traj.phase0.states:v',
phase.interp('v', ys=[0, 5]),
units='m/s')
# constant initial guess for control
p.set_val('traj.phase0.controls:theta', 90, units='deg')
# Run the driver to solve the problem and generate default plots of
# state and control values vs time
dm.run_problem(p, make_plots=True, simulate=True)
关于openmdao - 动力 : How can I set object to minimize a function value of state variables at the end of simulation time,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/74497365/
根本不是 SQL 人员。让顾问编写以下代码。 首先,它确保只选择了一所小学 - 然后,在 BEGIN 之后,如果变量 @Term 等于 3,我们想要在 IF 语句下执行操作。问题就在这里。当 @Ter
以下 javascript 将 bool 值呈现到每个语句的右侧: var reg = new RegExp(/^[\w\/].*result\b/); console.log(reg.test('p
有什么区别: x = 1 while x < 5 do x += 1 print x end 和: x = 1 while x < 5 x += 1 print x end 将 do
对于初学者来说,我是编程的“菜鸟”,所以只需了解事情的工作原理并向社区寻求帮助。 但是...我想知道的是: 我想要构建一个 Web 应用程序,两个主要用户界面之一实际上是日历产品。每个日历项目都有 8
我正在尝试制作带有图片上传选项的表单。我正在使用 express-http-proxy作为我的 API 代理和 multer按照建议。 app.use('/api', upload.any(), pr
根据this中的回答和 this问题,C++ 标准在 § 23.2.1 中声明 end() 对于所有 STL 容器都具有恒定的时间复杂度。 如果我理解正确的话: std::forward_list 只
当我使用 css 属性 align-items 时,我看不到 flex-end 值或 end 值有任何视觉差异>. align-items: end 和 align-items: flex-end 有
Sub RowRangeMove() Sheets.Add().Name = "CopySheet" With Sheets("BigDataSet - Copy")
假设第 1 到 5,000 列中有 25,000 到 50,000 行数据,每列可能有不同的行数。所有数据都是连续的,即列中没有空行,也没有空列。 考虑以下代码 Dim i As Long Dim W
我在 MYSQL 中有一个表,必须在 postgresql 中转换它。 我正在使用以下命令创建表格。 create table emp(COMPLETE BOOLEAN NOT NULL, END B
我正在尝试使用 Lark 为 BASIC 创建一个 LALR 解析器,而且我很难解决“END”语句和“END IF”等语句之间的冲突。这是语法的简化版本: %ignore /[ \t\f]+/ pro
试图理解this MSDN sample但我对这些行感到困惑: IAsyncResult result = Dns.BeginGetHostEntry(args[0], null, null); Co
我在 http://www.sgi.com/tech/stl/nth_element.html 阅读了 std::nth_element 的描述 template void nth_element(
为什么标准将 end() 定义为末尾,而不是实际末尾? 最佳答案 最好的论据是Dijkstra himself 提出的论据。 : 您希望范围的大小是一个简单的差异end - begin; 当序列退化为
我试图根据一些参数停止页面的其余部分加载;但不确定语法是否正确。 @if(dayRes + dayTri == 2){Sorry, etc @Response.End} 上面抛出这个错误: CS150
在二分搜索中,我们通常有 low 和 high 变量,并且通常有一个 while 循环来测试 low <= high,如以下代码所示(来自维基百科): int SortedArray[max] = {
我将 MS-Test 与 Visual Studio 2010 和 Visual Basic 结合使用。 在下面的函数中,代码覆盖率告诉我,有一个未检查的 block ,并且带有 “End Try”
所以今天我一直致力于使用 Protractor 为 Angular JS 应用程序设置端到端测试。为了编写更清晰的测试,我使用了 Protractor 网站上描述的 Page Object 模式。 测
所以 meteor js 的全部意义在于允许用户一次对整个堆栈进行编码,但是如果我正在使用像 django 这样的旧框架之一,可以借用meteor js的前端代码吗?比如前端的数据库同步,模板化,或者
我正在使用 wavesurfer.js 和 recorder.js 制作采样器。一切都很顺利,除了我无法使用 play([start[, end]]) 调整循环长度。 wavesurfer.seekT
我是一名优秀的程序员,十分优秀!