MCPcopy Create free account
hub / github.com/ComputationalRobotics/XM-code / Optimizer

Class Optimizer

XM/include/Optimization/optimization.h:29–70  ·  view source on GitHub ↗

Source from the content-addressed store, hash-verified

27};
28
29class Optimizer {
30public:
31 Optimizer(opt_objective func, opt_gradient grad): F(func), gradF(grad){}
32
33 void optimize(opt_var& initial_guess, opt_option& option,DeviceBlasHandle& cublas_H) {
34 x.copy(initial_guess);
35 gradx.copy(initial_guess);
36 switch (option.method) {
37 case opt_option::GradientDescent: {
38 gradientdecent(option.lr_gd, option.max_iteration_gd,cublas_H);
39 break;
40 }
41 case opt_option::NewtonMethod: {
42
43 std::cout << "Newton Method is not implemented yet." << std::endl;
44 break;
45 }
46 }
47
48 }
49
50 void gradientdecent(double lr, size_l maxitr,DeviceBlasHandle& cublas_H){
51 std::cout << "Initial value is:" << F(x) << std::endl;
52 double dlr = -lr;
53 for(size_l i = 0; i<maxitr; ++i){
54 gradF(x,gradx);
55 CHECK_CUBLAS(cublasDaxpy(cublas_H.cublas_handle, x.total_size,
56 &dlr,gradx.vals, 1,x.vals, 1));
57 if(i % 10 == 0){
58 std::cout << "New value is:" << F(x) << std::endl;
59 }
60 }
61 }
62
63// private:
64
65 opt_objective F;
66 opt_gradient gradF;
67 datatype Fx;
68 opt_var x;
69 opt_var gradx;
70};
71
72#endif // OPTIMIZATION_H

Callers

nothing calls this directly

Calls

no outgoing calls

Tested by

no test coverage detected