program.h
Go to the documentation of this file.
1 /*
2  * SPDX-FileCopyrightText: Copyright (c) 2021-2024, NVIDIA CORPORATION.
3  * SPDX-License-Identifier: Apache-2.0
4  */
5 
6 #pragma once
7 
8 #include "common.h"
9 
10 #include <raft/core/handle.hpp>
11 
12 #include <random>
13 
14 namespace cuml {
15 namespace genetic {
16 
21 struct program {
36  explicit program();
37 
43 
49  explicit program(const program& src);
50 
58  program& operator=(const program& src);
59 
62  int len;
64  int depth;
66  float raw_fitness_;
71 }; // struct program
72 
74 typedef program* program_t;
75 
86 void execute(const raft::handle_t& h,
87  const program_t& d_progs,
88  const int n_rows,
89  const int n_progs,
90  const float* data,
91  float* y_pred);
92 
106 void compute_metric(const raft::handle_t& h,
107  int n_rows,
108  int n_progs,
109  const float* y,
110  const float* y_pred,
111  const float* w,
112  float* score,
113  const param& params);
114 
127 void find_fitness(const raft::handle_t& h,
128  program_t& d_prog,
129  float* score,
130  const param& params,
131  const int n_rows,
132  const float* data,
133  const float* y,
134  const float* sample_weights);
135 
149 void find_batched_fitness(const raft::handle_t& h,
150  int n_progs,
151  program_t& d_progs,
152  float* score,
153  const param& params,
154  const int n_rows,
155  const float* data,
156  const float* y,
157  const float* sample_weights);
158 
171 void set_fitness(const raft::handle_t& h,
172  program_t& d_prog,
173  program& h_prog,
174  const param& params,
175  const int n_rows,
176  const float* data,
177  const float* y,
178  const float* sample_weights);
179 
193 void set_batched_fitness(const raft::handle_t& h,
194  int n_progs,
195  program_t& d_progs,
196  std::vector<program>& h_progs,
197  const param& params,
198  const int n_rows,
199  const float* data,
200  const float* y,
201  const float* sample_weights);
202 
211 float get_fitness(const program& prog, const param& params);
212 
219 int get_depth(const program& p_out);
220 
228 void build_program(program& p_out, const param& params, std::mt19937& rng);
229 
238 void point_mutation(const program& prog, program& p_out, const param& params, std::mt19937& rng);
239 
250 void crossover(const program& prog,
251  const program& donor,
252  program& p_out,
253  const param& params,
254  std::mt19937& rng);
255 
266 void subtree_mutation(const program& prog, program& p_out, const param& params, std::mt19937& rng);
267 
277 void hoist_mutation(const program& prog, program& p_out, const param& params, std::mt19937& rng);
278 } // namespace genetic
279 } // namespace cuml
RF_metrics score(const raft::handle_t &user_handle, const RandomForestClassifierF *forest, const int *ref_labels, int n_rows, const int *predictions, rapids_logger::level_enum verbosity=rapids_logger::level_enum::info)
void crossover(const program &prog, const program &donor, program &p_out, const param ¶ms, 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 ¶ms, std::mt19937 &rng)
Build a random program with depth atmost 10.
float get_fitness(const program &prog, const param ¶ms)
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 ¶ms, 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 ¶ms, std::mt19937 &rng)
Performs a crossover mutation with a randomly built new program. Since crossover is 'hoisted',...
metric_t
Definition: common.h:19
void find_fitness(const raft::handle_t &h, program_t &d_prog, float *score, const param ¶ms, 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 ¶ms, 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 ¶ms, 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:57
program * program_t
Definition: program.h:74
void set_batched_fitness(const raft::handle_t &h, int n_progs, program_t &d_progs, std::vector< program > &h_progs, const param ¶ms, 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 ¶ms)
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 ¶ms, std::mt19937 &rng)
Perform a point mutation on the given program(AST)
Definition: common.h:15
Represents a node in the syntax tree.
Definition: node.h:27
contains all the hyper-parameters for training
Definition: common.h:83
The main data structure to store the AST that represents a program in the current generation.
Definition: program.h:21
float raw_fitness_
Definition: program.h:66
metric_t metric
Definition: program.h:68
program & operator=(const program &src)
assignment operator
node * nodes
Definition: program.h:60
~program()
Destroy the program object.
int depth
Definition: program.h:64
int len
Definition: program.h:62
program(const program &src)
Copy constructor for a new program object.
mutation_t mut_type
Definition: program.h:70