Skip to content

Commit 25c5c36

Browse files
committed
first commit
0 parents  commit 25c5c36

11 files changed

+489
-0
lines changed

.gitignore

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
cmake-build-debug

.idea/.gitignore

Lines changed: 8 additions & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

.idea/material_theme_project_new.xml

Lines changed: 12 additions & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

CMakeLists.txt

Lines changed: 30 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,30 @@
1+
cmake_minimum_required(VERSION 3.29)
2+
project(CasADi_tutorial)
3+
4+
set(CMAKE_CXX_STANDARD 20)
5+
6+
find_package(casadi REQUIRED)
7+
8+
find_library(CASADI_LIBRARY
9+
NAMES casadi
10+
HINTS ${CASADI_INCLUDE_DIR}/../lib $ENV{CASADI_PREFIX}/lib)
11+
if(CASADI_LIBRARY)
12+
set(CASADI_LIBRARIES ${CASADI_LIBRARIES} ${CASADI_LIBRARY})
13+
endif()
14+
15+
include_directories(include
16+
${casadi_INCLUDE_DIRS}
17+
)
18+
19+
add_executable(chapter1 chapter1-SX.cpp)
20+
add_executable(chapter2 chapter2-DM.cpp)
21+
add_executable(chapter3 chapter3-Function.cpp)
22+
add_executable(chapter4 chapter4-OptimalProblem.cpp)
23+
add_executable(chapter5 chapter5-GenCode.cpp)
24+
25+
26+
target_link_libraries(chapter1 ${CASADI_LIBRARIES})
27+
target_link_libraries(chapter2 ${CASADI_LIBRARIES})
28+
target_link_libraries(chapter3 ${CASADI_LIBRARIES})
29+
target_link_libraries(chapter4 ${CASADI_LIBRARIES})
30+
target_link_libraries(chapter5 ${CASADI_LIBRARIES})

chapter1-SX.cpp

Lines changed: 76 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,76 @@
1+
#include <iostream>
2+
#include <string>
3+
#include <casadi/casadi.hpp>
4+
5+
using namespace casadi;
6+
int main(){
7+
// 创建符号变量
8+
casadi::SX q = casadi::SX::sym("q",7);
9+
std::cout<<"q: "<<q<<std::endl;
10+
11+
SX q2 = SX::sym("q2",7);
12+
std::cout<<"q2: "<<q2<<std::endl;
13+
14+
// 拼接符号变量
15+
// vertcat 垂直拼接 horzcat 水平拼接
16+
SX qh = SX::horzcat({q,q2});
17+
SX qv = SX::vertcat({q,q2});
18+
std::cout<<"qh: "<<qh<<std::endl;
19+
std::cout<<"qv: "<<qv<<std::endl;
20+
// 转置符号变量
21+
SX qt = qh.T();
22+
std::cout<<"transpose of qh: " <<qt <<std::endl;
23+
24+
// 修改创建的符号变量的值
25+
SX t1 = SX::sym("t1",3);
26+
27+
std::cout<<"t1 before: "<<t1<<std::endl;
28+
t1(0) = 3;
29+
t1(1) = 1;
30+
t1(2) = 4;
31+
std::cout<<"t1 after: "<<t1<<std::endl;
32+
33+
SX t2 = SX::ones(3,1);
34+
t2(1) = 3;
35+
t2(2) = 1;
36+
std::cout<<"t2: "<<t2<<std::endl;
37+
38+
SX T01 = casadi::SX::zeros(4,4);
39+
40+
T01(Slice(0,3,1),3) = t1;
41+
T01(3,3) = 1;
42+
T01(2,2) = -1;
43+
T01(0,0) = cos(q(0));
44+
T01(0,1) = -sin(q(0));
45+
T01(1,0) = -sin(q(0));
46+
T01(1,1) = -cos(q(0));
47+
std::cout<<"T01: "<<T01<<std::endl;
48+
49+
auto T12 = SX::zeros(4,4);
50+
T12(Slice(0,3),3) = t2;
51+
T12(3,3) = 1;
52+
T12(0,0) = cos(q(1));
53+
T12(0,1) = -sin(q(1));
54+
T12(2,0) = sin(q(1));
55+
T12(2,1) = cos(q(1));
56+
std::cout<<"T12: "<<T12<<std::endl;
57+
58+
// 矩阵乘法
59+
casadi::SX A = SX::zeros(2,2);
60+
A(0,0) = 100;
61+
A(0,1) = 50;
62+
A(1,0) = -1;
63+
A(0,1) = -2;
64+
casadi::SX B = SX::zeros(2,2);
65+
B(0,0) = 0.5;
66+
B(0,1) = 0.1;
67+
B(1,0) = 0.2;
68+
B(0,1) = 0.4;
69+
70+
std::cout<< "test matrix multiplication: "<<std::endl;
71+
std::cout<< "A: "<<A<<std::endl;
72+
std::cout<< "B: "<<B<<std::endl;
73+
std::cout<< "element multiplication: "<<A*B<<std::endl;
74+
std::cout<< "matrix multiplication: "<<SX::mtimes(A,B)<<std::endl;
75+
return 0;
76+
};

