-
Notifications
You must be signed in to change notification settings - Fork 0
/
problemToSolve.h
134 lines (106 loc) · 3.37 KB
/
problemToSolve.h
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
//
// Specify the problem functions here and allocate them to objectives or constraints (inequality <= 0,
// equality = 0) in the CreateProblem function using the appropriate vectors.
//
// Neill D.F. Campbell, 2015
//
#ifndef PROBLEM_TO_SOLVE_H
#define PROBLEM_TO_SOLVE_H
#define USE_GOOGLE_LOG
#include <ceres/ceres.h>
#include <cmath>
using namespace std;
#include "ConstrainedCeresProblem.hpp"
struct constraint_1
{
template <typename T> bool operator()(const T* const x, T* residual) const
{
T x0 = x[0];
T x1 = x[1];
T x2 = x[2];
residual[0] = ((x0 - x1*T(2.0))) * T(10.0) ;
residual[1] = ((x0 + x1) - x2) * T(10.0) ;
return true;
}
};
struct constraint_2
{
template <typename T> bool operator()(const T* const x, T* residual) const
{
T x0 = x[0];
T x1 = x[1];
T x2 = x[2];
residual[0] = (T(10.0) - x0) * T(10.0) ;
residual[1] = (T(5) - x1) * T(10.0) ;
residual[2] = (T(20) - x2) * T(10.0) ;
return true;
}
};
struct equality_constraint
{
template <typename T> bool operator()(const T* const x, T* residual) const
{
T x0 = x[0];
T x1 = x[1];
T x2 = x[2];
residual[0] = (T(6.0) - x1);
return true;
}
};
struct objective
{
template <typename T> bool operator()(const T* const x, T* residual) const
{
T x0 = x[0];
T x1 = x[1];
T x2 = x[2];
residual[0] = x0 + x1 + x2;
return true;
}
};
CeresConstrainedProblem* CreateProblem()
{
#ifdef USE_GOOGLE_LOG
// This should really be called from a main function therefore not a library..
static bool doneGoogleInit = false;
if (!doneGoogleInit)
{
mexInfoPrintf("\nINFO: InitGoogleLogging\n\n");
google::InitGoogleLogging("mexCeres");
doneGoogleInit = true;
}
#endif // USE_GOOGLE_LOG
//
// NOTE: Very IMPORTANT that the parameters are created with new[] since they
// will be deleted with delete[].
//
const int param_size = 3;
double* param = new double[param_size];
for(int i = 0; i < param_size; ++i)
{
param[i] = rand()/(RAND_MAX/1.0);
}
// Important to create the ceres::Problem as a shared_ptr..
std::shared_ptr<ceres::Problem> problem(new ceres::Problem());
std::vector<ceres::ResidualBlockId> objectiveIDs;
std::vector<ceres::ResidualBlockId> equalityConstraintIDs;
std::vector<ceres::ResidualBlockId> inequalityConstraintIDs;
ceres::CostFunction* constraint_function_1 =
new ceres::AutoDiffCostFunction<constraint_1, 2, param_size>(new constraint_1);
inequalityConstraintIDs.push_back(problem->AddResidualBlock(constraint_function_1, NULL, param));
ceres::CostFunction* constraint_function_2 =
new ceres::AutoDiffCostFunction<constraint_2 , 3, param_size>(new constraint_2);
inequalityConstraintIDs.push_back(problem->AddResidualBlock(constraint_function_2, NULL, param));
ceres::CostFunction* equality_constraint_function =
new ceres::AutoDiffCostFunction<equality_constraint, 1, param_size>(new equality_constraint);
equalityConstraintIDs.push_back(problem->AddResidualBlock(equality_constraint_function, NULL, param));
ceres::CostFunction* objectiveFunc =
new ceres::AutoDiffCostFunction<objective, 1, param_size>(new objective);
objectiveIDs.push_back(problem->AddResidualBlock(objectiveFunc, NULL, param));
CeresConstrainedProblem* constrainedProb;
constrainedProb = new CeresConstrainedProblem(problem, objectiveIDs,
equalityConstraintIDs,
inequalityConstraintIDs);
return constrainedProb;
}
#endif // PROBLEM_TO_SOLVE_H