MCPcopy Create free account
hub / github.com/Arash-codedev/openGA / MyClass

Class MyClass

examples/so-bind/example_bind.cpp:39–143  ·  view source on GitHub ↗

Source from the content-addressed store, hash-verified

37typedef EA::GenerationType<MySolution,MyMiddleCost> Generation_Type;
38
39class MyClass
40{
41public:
42 std::ofstream output_file;
43
44 MyClass(std::string output_location)
45 {
46 output_file.open(output_location);
47 output_file<<"step"<<"\t"<<"x_best"<<"\t"<<"y_best"<<"\t"<<"cost_avg"<<"\t"<<"cost_best"<<"\n";
48 }
49
50 ~MyClass()
51 {
52 output_file.close();
53 }
54
55 void init_genes(MySolution& p,const std::function<double(void)> &rnd01)
56 {
57 p.x=20.0*rnd01()-10.0;
58 p.y=20.0*rnd01()-10.0;
59 }
60
61 bool eval_solution(
62 const MySolution& p,
63 MyMiddleCost &c)
64 {
65 double x=p.x;
66 double y=p.y;
67 // see the surface plot at:
68 // https://academo.org/demos/3d-surface-plotter/?expression=x*x%2By*y%2B30.0*sin(x*100.0*sin(y)%2By*100.0*cos(x))%2B125%2B45.0*sqrt(x%2By)*sin((15.0*(x%2By))%2F(x*x%2By*y))&xRange=-10%2C%2B10&yRange=-10%2C%2B10&resolution=100
69 //
70 // the middle comupations of cost:
71 if(x+y>0)
72 {
73 double predictable_noise=30.0*sin(x*100.0*sin(y)+y*100.0*cos(x));
74 c.cost_distance2=x*x+y*y+predictable_noise;
75 c.cost_sqsin=125+45.0*sqrt(x+y)*sin((15.0*(x+y))/(x*x+y*y));
76 return true; // genes are accepted
77 }
78 else
79 return false; // genes are rejected
80 }
81
82 MySolution mutate(
83 const MySolution& X_base,
84 const std::function<double(void)> &rnd01,
85 double shrink_scale)
86 {
87 MySolution X_new;
88 bool in_range_x,in_range_y;
89 const double mu=0.2*shrink_scale; // mutation radius
90 do{
91 X_new=X_base;
92 X_new.x+=mu*(rnd01()-rnd01());
93 X_new.y+=mu*(rnd01()-rnd01());
94 in_range_x= (X_new.x>=-10.0 && X_new.x<10.0);
95 in_range_y= (X_new.y>=-10.0 && X_new.y<10.0);
96 } while(!in_range_x || !in_range_y);

Callers

nothing calls this directly

Calls

no outgoing calls

Tested by

no test coverage detected