1 // Optimizes general sigmoid function 1/(1 + exp(-a(t-m))) over a set of points.
5 #include "ceres/ceres.h"
6 #include "gflags/gflags.h"
7 #include "glog/logging.h"
17 class DistanceFromSigmoidCostFunction {
19 DistanceFromSigmoidCostFunction(Point p) : p_(p) {}
22 bool operator() (const T * const a, const T * const m, T* e) const {
23 *e = T(p_.adoption_rate) - T(1.0) / (T(1.0) + ceres::exp(- *a * (T(p_.t) - *m)));
31 int main(int argc, char** argv) {
32 google::ParseCommandLineFlags(&argc, &argv, true);
33 google::InitGoogleLogging(argv[0]);
37 FILE *fp = fopen(argv[1], "r");
43 // The two parameters to be optimized.
50 if (fgets(buf, 256, fp) == NULL) {
55 p.adoption_rate = atof(buf) * 0.01;
56 problem.AddResidualBlock(
57 new AutoDiffCostFunction<DistanceFromSigmoidCostFunction, 1, 1, 1>(
58 new DistanceFromSigmoidCostFunction(p)),
68 Solver::Options options;
69 options.max_num_iterations = 1000000;
70 options.linear_solver_type = ceres::DENSE_QR;
71 options.minimizer_progress_to_stdout = true;
73 Solver::Summary summary;
74 Solve(options, &problem, &summary);
76 std::cout << summary.BriefReport() << "\n";
77 std::cout << "a=" << a << std::endl;
78 std::cout << "m=" << m << std::endl;