chapter2-DM.cpp

Lines changed: 69 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,69 @@
1+
//
2+
// Created by lock on 2024/10/11.
3+
//
4+
// DM : Dense Matrix
5+
6+
#include "iostream"
7+
#include "string"
8+
#include "casadi/casadi.hpp"
9+
10+
using namespace casadi;
11+
12+
void print(const std::string name,DM dm){
13+
std::cout<<name<<": \n"<<dm<<std::endl;
14+
}
15+
void print(const std::string name,SX sx){
16+
std::cout<<name<<": \n"<<sx<<std::endl;
17+
}
18+
19+
int main(){
20+
// 创建稠密矩阵,以及基本的操作
21+
DM q = DM::ones(7,1);
22+
print("q",q);
23+
DM q2 = DM::rand(7,1);
24+
print("q2",q2);
25+
26+
auto qh = DM::horzcat({q, q2});
27+
print("qh",qh);
28+
auto qv = DM::vertcat({q, q2});
29+
print("qv",qv);
30+
31+
DM qt = qh.T();
32+
print("qt",qt);
33+
34+
// 修改稠密矩阵的变量
35+
DM t1 = DM::zeros(3,1);
36+
DM t2 = DM::ones(3,1);
37+
38+
print("t1 before",t1);
39+
print("t2 before",t2);
40+
t1(0) = 0;
41+
t1(1) = 0;
42+
t1(2) = 0.214;
43+
44+
t2(1) = 0.15;
45+
t2(2) = -0.2314;
46+
print("t1 after",t1);
47+
print("t2 after",t2);
48+
49+
DM T01 = DM::zeros(4,4);
50+
T01(Slice(0,3),3) = t1;
51+
52+
casadi::DM A = DM::zeros(2,2);
53+
A(0,0) = 100;
54+
A(0,1) = 50;
55+
A(1,0) = -1;
56+
A(0,1) = -2;
57+
casadi::DM B = DM::zeros(2,2);
58+
B(0,0) = 0.5;
59+
B(0,1) = 0.1;
60+
B(1,0) = 0.2;
61+
B(0,1) = 0.4;
62+
63+
std::cout<< "test matrix multiplication: "<<std::endl;
64+
std::cout<< "A: "<<A<<std::endl;
65+
std::cout<< "B: "<<B<<std::endl;
66+
std::cout<< "element multiplication: "<<A*B<<std::endl;
67+
std::cout<< "matrix multiplication: "<<DM::mtimes(A,B)<<std::endl;
68+
return 0;
69+
}

chapter3-Function.cpp

Lines changed: 79 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,79 @@
1+
//
2+
// Created by lock on 2024/10/11.
3+
//
4+
#include <casadi/casadi.hpp>
5+
#include <iostream>
6+
using namespace casadi;
7+
8+
void print(const std::string name,DM dm){
9+
std::cout<<name<<": \n"<<dm<<std::endl;
10+
}
11+
void print(const std::string name,SX sx){
12+
std::cout<<name<<": \n"<<sx<<std::endl;
13+
}
14+
15+
void print(const std::string name,Function f){
16+
std::cout<<name<<": \n"<<f<<std::endl;
17+
}
18+
19+
int main() {
20+
/*
21+
* 如何创建Function
22+
* a.使用符号表达式
23+
* b.使用字典构建(后面结尾再说)
24+
* */
25+
// 定义输入
26+
SX x = SX::sym("x",1);
27+
SX y = SX::sym("y",1);
28+
// 定义输出
29+
SX f = SX::sym("f");
30+
// 建立关系
31+
f = x*x+y*y;
32+
SX f2 = SX::sym("f2");
33+
f2 = x+y;
34+
Function myFunction = Function("myFunction",{x,y},{f,f2});
35+
print("myFunction",myFunction);
36+
37+
/*
38+
* Function的调用
39+
* a.计算函数值
40+
* b.替换得到符号表达式
41+
* */
42+
std::vector<DM> input = {2,3};
43+
std::vector<DM> output = myFunction(input);
44+
45+
std::cout<<output[0]<<std::endl;
46+
47+
/*
48+
* 多输出的Function
49+
* 取得
50+
* */
51+
std::cout<<output[1]<<std::endl;
52+
53+
/*
54+
* Function的自带的函数
55+
* 求导,Jacobian
56+
* */
57+
Function jac = myFunction.jacobian();
58+
print("jac",jac);
59+
std::vector<DM> jacInput = {DM::zeros(1,1),DM::ones(1,1),DM::ones(1,1),DM::ones(1,1)};
60+
auto jacOut = jac(jacInput);
61+
std::cout<<jacOut<<std::endl;
62+
/*
63+
* 嵌套Function
64+
* */
65+
Function cosFunction = Function("cosFunction",{x},{cos(x)});
66+
Function sinFunction = Function("sinFunction",{x},{sin(x)});
67+
Function inheritFunction = Function("inheritFunction",{x},sinFunction(cosFunction(x)[0]));
68+
print("inheritFunction",inheritFunction);
69+
DM inheritIn = DM::ones(1,1);
70+
DM inheritOut = inheritFunction({inheritIn})[0];
71+
print("inheritOut",inheritOut);
72+
73+
/*
74+
* 使用字典构建Function(引出求解器构建)
75+
* */
76+
77+
78+
return 0;
79+
}

