-
Notifications
You must be signed in to change notification settings - Fork 0
/
utils.c
executable file
·222 lines (203 loc) · 7.33 KB
/
utils.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
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
#include "utils.h"
Graph* disjoint_union(Graph* red, Graph* blue){
Graph *graph_copy = newGraph(100000, 100000);
int node_mapping_red[red->nodes.size];
int node_mapping_blue[blue->nodes.size];
//Copy nodes
for(int i = 0; i < red->nodes.size; i++){
Node *host_node = getNode(red, i);
if(host_node == NULL || host_node->index == -1) continue;
node_mapping_red[i] = addNode(graph_copy, host_node->root, makeHostLabel(1, host_node->label.length, copyHostList(host_node->label.list)));
}
for(int i = 0; i < blue->nodes.size; i++){
Node *host_node = getNode(blue, i);
if(host_node == NULL || host_node->index == -1) continue;
node_mapping_blue[i] = addNode(graph_copy, host_node->root, makeHostLabel(3, host_node->label.length, copyHostList(host_node->label.list)));
}
//Copy edges
for(int i = 0; i < red->edges.size; i++){
Edge *host_edge = getEdge(red, i);
if(host_edge == NULL || host_edge->index == -1) continue;
addEdge(graph_copy, makeHostLabel(1, host_edge->label.length, copyHostList(host_edge->label.list)), node_mapping_red[host_edge->source], node_mapping_red[host_edge->target]);
}
for(int i = 0; i < blue->edges.size; i++){
Edge *host_edge = getEdge(blue, i);
if(host_edge == NULL || host_edge->index == -1) continue;
addEdge(graph_copy, makeHostLabel(3, host_edge->label.length, copyHostList(host_edge->label.list)), node_mapping_blue[host_edge->source], node_mapping_blue[host_edge->target]);
}
return graph_copy;
}
Graph* get_red(Graph* red_blue){
return get_mark(red_blue, 1, 0);
}
Graph* get_blue(Graph* red_blue){
return get_mark(red_blue, 3, 0);
}
Graph* get_mark(Graph* multi_mark, int mark, int target_mark){
Graph *graph_copy = newGraph(100000, 100000);
int node_mapping[multi_mark->nodes.size];
//Copy nodes
for(int i = 0; i < multi_mark->nodes.size; i++){
Node *host_node = getNode(multi_mark, i);
if(host_node == NULL || host_node->index == -1 || host_node->label.mark != mark) continue;
node_mapping[i] = addNode(graph_copy, host_node->root, makeHostLabel(target_mark, host_node->label.length, copyHostList(host_node->label.list)));
}
//Copy edges
for(int i = 0; i < multi_mark->edges.size; i++){
Edge *host_edge = getEdge(multi_mark, i);
if(host_edge == NULL || host_edge->index == -1 || host_edge->label.mark != mark) continue;
addEdge(graph_copy, makeHostLabel(target_mark, host_edge->label.length, copyHostList(host_edge->label.list)), node_mapping[host_edge->source], node_mapping[host_edge->target]);
}
return graph_copy;
}
//Graph duplication function
Graph* duplicate_graph(Graph *graph)
{
Graph *graph_copy = newGraph(100000, 100000);
int node_mapping[graph->nodes.size];
//Copy nodes
for(int i = 0; i < graph->nodes.size; i++){
Node *host_node = getNode(graph, i);
if(host_node == NULL || host_node->index == -1) continue;
node_mapping[i] = addNode(graph_copy, host_node->root, makeHostLabel(host_node->label.mark, host_node->label.length, copyHostList(host_node->label.list)));
}
//Copy edges
for(int i = 0; i < graph->edges.size; i++){
Edge *host_edge = getEdge(graph, i);
if(host_edge == NULL || host_edge->index == -1) continue;
addEdge(graph_copy, makeHostLabel(host_edge->label.mark, host_edge->label.length, copyHostList(host_edge->label.list)), node_mapping[host_edge->source], node_mapping[host_edge->target]);
}
return graph_copy;
}
//Counts the active nodes in a GP individual.
int count_active_nodes(Graph* hostG, int inputs, int outputs){
mark_active_blue(hostG);
int count = 0;
for(int host_index = 0; host_index < hostG->nodes.size; host_index++)
{
Node *host_node = getNode(hostG, host_index);
if(host_node == NULL || host_node->index == -1) continue;
HostLabel label = host_node->label;
if(label.mark > 0){
count++;
}
}
unmark_graph(hostG);
if(count < (inputs + outputs)){
return 0;
}
return count - (inputs + outputs);
}
//Counts the active nodes in a GP individual.
int count_active_edges(Graph* hostG){
mark_active_blue(hostG);
int count = 0;
for(int host_index = 0; host_index < hostG->nodes.size; host_index++)
{
Node *host_node = getNode(hostG, host_index);
if(host_node == NULL || host_node->index == -1) continue;
HostLabel label = host_node->label;
if(label.mark > 0){
int counter = 0;
for(counter = 0; counter < host_node->out_edges.size + 2; counter++)
{
Edge *host_edge = getNthOutEdge(hostG, host_node, counter);
if(host_edge == NULL || host_edge->index == -1) continue;
count++;
}
}
}
unmark_graph(hostG);
return count;
}
void mark_active_blue(Graph* hostG){
for(int host_index = 0; host_index < hostG->nodes.size; host_index++)
{
Node *host_node = getNode(hostG, host_index);
if(host_node == NULL || host_node->index == -1) continue;
HostLabel label = host_node->label;
HostListItem *item = label.list->last;
if(item->atom.type != 's') break;
if(strcmp(item->atom.str, "OUT") == 0){
changeNodeMark(hostG, host_index, 3);
mark_active_children_blue(hostG, host_node);
}
}
}
void mark_active_children_blue(Graph* hostG, Node *node){
int counter;
for(counter = 0; counter < node->out_edges.size + 2; counter++)
{
Edge *host_edge = getNthOutEdge(hostG, node, counter);
if(host_edge == NULL || host_edge->index == -1) continue;
Node *target = getNode(hostG, host_edge->target);
HostLabel label = target->label;
if(label.mark == 0){
changeNodeMark(hostG, target->index, 3);
mark_active_children_blue(hostG, target);
}
}
}
void unmark_graph(Graph* hostG){
for(int host_index = 0; host_index < hostG->nodes.size; host_index++)
{
Node *host_node = getNode(hostG, host_index);
if(host_node == NULL || host_node->index == -1) continue;
if(host_node->label.mark != 0){
changeNodeMark(hostG, host_index, 0);
if(host_node->root){
host_node->root = false;
}
}
}
}
Graph* build_empty_host_graph()
{
Graph* new_host = newGraph(100000, 100000);
return new_host;
}
void free_graph_array(Graph** array, int graphs){
for(int i = 0; i < graphs; i++){
freeGraph(array[i]);
}
free(array);
}
void free_graph_data(Graph* graph){
if(graph == NULL) return;
printf("Freeing graph\n");
printfGraph(graph);
int index;
for(index = 0; index < graph->nodes.size; index++)
{
Node *node = getNode(graph, index);
if(node == NULL) continue;
if(node->out_edges.items != NULL) free(node->out_edges.items);
if(node->in_edges.items != NULL) free(node->in_edges.items);
removeHostList(node->label.list);
}
if(graph->nodes.holes.items) free(graph->nodes.holes.items);
if(graph->nodes.items) free(graph->nodes.items);
for(index = 0; index < graph->edges.size; index++)
{
Edge *edge = getEdge(graph, index);
if(edge == NULL) continue;
removeHostList(edge->label.list);
}
if(graph->edges.holes.items != NULL) free(graph->edges.holes.items);
if(graph->edges.items != NULL) free(graph->edges.items);
if(graph->root_nodes != NULL)
{
RootNodes *iterator = graph->root_nodes;
while(iterator != NULL)
{
RootNodes *temp = iterator;
iterator = iterator->next;
free(temp);
}
}
}
//Random integer from min (inclusive) to max (exclusive)
int random_int(int min, int max){
int nu_max = max - 1;
return min + rand() / (RAND_MAX / (nu_max - min + 1) + 1);
}