| 87 | // ---------------------------------------------------------------------------------------- |
| 88 | |
| 89 | int main() |
| 90 | { |
| 91 | try |
| 92 | { |
| 93 | // randomly pick a set of parameters to use in this example |
| 94 | const parameter_vector params = 10*randm(3,1); |
| 95 | cout << "params: " << trans(params) << endl; |
| 96 | |
| 97 | |
| 98 | // Now let's generate a bunch of input/output pairs according to our model. |
| 99 | std::vector<std::pair<input_vector, double> > data_samples; |
| 100 | input_vector input; |
| 101 | for (int i = 0; i < 1000; ++i) |
| 102 | { |
| 103 | input = 10*randm(2,1); |
| 104 | const double output = model(input, params); |
| 105 | |
| 106 | // save the pair |
| 107 | data_samples.push_back(make_pair(input, output)); |
| 108 | } |
| 109 | |
| 110 | // Before we do anything, let's make sure that our derivative function defined above matches |
| 111 | // the approximate derivative computed using central differences (via derivative()). |
| 112 | // If this value is big then it means we probably typed the derivative function incorrectly. |
| 113 | cout << "derivative error: " << length(residual_derivative(data_samples[0], params) - |
| 114 | derivative(residual)(data_samples[0], params) ) << endl; |
| 115 | |
| 116 | |
| 117 | |
| 118 | |
| 119 | |
| 120 | // Now let's use the solve_least_squares_lm() routine to figure out what the |
| 121 | // parameters are based on just the data_samples. |
| 122 | parameter_vector x; |
| 123 | x = 1; |
| 124 | |
| 125 | cout << "Use Levenberg-Marquardt" << endl; |
| 126 | // Use the Levenberg-Marquardt method to determine the parameters which |
| 127 | // minimize the sum of all squared residuals. |
| 128 | solve_least_squares_lm(objective_delta_stop_strategy(1e-7).be_verbose(), |
| 129 | residual, |
| 130 | residual_derivative, |
| 131 | data_samples, |
| 132 | x); |
| 133 | |
| 134 | // Now x contains the solution. If everything worked it will be equal to params. |
| 135 | cout << "inferred parameters: "<< trans(x) << endl; |
| 136 | cout << "solution error: "<< length(x - params) << endl; |
| 137 | cout << endl; |
| 138 | |
| 139 | |
| 140 | |
| 141 | |
| 142 | x = 1; |
| 143 | cout << "Use Levenberg-Marquardt, approximate derivatives" << endl; |
| 144 | // If we didn't create the residual_derivative function then we could |
| 145 | // have used this method which numerically approximates the derivatives for you. |
| 146 | solve_least_squares_lm(objective_delta_stop_strategy(1e-7).be_verbose(), |
nothing calls this directly
no test coverage detected