Moco求解最优化问题使用教程

news2024/9/21 14:46:42

理论部分

最简单的例子,流程

输出结果分析

理论部分

moco最终是调用CasAdi求解器来进行求解

对不常见的几个符号表达式含义进行解释:

多刚体动力学公式代表系统中,f_inertial (惯性力和科里奥利力);f_app (外力和接触力);拉格朗日乘子λ,用于在求解包含约束的动力学问题时,找到满足所有约束条件的广义约束力。;辅助动力学 (Auxiliary Dynamics)指从关节到肌肉求解过程中,肌肉的动力学模型部分;位置级(全约束)运动学约束 (Position-level (Holonomic) Kinematic Constraints),p位置,q速度;运动学约束雅可比矩阵 (Kinematic Constraint Jacobian G),一般用J表示;

路径约束,g: 表示代数路径约束的函数,与边界约束不同,路径约束在整个运动路径上都是有效的,而不仅仅是在特定的时间点或状态点上;Cost中,含有初始和最终时间t,初始和最终状态y,控制输入x,运动学约束乘子(用于计算关节力),时间不变参数(如物理常数、系统参数等)p,积分项 (Integral Terms)Sc,j 和 Sb,k,Sc,j表示控制输入的平方积分(评估控制量),Sb,k可能表示与边界约束违反程度相关的积分项;

边界约束与路径约束的区分:路径约束函数包含t,是动态变化的,更加复杂,与系统的当前状态、历史状态和未来状态相关;边界约束是硬约束确保系统稳定性,路径约束可以被视为软性;

最简单的例子,流程

官方给出一个最简单的质点一维运动的实力,要求质量小球在规定的时间以规定的速度到达规定的最终位置。包含了,建模,问题配置,求解以及可视化整套流程,主要使用方法在注释中:

import os
import opensim as osim

# 加载模型
model = osim.Model()
# 模型名称
model.setName('sliding_mass')
# 设定重力,设置模型中的重力向量,9.81为竖直向下
model.set_gravity(osim.Vec3(9.81, 0, 0))
# 创建一个新的刚体对象,刚体名称是body,质量为2kg,刚体质心坐标(0,0,0),主惯性矩
Ixx = 2 * (0.5 * 2.0 * 0.1**2)  # 假设的x轴惯性矩  
Iyy = Ixx                 # 假设的y轴惯性矩,与x轴相同  
Izz = 0.5 * 2.0 * 0.1**2        # z轴(圆柱体中心轴)的惯性矩  
Ixy = 0                        # 无x-y平面内的不对称性  
Ixz = 0                        # 无x-z平面内的不对称性
Iyz = 0                        # 无y-z平面内的不对称性
body_inertia = osim.Inertia(Ixx, Iyy, Izz, Ixy, Ixz, Iyz)  
# body = osim.Body('body', 2.0, osim.Vec3(0), osim.Inertia(0))
#
body = osim.Body('body', 2.0, osim.Vec3(0,0,0), body_inertia)
# 将定义好的刚体添加到模型中
model.addComponent(body)

# 添加滑动关节,配置关节参数 Allows translation along x.
# 两个刚体之间沿着一个轴进行相对滑动。
# 指定关节的名称为slider、父刚体为getGround和子刚体(要连接的刚体)。
joint = osim.SliderJoint('slider', model.getGround(), body)
# 获取与关节相关联的坐标对象的引用
coord = joint.updCoordinate()
# 设置坐标的名称为position
coord.setName('position')
# 将定义好的关节添加到模型中
model.addComponent(joint)

# 添加致动元件,设置名称,
actu = osim.CoordinateActuator()
# 作用在关节坐标上
actu.setCoordinate(coord)
actu.setName('actuator')
# 肌肉或致动器在特定条件下能够产生的最大力量的一个参数
actu.setOptimalForce(1)
# 将定义好的致动元件添加到模型中
model.addComponent(actu)
# 将刚体的外形显示成0.05的一个球体
body.attachGeometry(osim.Sphere(0.05))
# 模型的所有组件(如骨骼、关节、肌肉、力等)都被添加到模型中之后调用
# 内部数据结构被初始化,准备进行模拟
model.finalizeConnections()

# 创建一个study对象
study = osim.MocoStudy()
study.setName('sliding_mass')

# 从study对象获取problem对象的引用
problem = study.updProblem()

# 将一个已构建的模型(model)关联或设置到一个特定的问题(problem)实例中
problem.setModel(model)