chapter4-OptimalProblem.cpp

Lines changed: 111 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,111 @@
1+
//
2+
// Created by lock on 2024/10/11.
3+
//
4+
#include <iostream>
5+
#include <casadi/casadi.hpp>
6+
7+
using namespace casadi;
8+
9+
/*
10+
* test problem:
11+
* min x0^2 + x1^2
12+
* s.t. x0+x1-10 = 0
13+
* */
14+
15+
int main() {
16+
// auto x= SX::sym("x",2);
17+
// auto obj = pow(x(0),2)+ pow(x(1),2);
18+
// auto g = x(0)+x(1)-10;
19+
//
20+
// SXDict nlp = {{"x",x},{"f",obj},{"g",g}};
21+
// Function solver = nlpsol("solver","ipopt",nlp);
22+
// std::map<std::string,DM> arg,res;
23+
// arg["lbx"] = -DM::inf(2);
24+
// arg["ubx"] = DM::inf(2);
25+
// arg["lbg"] = 0;
26+
// arg["ubg"] = 0;
27+
//
28+
// arg["x0"] = DM::zeros(2);
29+
//
30+
// res = solver(arg);
31+
//
32+
// std::cout<<"----------"<<std::endl;
33+
// std::cout<<"obj in solution = " <<res.at("f")<<std::endl;
34+
// std::cout<<"primal solution = " << res.at("x")<<std::endl;
35+
// std::cout<<"dual solution(x) =" <<res.at("lam_x")<<std::endl;
36+
// std::cout<<"dual solution(g) =" <<res.at("lam_g")<<std::endl;
37+
38+
/*
39+
* test problem 2
40+
* max 2x+y
41+
* s.t x+y <=40
42+
* 4x-y>=10
43+
* 2x+y<=50
44+
* x,y>0
45+
* */
46+
// auto x = SX::sym("x", 2);
47+
// auto obj = 2 * x(0) + x(1);
48+
// auto g = SX::vertcat(
49+
// {x(0) + x(1),
50+
// 4 * x(0) - x(1),
51+
// 2 * (x(0) + x(1))});
52+
//
53+
// SXDict nlp = {{"x", x},
54+
// {"f", obj},
55+
// {"g", g}};
56+
// Function solver = nlpsol("solver", "ipopt", nlp);
57+
// std::map<std::string, DM> arg, res;
58+
// arg["lbx"] = 0;
59+
// arg["ubx"] = DM::inf(2);
60+
// arg["lbg"] = DM::vertcat({-DM::inf(), 10, -DM::inf()});
61+
// arg["ubg"] = DM::vertcat({40, DM::inf(), 50});
62+
//
63+
// arg["x0"] = DM::zeros(2);
64+
//
65+
// res = solver(arg);
66+
//
67+
// std::cout << "----------" << std::endl;
68+
// std::cout << "obj in solution = " << res.at("f") << std::endl;
69+
// std::cout << "primal solution = " << res.at("x") << std::endl;
70+
// std::cout << "dual solution(x) =" << res.at("lam_x") << std::endl;
71+
// std::cout << "dual solution(g) =" << res.at("lam_g") << std::endl;
72+
73+
/*
74+
* Nonlinear optimization problem
75+
*
76+
* min (x-2)^2 + (y-1)^2
77+
* s.t x^2+y^2<=4
78+
* x*y>=1
79+
* x+y>=1
80+
*
81+
* */
82+
83+
auto x = SX::sym("x", 2);
84+
auto obj = pow(x(0)-2,2) + pow(x(1)-1,2);
85+
auto g = SX::vertcat(
86+
{pow(x(0),2) + pow(x(1),2),
87+
x(0)*x(1),
88+
x(0)+x(1)});
89+
90+
SXDict nlp = {{"x", x},
91+
{"f", obj},
92+
{"g", g}};
93+
Function solver = nlpsol("solver", "ipopt", nlp);
94+
std::map<std::string, DM> arg, res;
95+
arg["lbx"] = -DM::inf(2);
96+
arg["ubx"] = DM::inf(2);
97+
arg["lbg"] = DM::vertcat({-DM::inf(), 1, 1});
98+
arg["ubg"] = DM::vertcat({4, DM::inf(), DM::inf()});
99+
100+
arg["x0"] = DM::zeros(2);
101+
102+
res = solver(arg);
103+
104+
std::cout << "----------" << std::endl;
105+
std::cout << "obj in solution = " << res.at("f") << std::endl;
106+
std::cout << "primal solution = " << res.at("x") << std::endl;
107+
std::cout << "dual solution(x) =" << res.at("lam_x") << std::endl;
108+
std::cout << "dual solution(g) =" << res.at("lam_g") << std::endl;
109+
110+
return 0;
111+
}

0 commit comments

Comments
 (0)