Line data Source code
1 : /**
2 : * @file gensvm_task.c
3 : * @author G.J.J. van den Burg
4 : * @date 2016-05-01
5 : * @brief Functions for initializing and freeing a GenTask
6 : *
7 : * @copyright
8 : Copyright 2016, G.J.J. van den Burg.
9 :
10 : This file is part of GenSVM.
11 :
12 : GenSVM is free software: you can redistribute it and/or modify
13 : it under the terms of the GNU General Public License as published by
14 : the Free Software Foundation, either version 3 of the License, or
15 : (at your option) any later version.
16 :
17 : GenSVM is distributed in the hope that it will be useful,
18 : but WITHOUT ANY WARRANTY; without even the implied warranty of
19 : MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
20 : GNU General Public License for more details.
21 :
22 : You should have received a copy of the GNU General Public License
23 : along with GenSVM. If not, see <http://www.gnu.org/licenses/>.
24 :
25 : */
26 :
27 : #include "gensvm_task.h"
28 :
29 : /**
30 : * @brief Initialize a GenTask structure
31 : *
32 : * @details
33 : * A GenTask structure is initialized and the default value for the
34 : * parameters are set. A pointer to the initialized GenTask is returned.
35 : *
36 : * @returns initialized GenTask
37 : */
38 113 : struct GenTask *gensvm_init_task(void)
39 : {
40 113 : struct GenTask *t = Malloc(struct GenTask, 1);
41 :
42 113 : t->kerneltype = K_LINEAR;
43 113 : t->weight_idx = 1;
44 113 : t->folds = 10;
45 113 : t->ID = -1;
46 113 : t->p = 1.0;
47 113 : t->kappa = 0.0;
48 113 : t->lambda = 1.0;
49 113 : t->epsilon = 1e-6;
50 113 : t->gamma = 1.0;
51 113 : t->coef = 0.0;
52 113 : t->degree = 2.0;
53 113 : t->train_data = NULL;
54 113 : t->test_data = NULL;
55 113 : t->performance = 0.0;
56 113 : t->max_iter = 1000000000;
57 :
58 113 : return t;
59 : }
60 :
61 : /**
62 : * @brief Free the GenTask struct
63 : *
64 : * @details
65 : * Freeing the allocated memory of the GenTask means freeing _only_ the task
66 : * itself. The datasets are not freed, as these are shared between all tasks.
67 : *
68 : * @param[in] t GenTask to be freed
69 : *
70 : */
71 113 : void gensvm_free_task(struct GenTask *t)
72 : {
73 113 : free(t);
74 113 : t = NULL;
75 113 : }
76 :
77 : /**
78 : * @brief Deepcopy a GenTask struct
79 : *
80 : * @details
81 : * Create a deep copy of a GenTask struct. Note that the datasets belonging to
82 : * the tasks are not copied, only the pointers to the datasets.
83 : *
84 : * @param[in] t input GenTask struct to copy
85 : *
86 : * @return a deepcopy of the input GenTask
87 : */
88 7 : struct GenTask *gensvm_copy_task(struct GenTask *t)
89 : {
90 7 : struct GenTask *nt = gensvm_init_task();
91 7 : nt->weight_idx = t->weight_idx;
92 7 : nt->folds = t->folds;
93 7 : nt->ID = t->ID;
94 7 : nt->p = t->p;
95 7 : nt->kappa = t->kappa;
96 7 : nt->lambda = t->lambda;
97 7 : nt->epsilon = t->epsilon;
98 7 : nt->train_data = t->train_data;
99 7 : nt->test_data = t->test_data;
100 7 : nt->performance = t->performance;
101 :
102 7 : nt->kerneltype = t->kerneltype;
103 7 : nt->gamma = t->gamma;
104 7 : nt->coef = t->coef;
105 7 : nt->degree = t->degree;
106 :
107 7 : nt->max_iter = t->max_iter;
108 :
109 7 : return nt;
110 : }
111 :
112 : /**
113 : * @brief Copy parameters from GenTask to GenModel
114 : *
115 : * @details
116 : * A GenTask struct only contains the parameters of the GenModel to be estimated.
117 : * This function is used to copy these parameters.
118 : *
119 : * @param[in] task GenTask instance with parameters
120 : * @param[in,out] model GenModel to which the parameters are copied
121 : */
122 4 : void gensvm_task_to_model(struct GenTask *task, struct GenModel *model)
123 : {
124 : // copy basic model parameters
125 4 : model->weight_idx = task->weight_idx;
126 4 : model->epsilon = task->epsilon;
127 4 : model->p = task->p;
128 4 : model->kappa = task->kappa;
129 4 : model->lambda = task->lambda;
130 :
131 : // copy kernel parameters
132 4 : model->kerneltype = task->kerneltype;
133 4 : model->gamma = task->gamma;
134 4 : model->coef = task->coef;
135 4 : model->degree = task->degree;
136 :
137 : // copy other parameters
138 4 : model->max_iter = task->max_iter;
139 4 : }
|