# 初始时间必须为0,最终时间可以在[0,5]范围内
problem.setTimeBounds(osim.MocoInitialBounds(0.), osim.MocoFinalBounds(0., 5.))

# 设定状态约束,状态位置范围MocoBounds上下界限为[-5,5]
# 初始位置为0,最终位置为1
problem.setStateInfo('/slider/position/value', osim.MocoBounds(-5, 5),
                     osim.MocoInitialBounds(0), osim.MocoFinalBounds(1))
# 设定状态约束,速度必须在[-50,50]以内。
# 初始和最终速度必须为0,此处使用简洁语法。
problem.setStateInfo('/slider/position/speed', [-50, 50], [0], [-3])

# 设定控制量约束,控制力上下界限为[-50, 50].
problem.setControlInfo('/actuator', osim.MocoBounds(-50, 50))

# Cost.该目标旨在最小化仿真的最终时间,给定约束下,使得完成任务所需时间最短
problem.addGoal(osim.MocoFinalTimeGoal())

# 配置求解器
# =====================
solver = study.initCasADiSolver()
# 设置求解过程中网格数量为100,对时间或某个参数进行离散化的数量
solver.set_num_mesh_intervals(100)

# 将配置好的study对象的配置,输出到.omoco文件
study.printToXML('sliding_mass.omoco')

# 求解
# ==================
solution = study.solve()
# 将求解结果输出到.sto文件中
# 包含time;状态量:/slider/position/value;/slider/position/speed;
# 控制量:/actuator
solution.write('sliding_mass_solution.sto')

# 检查环境变量 OPENSIM_USE_VISUALIZER,其值不是 '0',通过可视化工具(如OpenSim的Visualizer)来查看结果。
if os.getenv('OPENSIM_USE_VISUALIZER') != '0':
    study.visualize(solution);

输出结果分析

程序输出两个文件,sliding_mass.omoco与sliding_mass_solution.sto

sliding_mass.omoco是配置文件,是对应代码上面配置生成,条目比代码中要多出一些默认项

