-
Notifications
You must be signed in to change notification settings - Fork 0
/
Sauvegarde.c
133 lines (126 loc) · 3.45 KB
/
Sauvegarde.c
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
#include "Sauvegarde.h"
void DecisionTree_SaveTree(DecisionTreeNode *node, FILE *file)
{
if (node == NULL || file == NULL)
return;
if (node->left != NULL)
{
fwrite("n", sizeof(char), 1, file);
fwrite(&node->split.featureID, sizeof(int), 1, file);
fwrite(&node->split.threshold, sizeof(float), 1, file);
DecisionTree_SaveTree(node->left, file);
DecisionTree_SaveTree(node->right, file);
}
else
{
fwrite("f", sizeof(char), 1, file);
fwrite(&node->classID, sizeof(int), 1, file);
}
}
void DecisionTree_File(DecisionTreeNode *tree, const char *filename)
{
if (tree == NULL)
abort();
FILE *file = fopen(filename, "wb+");
if (file == NULL)
{
printf("Le fichier n'a pas pu être ouvert");
abort();
}
DecisionTree_SaveTree(tree, file);
fclose(file);
}
DecisionTreeNode *DecisionTree_RecreateTree(FILE *file, DecisionTreeNode *tree)
{
if (file == NULL)
{
printf("Fichier non-existant");
return NULL;
}
char type;
fread(&type, sizeof(char), 1, file);
DecisionTreeNode *node = (DecisionTreeNode *)calloc(1, sizeof(DecisionTreeNode));
if (type == 'n')
{
fread(&node->split.featureID, sizeof(int), 1, file);
fread(&node->split.threshold, sizeof(float), 1, file);
node->left = DecisionTree_RecreateTree(file, node->left);
node->right = DecisionTree_RecreateTree(file, node->right);
return node;
}
else if (type == 'f')
{
fread(&node->classID, sizeof(int), 1, file);
return node;
}
// else if (type == '\n')
// {
// free(node);
// return NULL;
// }
else
{
printf("Erreur de lecture/d'écriture");
return NULL;
}
}
DecisionTreeNode *DecisionTree_GetinFile(FILE *file)
{
if (file == NULL)
{
printf("Fichier non-existant");
return NULL;
}
DecisionTreeNode *tree = DecisionTree_RecreateTree(file, NULL);
return tree;
}
void RandomForest_File(RandomForest *forest, const char *filename)
{
if (forest == NULL)
abort();
FILE *file = fopen(filename, "wb+");
if (file == NULL)
{
printf("Le fichier n'a pas pu être ouvert");
abort();
}
RandomForest_SaveForest(forest, file);
fclose(file);
}
RandomForest *RandomForest_GetinFile(FILE *file)
{
if (file == NULL)
{
printf("Fichier non-existant");
return NULL;
}
int treeCount = 0;
int classCount = 0;
fread(&treeCount, sizeof(int), 1, file);
fread(&classCount, sizeof(int), 1, file);
RandomForest *forest = (RandomForest *)calloc(1, sizeof(RandomForest));
forest->trees = (DecisionTreeNode **)calloc(treeCount, sizeof(DecisionTreeNode *));
for (int i = 0; i < treeCount; i++)
{
forest->trees[i] = DecisionTree_GetinFile(file);
if (forest->trees[i] == NULL)
{
printf("Erreur de lecture/d'écriture sur l'arbre %d\n", i);
return NULL;
}
}
forest->treeCount = treeCount;
forest->classCount = classCount;
return forest;
}
void RandomForest_SaveForest(RandomForest *forest, FILE *file)
{
if (forest == NULL || file == NULL)
return;
fwrite(&forest->treeCount, sizeof(int), 1, file);
fwrite(&forest->classCount, sizeof(int), 1, file);
for (int i = 0; i < forest->treeCount; i++)
{
DecisionTree_SaveTree(forest->trees[i], file);
}
}