Loading [MathJax]/extensions/tex2jax.js
cuML C++ API  23.12
All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros Modules Pages
program.h
Go to the documentation of this file.
1 /*
2  * Copyright (c) 2021-2023, NVIDIA CORPORATION.
3  *
4  * Licensed under the Apache License, Version 2.0 (the "License");
5  * you may not use this file except in compliance with the License.
6  * You may obtain a copy of the License at
7  *
8  * http://www.apache.org/licenses/LICENSE-2.0
9  *
10  * Unless required by applicable law or agreed to in writing, software
11  * distributed under the License is distributed on an "AS IS" BASIS,
12  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13  * See the License for the specific language governing permissions and
14  * limitations under the License.
15  */
16 
17 #pragma once
18 
19 #include <raft/core/handle.hpp>
20 #include <random>
21 
22 #include "common.h"
23 
24 namespace cuml {
25 namespace genetic {
26 
31 struct program {
46  explicit program();
47 
53 
59  explicit program(const program& src);
60 
68  program& operator=(const program& src);
69 
72  int len;
74  int depth;
76  float raw_fitness_;
81 }; // struct program
82 
84 typedef program* program_t;
85 
96 void execute(const raft::handle_t& h,
97  const program_t& d_progs,
98  const int n_rows,
99  const int n_progs,
100  const float* data,
101  float* y_pred);
102 
116 void compute_metric(const raft::handle_t& h,
117  int n_rows,
118  int n_progs,
119  const float* y,
120  const float* y_pred,
121  const float* w,
122  float* score,
123  const param& params);
124 
137 void find_fitness(const raft::handle_t& h,
138  program_t& d_prog,
139  float* score,
140  const param& params,
141  const int n_rows,
142  const float* data,
143  const float* y,
144  const float* sample_weights);
145 
159 void find_batched_fitness(const raft::handle_t& h,
160  int n_progs,
161  program_t& d_progs,
162  float* score,
163  const param& params,
164  const int n_rows,
165  const float* data,
166  const float* y,
167  const float* sample_weights);
168 
181 void set_fitness(const raft::handle_t& h,
182  program_t& d_prog,
183  program& h_prog,
184  const param& params,
185  const int n_rows,
186  const float* data,
187  const float* y,
188  const float* sample_weights);
189 
203 void set_batched_fitness(const raft::handle_t& h,
204  int n_progs,
205  program_t& d_progs,
206  std::vector<program>& h_progs,
207  const param& params,
208  const int n_rows,
209  const float* data,
210  const float* y,
211  const float* sample_weights);
212 
221 float get_fitness(const program& prog, const param& params);
222 
229 int get_depth(const program& p_out);
230 
238 void build_program(program& p_out, const param& params, std::mt19937& rng);
239 
248 void point_mutation(const program& prog, program& p_out, const param& params, std::mt19937& rng);
249 
260 void crossover(const program& prog,
261  const program& donor,
262  program& p_out,
263  const param& params,
264  std::mt19937& rng);
265 
276 void subtree_mutation(const program& prog, program& p_out, const param& params, std::mt19937& rng);
277 
287 void hoist_mutation(const program& prog, program& p_out, const param& params, std::mt19937& rng);
288 } // namespace genetic
289 } // namespace cuml
RF_metrics score(const raft::handle_t &user_handle, const RandomForestClassifierF *forest, const int *ref_labels, int n_rows, const int *predictions, int verbosity=CUML_LEVEL_INFO)
void crossover(const program &prog, const program &donor, program &p_out, const param &params, std::mt19937 &rng)
Perform a 'hoisted' crossover mutation using the parent and donor programs. The donor subtree selecte...
void build_program(program &p_out, const param &params, std::mt19937 &rng)
Build a random program with depth atmost 10.
float get_fitness(const program &prog, const param &params)
Returns precomputed fitness score of program on the host, after accounting for parsimony.
void set_fitness(const raft::handle_t &h, program_t &d_prog, program &h_prog, const param &params, const int n_rows, const float *data, const float *y, const float *sample_weights)
Computes and sets the fitness scores for a single program on the given dataset.
void subtree_mutation(const program &prog, program &p_out, const param &params, std::mt19937 &rng)
Performs a crossover mutation with a randomly built new program. Since crossover is 'hoisted',...
metric_t
Definition: common.h:30
void find_fitness(const raft::handle_t &h, program_t &d_prog, float *score, const param &params, const int n_rows, const float *data, const float *y, const float *sample_weights)
Computes the fitness scores for a sngle program on the given dataset.
void find_batched_fitness(const raft::handle_t &h, int n_progs, program_t &d_progs, float *score, const param &params, const int n_rows, const float *data, const float *y, const float *sample_weights)
Computes the fitness scores for all programs on the given dataset.
void hoist_mutation(const program &prog, program &p_out, const param &params, std::mt19937 &rng)
Perform a hoist mutation on a random subtree of the given program (replace a subtree with a subtree o...
void execute(const raft::handle_t &h, const program_t &d_progs, const int n_rows, const int n_progs, const float *data, float *y_pred)
Calls the execution kernel to evaluate all programs on the given dataset.
int get_depth(const program &p_out)
Evaluates and returns the depth of the current program.
mutation_t
Definition: common.h:68
program * program_t
Definition: program.h:84
void set_batched_fitness(const raft::handle_t &h, int n_progs, program_t &d_progs, std::vector< program > &h_progs, const param &params, const int n_rows, const float *data, const float *y, const float *sample_weights)
Computes and sets the fitness scores for all programs on the given dataset.
void compute_metric(const raft::handle_t &h, int n_rows, int n_progs, const float *y, const float *y_pred, const float *w, float *score, const param &params)
Compute the loss based on the metric specified in the training hyperparameters. It performs a batched...
void point_mutation(const program &prog, program &p_out, const param &params, std::mt19937 &rng)
Perform a point mutation on the given program(AST)
Definition: common.h:26
Represents a node in the syntax tree.
Definition: node.h:38
contains all the hyper-parameters for training
Definition: common.h:94
The main data structure to store the AST that represents a program in the current generation.
Definition: program.h:31
float raw_fitness_
Definition: program.h:76
metric_t metric
Definition: program.h:78
program & operator=(const program &src)
assignment operator
node * nodes
Definition: program.h:70
~program()
Destroy the program object.
int depth
Definition: program.h:74
int len
Definition: program.h:72
program(const program &src)
Copy constructor for a new program object.
mutation_t mut_type
Definition: program.h:80