<?xml version="1.0" encoding="UTF-8" ?>
<OpenSimDocument Version="40500">
	<MocoStudy name="sliding_mass">
		<!--The optimal control problem to solve.-->
		<MocoProblem name="problem">
			<!--List of 1 or more MocoPhases.-->
			<MocoPhase name="phases">
				<!--OpenSim Model to provide dynamics.-->
				<ModelProcessor name="model">
					<!--Base model to process.-->
					<model>
						<Model name="sliding_mass">
							<!--List of components that this component owns and serializes.-->
							<components>
								<Body name="body">
									<!--The geometry used to display the axes of this Frame.-->
									<FrameGeometry name="frame_geometry">
										<!--Path to a Component that satisfies the Socket 'frame' of type Frame.-->
										<socket_frame>..</socket_frame>
										<!--Scale factors in X, Y, Z directions respectively.-->
										<scale_factors>0.20000000000000001 0.20000000000000001 0.20000000000000001</scale_factors>
									</FrameGeometry>
									<!--List of geometry attached to this Frame. Note, the geometry are treated as fixed to the frame and they share the transform of the frame when visualized-->
									<attached_geometry>
										<Sphere name="body_geom_1">
											<!--Path to a Component that satisfies the Socket 'frame' of type Frame.-->
											<socket_frame>..</socket_frame>
											<!--Radius of sphere, defaults to 1.0-->
											<radius>0.050000000000000003</radius>
										</Sphere>
									</attached_geometry>
									<!--The mass of the body (kg)-->
									<mass>2</mass>
									<!--The location (Vec3) of the mass center in the body frame.-->
									<mass_center>0 0 0</mass_center>
									<!--The elements of the inertia tensor (Vec6) as [Ixx Iyy Izz Ixy Ixz Iyz] measured about the mass_center and not the body origin.-->
									<inertia>0.020000000000000004 0.020000000000000004 0.010000000000000002 0 0 0</inertia>
								</Body>
								<SliderJoint name="slider">
									<!--Path to a Component that satisfies the Socket 'parent_frame' of type PhysicalFrame (description: The parent frame for the joint.).-->
									<socket_parent_frame>/ground</socket_parent_frame>
									<!--Path to a Component that satisfies the Socket 'child_frame' of type PhysicalFrame (description: The child frame for the joint.).-->
									<socket_child_frame>/body</socket_child_frame>
									<!--List containing the generalized coordinates (q's) that parameterize this joint.-->
									<coordinates>
										<Coordinate name="position">
											<!--All properties of this object have their default values.-->
										</Coordinate>
									</coordinates>
								</SliderJoint>
								<CoordinateActuator name="actuator">
									<!--Name of the generalized coordinate to which the actuator applies.-->
									<coordinate>position</coordinate>
									<!--The maximum generalized force produced by this actuator.-->
									<optimal_force>1</optimal_force>
								</CoordinateActuator>
							</components>
							<!--The model's ground reference frame.-->
							<Ground name="ground">
								<!--The geometry used to display the axes of this Frame.-->
								<FrameGeometry name="frame_geometry">
									<!--Path to a Component that satisfies the Socket 'frame' of type Frame.-->
									<socket_frame>..</socket_frame>
									<!--Scale factors in X, Y, Z directions respectively.-->
									<scale_factors>0.20000000000000001 0.20000000000000001 0.20000000000000001</scale_factors>
								</FrameGeometry>
							</Ground>
							<!--Acceleration due to gravity, expressed in ground.-->
							<gravity>9.8100000000000005 0 0</gravity>
							<!--List of joints that connect the bodies.-->
							<JointSet name="jointset">
								<objects />
								<groups />
							</JointSet>
							<!--Controllers that provide the control inputs for Actuators.-->
							<ControllerSet name="controllerset">
								<objects />
								<groups />
							</ControllerSet>
							<!--Forces in the model (includes Actuators).-->
							<ForceSet name="forceset">
								<objects />
								<groups />
							</ForceSet>
						</Model>
					</model>
				</ModelProcessor>
				<!--Bounds on initial value.-->
				<MocoInitialBounds name="time_initial_bounds">
					<!--1 value: required value. 2 values: lower, upper bounds on value.-->
					<bounds>0</bounds>
				</MocoInitialBounds>
				<!--Bounds on final value.-->
				<MocoFinalBounds name="time_final_bounds">
					<!--1 value: required value. 2 values: lower, upper bounds on value.-->
					<bounds>0 5</bounds>
				</MocoFinalBounds>
				<!--The state variables' bounds.-->
				<state_infos>
					<MocoVariableInfo name="/slider/position/value">
						<!--1 value: required value over all time. 2 values: lower, upper bounds on value over all time.-->
						<bounds>-5 5</bounds>
						<!--1 value: required initial value. 2 values: lower, upper bounds on initial value.-->
						<initial_bounds>0</initial_bounds>
						<!--1 value: required final value. 2 values: lower, upper bounds on final value.-->
						<final_bounds>1</final_bounds>
					</MocoVariableInfo>
					<MocoVariableInfo name="/slider/position/speed">
						<!--1 value: required value over all time. 2 values: lower, upper bounds on value over all time.-->
						<bounds>-50 50</bounds>
						<!--1 value: required initial value. 2 values: lower, upper bounds on initial value.-->
						<initial_bounds>0</initial_bounds>
						<!--1 value: required final value. 2 values: lower, upper bounds on final value.-->
						<final_bounds>-3</final_bounds>
					</MocoVariableInfo>
				</state_infos>
				<!--The control variables' bounds.-->
				<control_infos>
					<MocoVariableInfo name="/actuator">
						<!--1 value: required value over all time. 2 values: lower, upper bounds on value over all time.-->
						<bounds>-50 50</bounds>
						<!--1 value: required initial value. 2 values: lower, upper bounds on initial value.-->
						<initial_bounds></initial_bounds>
						<!--1 value: required final value. 2 values: lower, upper bounds on final value.-->
						<final_bounds></final_bounds>
					</MocoVariableInfo>
				</control_infos>
				<!--Integral/endpoint quantities to minimize or constrain.-->
				<goals>
					<MocoFinalTimeGoal name="goal">
						<!--All properties of this object have their default values.-->
					</MocoFinalTimeGoal>
				</goals>
			</MocoPhase>
		</MocoProblem>
		<!--The optimal control algorithm for solving the problem.-->
		<MocoCasADiSolver name="solver">
			<!--The number of uniformly-sized mesh intervals for the problem (default: 100). If a non-uniform mesh exists, the non-uniform mesh is used instead.-->
			<num_mesh_intervals>100</num_mesh_intervals>
		</MocoCasADiSolver>
	</MocoStudy>
</OpenSimDocument>

sliding_mass_solution.sto包含一个head信息,和数据主体内容,包含了时间,状态量,控制量三列信息,与代码中的配置相一致

下面是head信息,包含了一些基本内容

objective_goal=0.692622        目标函数的数值大小
solver_duration=8.912633        求解时间
status=Solve_Succeeded        求解成功
success=true                            求解成功的标志位
DataType=double                     时间,状态量,控制量的数据类型
version=3
OpenSimVersion=4.5
endheader

本文来自互联网用户投稿,该文观点仅代表作者本人,不代表本站立场。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如若转载,请注明出处:http://www.coloradmin.cn/o/2082186.html

如若内容造成侵权/违法违规/事实不符,请联系多彩编程网进行投诉反馈,一经查实,立即删除!

相关文章

SQL注入-ctfshow

首先还是对sql的具体分析和讲解 原理&#xff1a; SQL注入是一种安全漏洞&#xff0c;它允许攻击者通过在应用程序的输入中插入或者操作SQL命令来改变后端数据库的查询和操作。SQL注入的主要原因是代码中直接将用户输入与SQL命令拼接在一起&#xff0c;没有进行适当的验证或清…

网络安全可以从事哪些岗位岗位职责是什么网络安全专业的就业前景

网络安全可以从事哪些岗位 伴随着社会的发展&#xff0c;网络安全被列为国家安全战略的一部分&#xff0c;因此越来越多的行业开始迫切需要网安人员&#xff0c;也有不少人转行学习网络安全。那么网络安全可以从事哪些岗位?岗位职责是什么?相信很多人都不太了解&#xff0c;…

『功能项目』着色器光透魔法球Shaders【09】

我们打开上一篇08技能释放的项目&#xff0c; 本章要做的事情是为魔法球增添一个光透效果shaders。 首先在Assets中创建一个Shaders文件夹 接着将场景中的灯光调暗一些&#xff08;避免灯光太强压过将要设置半透明光透效果的魔法球&#xff09; 将新Resources中的Shpere拖拽至…

MQTT服务器-mosquitto配置

我们要使用ESP8266使得STM32能够和服务器之间传递数据&#xff0c;需要有一台MQTT服务器。当然读者可以使用腾讯云、阿里云、OneNet等平台提供的MQTT服务更方便一些。 逻辑是这样的&#xff1a;我们首先需要一台服务器A作为中转站&#xff0c;然后我们的STM32作为客户端能够发…

Java接口中的长连接与短连接详解:概念、应用场景及实现

个人名片 &#x1f393;作者简介&#xff1a;java领域优质创作者 &#x1f310;个人主页&#xff1a;码农阿豪 &#x1f4de;工作室&#xff1a;新空间代码工作室&#xff08;提供各种软件服务&#xff09; &#x1f48c;个人邮箱&#xff1a;[2435024119qq.com] &#x1f4f1…

2024年十款好用的图纸加密软件推荐|有效的图纸加密方法分享

在数字化时代&#xff0c;图纸作为企业的重要资产&#xff0c;其安全性显得尤为重要。随着技术的不断进步&#xff0c;市场上涌现出多款优秀的图纸加密软件&#xff0c;为企业和个人提供了强有力的安全保障。本文将为您推荐2024年十款好用的图纸加密软件。 1.安秉图纸加密软件…

JavaSE 面试题 46-50

使用runnable需重写run方法&#xff0c;而且返回值为viod型&#xff0c;等于没有返回值&#xff1b; Thread 类在调用 start()函数后就是执行的 是 Runnable 的 run()函数。 callable需重写call方法&#xff0c;call方法可以有返回值&#xff0c;支持泛型而且可以捕获解决异常&…

秋招突击——知识复习——HTTP/2、HTTP/3的改良

文章目录 引言正文HTTP/1.1与HTTP/1.01、长连接代替短链接2、管道传输缺点 HTTP2.0和HTTP1.11、头部压缩2、二进制格式3、并发传输4、服务器主动推送资源缺点 HTTP/3和HTTP/21、无队头阻塞2、更快的连接建立3、连接迁移 面试题1、HTTP是长连接还是短链接&#xff1f;2、HTTP长连…

中年男明星们,正在视频号“收割”50+姐姐

还记得几年前&#xff0c;抖音上许多大妈和“靳东谈恋爱”的事情吗&#xff1f; 尽管那些靳东高仿号&#xff0c;发的是花花绿绿、粗制滥造的视频&#xff0c;明眼人一看就知真假&#xff0c;但仍有众多大妈痴迷。 如今&#xff0c;在视频号上也有一群姐姐们&#xff0c;“迷恋…

模块化沙箱有几种类型?各类模块化沙箱的功能是什么?

模块化沙箱有几种类型&#xff1f;各类模块化沙箱的功能是什么&#xff1f; 模块化沙箱是一种高灵活性和高扩展性的数据安全产品&#xff0c;通过选择不同的沙箱模块&#xff0c;满足不同的安全需求。 模块化沙箱是SDC沙箱的几种表现形式的总称&#xff0c;模块化沙箱总共分为…

Kubemetes高级调度

一组特殊的容器 初始化容器是用来进行初始化操作的&#xff0c;在很多情况下&#xff0c;程序的启动需要依赖各类配置&#xff0c;资源&#xff0c;但是又不能继承在原有的启动命令或者镜像当中&#xff0c;因为程序的镜像可能并没有加载配置命令&#xff0c;此时InitContaine…

Dubbo ZooKeeper Spring Boot整合

依赖配置 1. Dubbo 起步依赖 Dubbo 是一款高性能的 Java RPC 框架&#xff0c;用于快速开发高性能的服务。 <dependency><groupId>org.apache.dubbo</groupId><artifactId>dubbo-spring-boot-starter</artifactId><version>${dubbo.ver…

Linux中的常见命令——用户管理命令

1、useradd添加新用户 基本语法 语法功能描述useradd 用户名添加新用户useradd -g 组名 用户名添加新用户到某个组 实操案例 1、添加一个新用户【此时的用户是没有密码的】 [rootcentos100 ~]# cd /home [rootcentos100 home]# ls www zss [rootcentos100 home]# useradd…

如果自学通过PMP?

自学&#xff1f;现在没有自学通道啦&#xff0c;要通过有R.E.P授权的机构学习&#xff0c;获得35个PDU才能报考哦~ 所以要报培训班~ 一是&#xff0c;PMP 官方的报考条件需要35个PDU&#xff0c;就是要报机构学习后获得。个人报考渠道去年就关闭了&#xff0c;只能通过机构报…

算法-初阶

文章目录 -1.C 标准0.语法基础 1. C头文件2. C命名空间3. 主函数4. 变量类型5. ASCII码6. 注释1.顺序结构 一、代码示例二、例题1&#xff1a;求圆的面积三、例题2&#xff1a;求解一元二次方程四、总结&#xff1a;2.分支结构 一、代码示例二、例题1&#xff1a;判断一个数是否…

必看 | CDP盘活存量客户的5大步骤和3个应用案例

​“我们有几十家门店&#xff0c;也有APP、小程序和网站&#xff0c;客户信息散落在各系统中&#xff0c;比较杂乱&#xff0c;一直没有统一去维护&#xff0c;现在想把存量客户运营起来&#xff0c;你们CDP能做吗&#xff1f;”类似的咨询&#xff0c;我们几乎每天都会接到。…

工业三防平板全面提升工厂的工作效率

在当今高度自动化和智能化的工业时代&#xff0c;工厂的工作效率成为了企业竞争力的关键因素。而工业三防平板的出现&#xff0c;犹如一颗璀璨的新星&#xff0c;为工厂带来了全新的变革&#xff0c;全面提升了工厂的工作效率。 工业三防平板具备防水、防尘、防摔的特性无论是潮…

基于 Redis 的 HyperLogLog 实现了 UV 的统计

文章目录 前言HyperLogLog 简介HyperLogLog 的工作原理例子总结 前言 在现代网站开发中&#xff0c;用户行为分析是一个非常重要的环节。其中&#xff0c;UV&#xff08;Unique Visitor&#xff0c;独立访客&#xff09;和PV&#xff08;Page View&#xff0c;页面浏览量&…

10 Java数据结构(下):集合进阶之Map(双列集合)系列:

文章目录 前言一、 Map(双列集合)接口1 常用方法2 遍历方式(1)使用map.keySet()方法(2)使用map.entrySet()方法(3)lambda表达式遍历(这个最简单)3 实现类:HashMap、LinkedHashMap、TreeMap(1)HashMap---- HashMap的特点---- HashMap的底层原理---- 特别注意:自定义…

Spring之SpringSecurity

SpringSecurity相关 一、SpringSecurity简介二、SpringSecurity主要功能三、SpringSecurity的Maven依赖四、Security本质&#xff1a;过滤器链五、用户认证1、根据用户实体&#xff0c;封装一个UserDetails实体对象LoginUser类2、自定义UserDetailsService接口的实现 一、Sprin…