1 | // Example program exhibiting crashing behavior on ceres-solver from MacPorts. |
---|
2 | #include <fstream> |
---|
3 | #include <vector> |
---|
4 | #include <ceres/ceres.h> |
---|
5 | #include <glog/logging.h> |
---|
6 | |
---|
7 | using namespace std; |
---|
8 | |
---|
9 | struct Bounds { |
---|
10 | Bounds(size_t _var, double _val) : var(_var), value(_val) { } |
---|
11 | size_t var; double value; |
---|
12 | }; |
---|
13 | |
---|
14 | struct Block { |
---|
15 | double vars[4]; |
---|
16 | Block() { vars[0] = vars[1] = vars[2] = vars[3] = 1.0; } |
---|
17 | |
---|
18 | struct Cost { |
---|
19 | vector<double> v1, v2; |
---|
20 | Cost(const vector<double> &_v1, const vector<double> &_v2) |
---|
21 | : v1(_v1), v2(_v2) { } |
---|
22 | |
---|
23 | template<typename T> |
---|
24 | bool operator()(const T *x, T *e) const { |
---|
25 | T a = -x[2] / x[1]; |
---|
26 | e[0] = T(v2[0]) / x[0] + T(v2[1]) * a - T(v1[0]); |
---|
27 | e[1] = T(v2[0]) * a + T(v2[1]) / x[1] - T(v1[1]); |
---|
28 | e[2] = T(0.5 * v2[2]) / x[3] - T(v1[2]); |
---|
29 | |
---|
30 | return true; |
---|
31 | } |
---|
32 | }; |
---|
33 | |
---|
34 | vector<Bounds> lowerBounds() const { return { Bounds(0, 0.01), Bounds(1, 0.01), |
---|
35 | Bounds(2, -0.75), Bounds(3, 0.01) }; } |
---|
36 | }; |
---|
37 | |
---|
38 | // Initialized with magic values at the end... |
---|
39 | extern vector<vector<double> > v1s, v2s; |
---|
40 | |
---|
41 | void run(size_t iterations, vector<Block> &blocks) { |
---|
42 | for (size_t i = 0; i < iterations; ++i) { |
---|
43 | ceres::Problem problem; |
---|
44 | |
---|
45 | for (size_t b = 0; b < blocks.size(); ++b) { |
---|
46 | typedef ceres::AutoDiffCostFunction<Block::Cost, 3, 4> ADCost; |
---|
47 | problem.AddResidualBlock(new ADCost(new Block::Cost(v1s.at(b), v2s.at(b))), |
---|
48 | NULL, &(blocks.at(b).vars[0])); |
---|
49 | } |
---|
50 | |
---|
51 | for (size_t b = 0; b < blocks.size(); ++b) { |
---|
52 | for (const auto &bd : blocks.at(b).lowerBounds()) |
---|
53 | problem.SetParameterLowerBound(&(blocks.at(b).vars[0]), bd.var, bd.value); |
---|
54 | } |
---|
55 | |
---|
56 | ceres::Solver::Options options; |
---|
57 | ceres::Solver::Summary summary; |
---|
58 | ceres::Solve(options, &problem, &summary); |
---|
59 | cout << i << endl; |
---|
60 | } |
---|
61 | } |
---|
62 | |
---|
63 | int main(int argc, char *argv[]) { |
---|
64 | google::InitGoogleLogging(argv[0]); |
---|
65 | vector<Block> blocks(32); |
---|
66 | run(20, blocks); |
---|
67 | return 0; |
---|
68 | } |
---|
69 | |
---|
70 | // Magic values! |
---|
71 | vector<vector<double> > v1s = |
---|
72 | { {3.0817489158798020e-02, -1.4639652781992274e-01, 2.5384337331345333e-02}, |
---|
73 | {3.6416333288597572e-02, -1.4370318305552215e-01, 1.4599625998471710e-02}, |
---|
74 | {5.9139959045658906e-02, -1.6027414824842132e-01, 4.6539090917746689e-02}, |
---|
75 | {-7.7137836451530994e-04, -1.3403132909752880e-01, -4.1484586240324015e-02}, |
---|
76 | {2.5560607024309095e-02, -1.2919253632002803e-01, 2.5832911869640728e-02}, |
---|
77 | {3.2071237742699508e-02, -1.3022405189772818e-01, 2.7268453999170710e-02}, |
---|
78 | {2.1622790894534089e-02, -1.2726778921938658e-01, -5.1173706653678992e-03}, |
---|
79 | {4.0244356858828156e-02, -1.6130786507298731e-01, -1.1163660781301264e-02}, |
---|
80 | {4.8788063640979901e-02, -1.8120846219081277e-01, -1.4565309498155628e-02}, |
---|
81 | {5.5232208414017762e-02, -1.5704262740549430e-01, 2.4233785919737232e-02}, |
---|
82 | {4.6104712366251126e-02, -2.0702534294091537e-01, -3.7053354995384750e-02}, |
---|
83 | {0.0000000000000000e+00, -2.5045832979528465e-01, -8.1854068155362947e-02}, |
---|
84 | {0.0000000000000000e+00, -1.3898004129056230e-01, 8.8575952321807663e-02}, |
---|
85 | {1.3477835680895604e-02, -1.0174098641907151e-01, 4.2912726175510882e-02}, |
---|
86 | {0.0000000000000000e+00, -3.8842425363172284e-02, 8.1837034481359830e-02}, |
---|
87 | {0.0000000000000000e+00, -1.9230435386316186e-01, -1.1777727633638249e-01}, |
---|
88 | {0.0000000000000000e+00, -1.2919253632002803e-01, 1.8577588501893672e-02}, |
---|
89 | {0.0000000000000000e+00, -1.6130786507298731e-01, -1.2410881654559655e-02}, |
---|
90 | {5.5232208414017762e-02, -2.0702534294091537e-01, 2.1209618255679089e-02}, |
---|
91 | {2.1622790894534089e-02, -1.0174098641907151e-01, -1.4142330271570670e-02}, |
---|
92 | {0.0000000000000000e+00, -1.4370318305552215e-01, 5.7972849897391376e-03}, |
---|
93 | {2.5560607024309095e-02, -1.5704262740549430e-01, 1.0603850886092458e-02}, |
---|
94 | {3.0817489158798020e-02, -1.3022405189772818e-01, -1.9981528724067402e-03}, |
---|
95 | {0.0000000000000000e+00, -1.6380230681950408e-01, -3.2533060083973747e-02}, |
---|
96 | {4.0244356858828156e-02, -1.6170222153671412e-01, -9.0840425621821694e-03}, |
---|
97 | {3.6416333288597572e-02, -1.8120846219081277e-01, 2.7429309643678887e-03}, |
---|
98 | {4.8788063640979901e-02, -1.4928171214915323e-01, -6.8398639621393004e-03}, |
---|
99 | {4.6104712366251126e-02, -1.2521375949462860e-01, 3.8205731780906471e-03}, |
---|
100 | {5.9139959045658906e-02, -2.5045832979528465e-01, 6.2406787592275942e-02}, |
---|
101 | {3.2071237742699508e-02, -1.6027414824842132e-01, 2.2942846291737932e-02}, |
---|
102 | {1.3477835680895604e-02, -1.3898004129056230e-01, 3.8507144358112630e-02}, |
---|
103 | {-7.7137836451530994e-04, -3.8842425363172284e-02, -4.1432001268645366e-02} }; |
---|
104 | |
---|
105 | vector<vector<double> > v2s = |
---|
106 | { {1.0603676351030047e+00, -1.9821797140167170e+01, 4.2698931306173304e-17}, |
---|
107 | {5.6935183941423795e-01, -2.0952036543452198e+01, 5.7377285462306378e-17}, |
---|
108 | {-8.8813039511212222e-01, -1.5572692709906484e+01, 2.2862213501255789e-16}, |
---|
109 | {4.9215594754148624e+00, -2.7702822165808850e+01, 2.1351241181995114e-17}, |
---|
110 | {1.4881530961183600e+00, -2.8895795691536350e+01, 4.2698896770941251e-17}, |
---|
111 | {7.7261568397273850e-01, -2.8167259451299802e+01, -1.0675352592850044e-17}, |
---|
112 | {1.9438109265215457e+00, -3.0331887148391719e+01, 0.0000000000000000e+00}, |
---|
113 | {5.1134528154470149e-01, -1.4796491124909133e+01, -1.0675167676094421e-17}, |
---|
114 | {2.5884991669862606e-01, -1.0444103837535154e+01, -1.0675151308821354e-17}, |
---|
115 | {-6.0729046267177389e-01, -1.6388132235232202e+01, 7.4720835987312826e-17}, |
---|
116 | {5.2791651009406715e-01, -6.9756939288785551e+00, 6.4057746169957302e-17}, |
---|
117 | {1.6397218338183999e+00, -4.5068363344240403e+00, -9.6045084840126392e-17}, |
---|
118 | {4.4903656911643113e+00, -2.4826786375995109e+01, 3.8508571353500547e-16}, |
---|
119 | {3.7227601674009030e+00, -5.9717229830149542e+01, 1.0675014912059824e-17}, |
---|
120 | {4.5388631239396545e+01, -1.0493299676690988e+03, -4.2707185199475057e-17}, |
---|
121 | {2.5434916304734618e+00, -9.6377021723256533e+00, 8.5438030089939855e-17}, |
---|
122 | {5.1144674018812886e+00, -3.0754613768451545e+01, 4.2699455345762322e-17}, |
---|
123 | {3.4516035612843337e+00, -1.6062626076064539e+01, 4.2701840996489685e-17}, |
---|
124 | {2.2720224673857706e-01, -6.9924100045584581e+00, 1.1741508636714041e-16}, |
---|
125 | {1.9070342209621305e+00, -5.9177200450192416e+01, 8.0067987927799027e-17}, |
---|
126 | {4.2317777221540904e+00, -2.2512678201071591e+01, 8.5399985896146723e-17}, |
---|
127 | {1.4518488146156971e+00, -1.6158244294372508e+01, 8.5398505717778069e-17}, |
---|
128 | {9.1093611077442560e-01, -2.8163859933753752e+01, 2.1350481237960489e-17}, |
---|
129 | {3.3600165951024215e+00, -1.5359958929341769e+01, 4.2703390019456474e-17}, |
---|
130 | {5.1546092977785030e-01, -1.4687752130561263e+01, -8.5398973744921281e-17}, |
---|
131 | {8.3229834968097360e-01, -1.0439683645432424e+01, 4.2700674398446374e-17}, |
---|
132 | {-3.0907582755895013e-01, -1.8899119139130999e+01, 5.3376929702094496e-17}, |
---|
133 | {-1.3480514777053112e+00, -3.2431741303676304e+01, -4.2701179686587152e-17}, |
---|
134 | {3.2891299843734711e-01, -3.9244573081873497e+00, 1.7072665182609013e-16}, |
---|
135 | {1.0215084053935168e+00, -1.5107863743949604e+01, 4.2699119273145233e-17}, |
---|
136 | {2.6771252624695112e+00, -2.3728381584139054e+01, -4.2703849896809369e-17}, |
---|
137 | {4.9915099046852518e+01, -1.0484635916681621e+03, 4.2704075046392998e-17} }; |
---|