jetson nano install ipopt and CppAD and pyomo

jetson nano + ubuntu 18.04+128G TF

值得一说的是,本篇同样适合jetson TX1与TX2。


  • 1.下载库
wget http://www.coin-or.org/download/source/Ipopt/Ipopt-3.12.9.tgz
tar xvzf Ipopt-3.12.9.tgz
  • 2.下载第三方包
cd ~/Ipopt-3.12.9/ThirdParty/Blas
./get.Blas
cd ../Lapack
./get.Lapack
cd ../Mumps
./get.Mumps
cd ../Metis
./get.Metis
cd ../../ 
  • 3.将jetson nano系统的config.guess替代ipop中的config.guess. (成功的关键!!!!!)
cp /usr/share/automake-1.15/config.guess ~/Ipopt-3.12.9/BuildTools/config.guess
cp /usr/share/automake-1.15/config.guess ~/Ipopt-3.12.9/Ipopt/config.guess
cp /usr/share/automake-1.15/config.guess ~/Ipopt-3.12.9/ThirdParty/ASL/config.guess
cp /usr/share/automake-1.15/config.guess ~/Ipopt-3.12.9/ThirdParty/Blas/config.guess
cp /usr/share/automake-1.15/config.guess ~/Ipopt-3.12.9/ThirdParty/HSL/config.guess
cp /usr/share/automake-1.15/config.guess ~/Ipopt-3.12.9/ThirdParty/Lapack/config.guess
cp /usr/share/automake-1.15/config.guess ~/Ipopt-3.12.9/ThirdParty/Metis/config.guess
cp /usr/share/automake-1.15/config.guess ~/Ipopt-3.12.9/ThirdParty/Mumps/config.guess
  • 4.编译ipopt
cd ~/Ipopt-3.12.9/
mkdir build
cd build
../configure --prefix=/usr/local   
make
sudo make install
  • 5.安装cppad
sudo apt-get install cppad
  • 6.测试cppad+ipopt

qtcreator->File->New File or Project->Non-Qt Project->Plain C++ Application->Choose

在跳出来的界面中,项目名称随意取,此处为:test

然后将以下代码替代main.cpp中的hello world代码。

并在test.pro文件未尾添加ipopt依赖库LIBS += -L/usr/local/lib -lipopt

#include 
#include 

using namespace std;

namespace {
using CppAD::AD;
class FG_eval {
public:
    typedef CPPAD_TESTVECTOR(AD<double>) ADvector;
    void operator()(ADvector& fg, const ADvector& x)
    {
        assert(fg.size() == 3);
        assert(x.size() == 4);
        // variables
        AD<double> x1 = x[0];
        AD<double> x2 = x[1];
        AD<double> x3 = x[2];
        AD<double> x4 = x[3];
        // f(x) objective function
        fg[0] = x1 * x4 * (x1 + x2 + x3) + x3;
        // constraints
        fg[1] = x1 * x2 * x3 * x4;
        fg[2] = x1 * x1 + x2 * x2 + x3 * x3 + x4 * x4;
        return;
    }

};

}

bool get_started(void)
{
    bool ok = true;
    size_t i;
    typedef CPPAD_TESTVECTOR(double) Dvector;

    size_t nx = 4; // number of varibles
    size_t ng = 2; // number of constraints
    Dvector x0(nx); // initial condition of varibles
    x0[0] = 1.0;
    x0[1] = 5.0;
    x0[2] = 5.0;
    x0[3] = 1.0;

    // lower and upper bounds for varibles
    Dvector xl(nx), xu(nx);
    for(i = 0; i < nx; i++)
    {
        xl[i] = 1.0;
        xu[i] = 5.0;
    }
    Dvector gl(ng), gu(ng);
    gl[0] = 25.0;    gu[0] = 1.0e19;
    gl[1] = 40.0;    gu[1] = 40.0;
    // object that computes objective and constraints
    FG_eval fg_eval;

    // options
    string options;
    // turn off any printing
    options += "Integer print_level  0\n";
    options += "String sb            yes\n";
    // maximum iterations
    options += "Integer max_iter     10\n";
    //approximate accuracy in first order necessary conditions;
    // see Mathematical Programming, Volume 106, Number 1,
    // Pages 25-57, Equation (6)
    options += "Numeric tol          1e-6\n";
    //derivative tesing
    options += "String derivative_test   second-order\n";
    // maximum amount of random pertubation; e.g.,
    // when evaluation finite diff
    options += "Numeric point_perturbation_radius   0.\n";


    CppAD::ipopt::solve_result<Dvector> solution; // solution
    CppAD::ipopt::solve<Dvector, FG_eval>(options, x0, xl, xu, gl, gu, fg_eval, solution); // solve the problem

    cout<<"solution: "<<solution.x<<endl;

    //
    //check some of the solution values
    //
    ok &= solution.status == CppAD::ipopt::solve_result<Dvector>::success;
    //
    double check_x[]  = {1.000000, 4.743000, 3.82115, 1.379408};
    double check_zl[] = {1.087871, 0.,       0.,       0.      };
    double check_zu[] = {0.,       0.,       0.,       0.      };
    double rel_tol    = 1e-6; // relative tolerance
    double abs_tol    = 1e-6; // absolute tolerance
    for(i = 0; i < nx; i++)
    {
        ok &= CppAD::NearEqual(
                    check_x[i], solution.x[i], rel_tol, abs_tol);
        ok &= CppAD::NearEqual(
                    check_zl[i], solution.zl[i], rel_tol, abs_tol);
        ok &= CppAD::NearEqual(
                    check_zu[i], solution.zu[i], rel_tol, abs_tol);
    }

    return ok;
}

int main()
{
    cout << "CppAD : Hello World Demo!" << endl;
    get_started();
    return 0;
}


输出: CppAD : Hello World Demo!
solution: {1, 4.743, 3.82115, 1.37941}

  • 7.pyomo+ipopt
    至少需要先按装以上步骤成功安装c++版本的ipopt,才能安装python版本的ipopt

从pip安装,如果觉得慢可以更改pypi源。

sudo pip3 install pyomo
sudo pip3 install ipopt

在.bashrc文件未尾添加ipopt库路径,好让python里的ipopt找到它。这一步不加,在测试时会出错。

echo "export LD_LIBRARY_PATH=${LD_LIBRARY_PATH}:/usr/local/lib" >> ~/.bashrc
source ~/.bashrc

测试:

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

model = ConcreteModel()
model.x = Var(RangeSet(1, 4), bounds=(1, 25))
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))

输出: solutions is :[1.0, 4.742999644013376, 3.821149978907638, 1.3794082901545968]


END

今天很热,热的路由器罢工,网速从变慢然后无网络连接。我下楼问我妈,以前冰箱配的冻小冰块的那个模具呢?我要冻点冰块给路由器降温。然而,多年前这个冻小冰块模具已经被我妈当作没用的东西扔掉了。热的飞起,今天35度!

2020年5月4日 晴晴晴晴晴晴晴

差点忘记了,五四青年节快乐!

你可能感兴趣的:(ubuntu)