【python+ROS+路径规划】五、pyomo非线性规划工具

看到了这篇博客 突然发现python也有mpc工具,所以干脆不用matlab了,直接使用python做mpc的轨迹跟踪。

pyomo的简单了解

pyomo的安装

在VSCode上面安装了半天,各种BUG,最后还是先安装了Ananconda,然后再安装pyomo

在Ubuntu20.04 + Anaconda环境下安装pyomo与ipopt:

conda update conda
conda install -c conda-forge ipopt
conda install -c conda-forge coincbc
conda install -c conda-forge pyomo
conda install -c conda-forge pyomo.extras

Anaconda与ros结合使用

在调好Anaconda之后打开VSCode,使用roscore命令会报错:

Command roscore not found, but can be installed with:

sudo apt install python3-roslaunch

这个时候

source ./devel/setup.bash

一下就好了 而且发现也可以使用rosrun,Anaconda里面的包也都可以使用。

基本使用

官网:之后有空会翻译一下。

例子

import numpy as np
from pyomo.environ import *
from pyomo.dae import *

model = ConcreteModel()
model.x = Var(RangeSet(1, 4), bounds=(1, 5))
model.cons1 = Constraint(rule=lambda model: 40==model.x[1]**2+model.x[2]**2+model.x[3]**2+model.x[4]**2)
model.cons2 = Constraint(rule=lambda model: 25<=model.x[1]*model.x[2]*model.x[3]*model.x[4])
model.obj = Objective(expr = model.x[1]*model.x[4]*(model.x[1] + model.x[2] + model.x[3]) + model.x[3], sense=minimize) 
SolverFactory(ipopt).solve(model)
solutions = [model.x[i]() for i in range(1, 5)]
print("solutins is :"+str(solutions))
经验分享 程序员 微信小程序 职场和发展