configuration_space.c 36.7 KB
Newer Older
1
2
#include "cconfigspace_internal.h"
#include "configuration_space_internal.h"
3
#include "configuration_internal.h"
4
#include "utlist.h"
5

6
7
8
static ccs_error_t
_generate_constraints(ccs_configuration_space_t configuration_space);

9
10
11
12
13
14
15
16
17
18
19
20
static inline _ccs_configuration_space_ops_t *
ccs_configuration_space_get_ops(ccs_configuration_space_t configuration_space) {
	return (_ccs_configuration_space_ops_t *)configuration_space->obj.ops;
}

static ccs_error_t
_ccs_configuration_space_del(ccs_object_t object) {
	ccs_configuration_space_t configuration_space = (ccs_configuration_space_t)object;
	UT_array *array = configuration_space->data->hyperparameters;
	_ccs_hyperparameter_wrapper_t *wrapper = NULL;
	while ( (wrapper = (_ccs_hyperparameter_wrapper_t *)utarray_next(array, wrapper)) ) {
		ccs_release_object(wrapper->hyperparameter);
21
22
		if (wrapper->condition)
			ccs_release_object(wrapper->condition);
Brice Videau's avatar
Brice Videau committed
23
24
		utarray_free(wrapper->parents);
		utarray_free(wrapper->children);
25
26
27
28
29
	}
	array = configuration_space->data->forbidden_clauses;
	ccs_expression_t *expr = NULL;
	while ( (expr = (ccs_expression_t *)utarray_next(array, expr)) ) {
		ccs_release_object(*expr);
30
31
	}
	HASH_CLEAR(hh_name, configuration_space->data->name_hash);
32
	HASH_CLEAR(hh_handle, configuration_space->data->handle_hash);
33
34
	utarray_free(configuration_space->data->hyperparameters);
	utarray_free(configuration_space->data->forbidden_clauses);
Brice Videau's avatar
Brice Videau committed
35
	utarray_free(configuration_space->data->sorted_indexes);
36
37
38
39
40
41
42
	_ccs_distribution_wrapper_t *dw;
	_ccs_distribution_wrapper_t *tmp;
	DL_FOREACH_SAFE(configuration_space->data->distribution_list, dw, tmp) {
		DL_DELETE(configuration_space->data->distribution_list, dw);
		ccs_release_object(dw->distribution);
		free(dw);
	}
43
44
45
46
	ccs_release_object(configuration_space->data->rng);
	return CCS_SUCCESS;
}

47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
static ccs_error_t
_ccs_configuration_space_get_hyperparameter_index(_ccs_context_data_t *data,
                                                  ccs_hyperparameter_t  hyperparameter,
                                                  size_t               *index_ret) {
	_ccs_configuration_space_data_t *csdata =
		(_ccs_configuration_space_data_t *)data;
	_ccs_hyperparameter_wrapper_t *wrapper;
	HASH_FIND(hh_handle, csdata->handle_hash, &hyperparameter,
	          sizeof(ccs_hyperparameter_t), wrapper);
	if (!wrapper)
		return -CCS_INVALID_HYPERPARAMETER;
	*index_ret = wrapper->index;
	return CCS_SUCCESS;
}

62
static _ccs_configuration_space_ops_t _configuration_space_ops =
63
64
    { { {&_ccs_configuration_space_del},
	 &_ccs_configuration_space_get_hyperparameter_index} };
65
66
67
68
69
70
71
72

static const UT_icd _hyperparameter_wrapper_icd = {
	sizeof(_ccs_hyperparameter_wrapper_t),
	NULL,
	NULL,
	NULL,
};

73
74
75
76
77
78
79
static const UT_icd _forbidden_clauses_icd = {
	sizeof(ccs_expression_t),
	NULL,
	NULL,
	NULL,
};

Brice Videau's avatar
Brice Videau committed
80
81
82
83
84
85
86
static UT_icd _size_t_icd = {
	sizeof(size_t),
	NULL,
	NULL,
	NULL
};

87
88
89
#undef  utarray_oom
#define utarray_oom() { \
	ccs_release_object(config_space->data->rng); \
90
91
	err = -CCS_ENOMEM; \
	goto arrays; \
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
}
ccs_error_t
ccs_create_configuration_space(const char                *name,
                               void                      *user_data,
                               ccs_configuration_space_t *configuration_space_ret) {
	if (!name || !configuration_space_ret)
		return -CCS_INVALID_VALUE;
	uintptr_t mem = (uintptr_t)calloc(1, sizeof(struct _ccs_configuration_space_s) + sizeof(struct _ccs_configuration_space_data_s) + strlen(name) + 1);
	if (!mem)
		return -CCS_ENOMEM;
	ccs_rng_t rng;
	ccs_error_t err = ccs_rng_create(&rng);
	if (err) {
		free((void *)mem);
		return err;
	}

	ccs_configuration_space_t config_space = (ccs_configuration_space_t)mem;
	_ccs_object_init(&(config_space->obj), CCS_CONFIGURATION_SPACE, (_ccs_object_ops_t *)&_configuration_space_ops);
	config_space->data = (struct _ccs_configuration_space_data_s*)(mem + sizeof(struct _ccs_configuration_space_s));
	config_space->data->name = (const char *)(mem + sizeof(struct _ccs_configuration_space_s) + sizeof(struct _ccs_configuration_space_data_s));
	config_space->data->user_data = user_data;
	config_space->data->rng = rng;
	utarray_new(config_space->data->hyperparameters, &_hyperparameter_wrapper_icd);
116
	utarray_new(config_space->data->forbidden_clauses, &_forbidden_clauses_icd);
Brice Videau's avatar
Brice Videau committed
117
	utarray_new(config_space->data->sorted_indexes, &_size_t_icd);
Brice Videau's avatar
Brice Videau committed
118
	config_space->data->graph_ok = CCS_TRUE;
119
120
121
	strcpy((char *)(config_space->data->name), name);
	*configuration_space_ret = config_space;
	return CCS_SUCCESS;
122
arrays:
Brice Videau's avatar
Brice Videau committed
123
	if (config_space->data->hyperparameters)
124
		utarray_free(config_space->data->hyperparameters);
Brice Videau's avatar
Brice Videau committed
125
	if (config_space->data->forbidden_clauses)
126
		utarray_free(config_space->data->forbidden_clauses);
Brice Videau's avatar
Brice Videau committed
127
128
	if (config_space->data->sorted_indexes)
		utarray_free(config_space->data->sorted_indexes);
129
130
	free((void *)mem);
	return err;
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
}

ccs_error_t
ccs_configuration_space_get_name(ccs_configuration_space_t   configuration_space,
                                 const char                **name_ret) {
	if (!configuration_space || !configuration_space->data)
		return -CCS_INVALID_OBJECT;
	if (!name_ret)
		return -CCS_INVALID_VALUE;
	*name_ret = configuration_space->data->name;
	return CCS_SUCCESS;
}

ccs_error_t
ccs_configuration_space_get_user_data(ccs_configuration_space_t   configuration_space,
                                      void                      **user_data_ret) {
	if (!configuration_space || !configuration_space->data)
		return -CCS_INVALID_OBJECT;
	if (!user_data_ret)
		return -CCS_INVALID_VALUE;
	*user_data_ret = configuration_space->data->user_data;
	return CCS_SUCCESS;
}

ccs_error_t
ccs_configuration_space_set_rng(ccs_configuration_space_t configuration_space,
                                ccs_rng_t                 rng) {
	if (!configuration_space || !configuration_space->data || !rng)
		return -CCS_INVALID_OBJECT;
	ccs_error_t err;
	err = ccs_release_object(configuration_space->data->rng);
	if(err)
		return err;
	configuration_space->data->rng = rng;
	return CCS_SUCCESS;
}

ccs_error_t
ccs_configuration_space_get_rng(ccs_configuration_space_t  configuration_space,
                                ccs_rng_t                 *rng_ret) {
	if (!configuration_space || !configuration_space->data)
		return -CCS_INVALID_OBJECT;
	if (!rng_ret)
		return -CCS_INVALID_VALUE;
	*rng_ret = configuration_space->data->rng;
	return CCS_SUCCESS;
}


#undef  utarray_oom
#define utarray_oom() { \
	err = -CCS_ENOMEM; \
183
	goto errordistrib_wrapper; \
184
185
186
187
188
189
190
191
192
193
194
195
196
}
#undef uthash_nonfatal_oom
#define uthash_nonfatal_oom(elt) { \
	err = -CCS_ENOMEM; \
	goto errorutarray; \
}
ccs_error_t
ccs_configuration_space_add_hyperparameter(ccs_configuration_space_t configuration_space,
                                           ccs_hyperparameter_t      hyperparameter,
                                           ccs_distribution_t        distribution) {
	if (!configuration_space || !configuration_space->data || !hyperparameter)
		return -CCS_INVALID_OBJECT;
	ccs_error_t err;
197
198
199
200
201
202
203
204
205
206
207
208
209
	const char *name;
	size_t sz_name;
	_ccs_hyperparameter_wrapper_t *p_hyper_wrapper;
	err = ccs_hyperparameter_get_name(hyperparameter, &name);
	if (err)
		goto error;
	sz_name = strlen(name);
	HASH_FIND(hh_name, configuration_space->data->name_hash,
	          name, sz_name, p_hyper_wrapper);
	if (p_hyper_wrapper) {
		err = -CCS_INVALID_HYPERPARAMETER;
		goto error;
	}
210
211
	UT_array *hyperparameters;
	unsigned int index;
212
	size_t dimension;
213
	_ccs_hyperparameter_wrapper_t hyper_wrapper;
214
215
	_ccs_distribution_wrapper_t *distrib_wrapper;
	uintptr_t pmem;
216
	hyper_wrapper.hyperparameter = hyperparameter;
217
218
219
220
	err = ccs_retain_object(hyperparameter);
	if (err)
		goto error;

221
	if (distribution) {
222
223
224
225
226
227
228
229
230
231
		err = ccs_distribution_get_dimension(distribution, &dimension);
		if (err)
			goto errorhyper;
		if (dimension != 1) {
			err = -CCS_INVALID_DISTRIBUTION;
			goto errorhyper;
		}
		err = ccs_retain_object(distribution);
		if (err)
			goto errorhyper;
232
	} else {
233
		err = ccs_hyperparameter_get_default_distribution(hyperparameter, &distribution);
234
		if (err)
235
236
237
238
239
240
241
			goto errorhyper;
		dimension = 1;
	}
	pmem = (uintptr_t)malloc(sizeof(_ccs_distribution_wrapper_t) + sizeof(size_t)*dimension);
	if (!pmem) {
		err = -CCS_ENOMEM;
		goto errordistrib;
242
	}
243
244
245
246
        distrib_wrapper = (_ccs_distribution_wrapper_t *)pmem;
	distrib_wrapper->distribution = distribution;
	distrib_wrapper->dimension = dimension;
	distrib_wrapper->hyperparameter_indexes = (size_t *)(pmem + sizeof(_ccs_distribution_wrapper_t));
247
248
	hyperparameters = configuration_space->data->hyperparameters;
	index = utarray_len(hyperparameters);
249
	distrib_wrapper->hyperparameter_indexes[0] = index;
250
	hyper_wrapper.index = index;
251
252
253
	hyper_wrapper.distribution_index = 0;
	hyper_wrapper.distribution = distrib_wrapper;
	hyper_wrapper.name = name;
254
	hyper_wrapper.condition = NULL;
Brice Videau's avatar
Brice Videau committed
255
256
257
258
	hyper_wrapper.parents = NULL;
	hyper_wrapper.children = NULL;
	utarray_new(hyper_wrapper.parents, &_size_t_icd);
	utarray_new(hyper_wrapper.children, &_size_t_icd);
259
	utarray_push_back(hyperparameters, &hyper_wrapper);
Brice Videau's avatar
Brice Videau committed
260
	utarray_push_back(configuration_space->data->sorted_indexes, &(hyper_wrapper.index));
261

262
	p_hyper_wrapper =
263
264
	   (_ccs_hyperparameter_wrapper_t*)utarray_eltptr(hyperparameters, index);
	HASH_ADD_KEYPTR( hh_name, configuration_space->data->name_hash,
265
	                 name, sz_name, p_hyper_wrapper );
266
267
268
	HASH_ADD( hh_handle, configuration_space->data->handle_hash,
	          hyperparameter, sizeof(ccs_hyperparameter_t), p_hyper_wrapper );
	DL_APPEND( configuration_space->data->distribution_list, distrib_wrapper );
269
270
271
272

	return CCS_SUCCESS;
errorutarray:
	utarray_pop_back(hyperparameters);
273
errordistrib_wrapper:
Brice Videau's avatar
Brice Videau committed
274
275
276
277
	if (hyper_wrapper.parents)
		utarray_free(hyper_wrapper.parents);
	if (hyper_wrapper.children)
		utarray_free(hyper_wrapper.children);
278
	free(distrib_wrapper);
279
errordistrib:
280
281
282
	ccs_release_object(distribution);
errorhyper:
	ccs_release_object(hyperparameter);
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
error:
	return err;
}
#undef  utarray_oom
#define utarray_oom() exit(-1)

ccs_error_t
ccs_configuration_space_add_hyperparameters(ccs_configuration_space_t  configuration_space,
                                            size_t                     num_hyperparameters,
                                            ccs_hyperparameter_t      *hyperparameters,
                                            ccs_distribution_t        *distributions) {
	if (!configuration_space)
		return -CCS_INVALID_OBJECT;
	if (num_hyperparameters > 0 && !hyperparameters)
		return -CCS_INVALID_VALUE;
	for (size_t i = 0; i < num_hyperparameters; i++) {
		ccs_distribution_t distribution = NULL;
		if (distributions)
			distribution = distributions[i];
		ccs_error_t err =
		    ccs_configuration_space_add_hyperparameter( configuration_space,
		                                                hyperparameters[i],
		                                                distribution );
		if (err)
			return err;
	}
	return CCS_SUCCESS;
}

ccs_error_t
ccs_configuration_space_get_num_hyperparameters(ccs_configuration_space_t  configuration_space,
                                                size_t                     *num_hyperparameters_ret) {
	if (!configuration_space || !configuration_space->data)
		return -CCS_INVALID_OBJECT;
	if (!num_hyperparameters_ret)
		return -CCS_INVALID_VALUE;
	*num_hyperparameters_ret = utarray_len(configuration_space->data->hyperparameters);
	return CCS_SUCCESS;
}

ccs_error_t
ccs_configuration_space_get_hyperparameter(ccs_configuration_space_t  configuration_space,
                                           size_t                     index,
                                           ccs_hyperparameter_t      *hyperparameter_ret) {
	if (!configuration_space || !configuration_space->data)
		return -CCS_INVALID_OBJECT;
	if (!hyperparameter_ret)
		return -CCS_INVALID_VALUE;
	_ccs_hyperparameter_wrapper_t *wrapper = (_ccs_hyperparameter_wrapper_t*)
	    utarray_eltptr(configuration_space->data->hyperparameters, (unsigned int)index);
333
334
335
	if (!wrapper)
		return -CCS_OUT_OF_BOUNDS;
	*hyperparameter_ret = wrapper->hyperparameter;
336
337
338
339
340
341
342
343
344
345
346
	return CCS_SUCCESS;
}

ccs_error_t
ccs_configuration_space_get_hyperparameter_by_name(
		ccs_configuration_space_t  configuration_space,
		const char *               name,
		ccs_hyperparameter_t      *hyperparameter_ret) {
	if (!configuration_space || !configuration_space->data)
		return -CCS_INVALID_OBJECT;
	if (!hyperparameter_ret)
347
		return -CCS_INVALID_VALUE;
348
349
350
351
352
	_ccs_hyperparameter_wrapper_t *wrapper;
	size_t sz_name;
	sz_name = strlen(name);
	HASH_FIND(hh_name, configuration_space->data->name_hash,
	          name, sz_name, wrapper);
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
	if (!wrapper)
		return -CCS_INVALID_NAME;
	*hyperparameter_ret = wrapper->hyperparameter;
	return CCS_SUCCESS;
}

ccs_error_t
ccs_configuration_space_get_hyperparameter_index_by_name(
		ccs_configuration_space_t  configuration_space,
		const char                *name,
		size_t                    *index_ret) {
	if (!configuration_space || !configuration_space->data)
		return -CCS_INVALID_OBJECT;
	if (!index_ret)
		return -CCS_INVALID_VALUE;
	_ccs_hyperparameter_wrapper_t *wrapper;
	size_t sz_name;
	sz_name = strlen(name);
	HASH_FIND(hh_name, configuration_space->data->name_hash,
	          name, sz_name, wrapper);
	if (!wrapper)
		return -CCS_INVALID_NAME;
	*index_ret = wrapper->index;
376
377
378
	return CCS_SUCCESS;
}

379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
ccs_error_t
ccs_configuration_space_get_hyperparameter_index(
		ccs_configuration_space_t  configuration_space,
		ccs_hyperparameter_t       hyperparameter,
		size_t                    *index_ret) {
	if (!configuration_space || !configuration_space->data)
		return -CCS_INVALID_OBJECT;
	if (!hyperparameter)
		return -CCS_INVALID_HYPERPARAMETER;
	if (!index_ret)
		return -CCS_INVALID_VALUE;
	_ccs_hyperparameter_wrapper_t *wrapper;
	HASH_FIND(hh_handle, configuration_space->data->handle_hash,
	          &hyperparameter, sizeof(ccs_hyperparameter_t), wrapper);
	if (!wrapper)
		return -CCS_INVALID_HYPERPARAMETER;
	*index_ret = wrapper->index;
	return CCS_SUCCESS;
}

Brice Videau's avatar
Brice Videau committed
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
ccs_error_t
ccs_configuration_space_get_hyperparameter_indexes(
		ccs_configuration_space_t  configuration_space,
		size_t                     num_hyperparameters,
		ccs_hyperparameter_t      *hyperparameters,
		size_t                    *indexes) {
	if (!configuration_space || !configuration_space->data)
		return -CCS_INVALID_OBJECT;
	if (num_hyperparameters && (!hyperparameters || !indexes ))
		return -CCS_INVALID_VALUE;
	_ccs_hyperparameter_wrapper_t *wrapper;
	for(size_t i = 0; i < num_hyperparameters; i++) {
		HASH_FIND(hh_handle, configuration_space->data->handle_hash,
			hyperparameters + i, sizeof(ccs_hyperparameter_t), wrapper);
		if (!wrapper)
			return -CCS_INVALID_HYPERPARAMETER;
		indexes[i] = wrapper->index;
	}
	return CCS_SUCCESS;
}

420
ccs_error_t
421
422
423
424
425
ccs_configuration_space_get_hyperparameters(
		ccs_configuration_space_t  configuration_space,
		size_t                     num_hyperparameters,
		ccs_hyperparameter_t      *hyperparameters,
		size_t                    *num_hyperparameters_ret) {
426
427
428
429
430
431
432
433
434
435
436
	if (!configuration_space || !configuration_space->data)
		return -CCS_INVALID_OBJECT;
	if (num_hyperparameters && !hyperparameters)
		return -CCS_INVALID_VALUE;
	if (hyperparameters && !num_hyperparameters)
		return -CCS_INVALID_VALUE;
	if (!num_hyperparameters_ret && !hyperparameters)
		return -CCS_INVALID_VALUE;
	UT_array *array = configuration_space->data->hyperparameters;
	size_t size = utarray_len(array);
	if (hyperparameters) {
437
438
		if (num_hyperparameters < size)
			return -CCS_INVALID_VALUE;
439
440
441
		_ccs_hyperparameter_wrapper_t *wrapper = NULL;
		size_t index = 0;
		while ( (wrapper = (_ccs_hyperparameter_wrapper_t *)utarray_next(array, wrapper)) )
442
			hyperparameters[index++] = wrapper->hyperparameter;
443
444
445
446
447
448
449
450
		for (size_t i = size; i < num_hyperparameters; i++)
			hyperparameters[i] = NULL;
	}
	if (num_hyperparameters_ret)
		*num_hyperparameters_ret = size;
	return CCS_SUCCESS;
}

451
452
453
454
455
456
457
458
459
ccs_error_t
ccs_configuration_space_get_default_configuration(ccs_configuration_space_t  configuration_space,
                                                  ccs_configuration_t       *configuration_ret) {
	if (!configuration_space || !configuration_space->data)
		return -CCS_INVALID_OBJECT;
	if (!configuration_ret)
		return -CCS_INVALID_VALUE;
	ccs_error_t err;
	ccs_configuration_t config;
460
	err = ccs_create_configuration(configuration_space, 0, NULL, NULL, &config);
461
462
463
464
	if (err)
		return err;
	UT_array *array = configuration_space->data->hyperparameters;
	_ccs_hyperparameter_wrapper_t *wrapper = NULL;
465
	ccs_datum_t *values = config->data->values;
466
	while ( (wrapper = (_ccs_hyperparameter_wrapper_t *)utarray_next(array, wrapper)) ) {
467
468
469
470
471
472
		err = ccs_hyperparameter_get_default_value(wrapper->hyperparameter,
		                                           values++);
		if (unlikely(err)) {
			ccs_release_object(config);
			return err;
		}
473
474
475
	}
	*configuration_ret = config;
	return CCS_SUCCESS;
476
477
}

Brice Videau's avatar
Brice Videau committed
478
static ccs_error_t
479
480
_set_actives(ccs_configuration_space_t configuration_space,
             ccs_configuration_t       configuration) {
Brice Videau's avatar
Brice Videau committed
481
	size_t *p_index = NULL;
482
	UT_array *indexes = configuration_space->data->sorted_indexes;
Brice Videau's avatar
Brice Videau committed
483
484
	UT_array *array = configuration_space->data->hyperparameters;
	ccs_datum_t *values = configuration->data->values;
485
	while ( (p_index = (size_t *)utarray_next(indexes, p_index)) ) {
Brice Videau's avatar
Brice Videau committed
486
487
		_ccs_hyperparameter_wrapper_t *wrapper = NULL;
		wrapper = (_ccs_hyperparameter_wrapper_t *)utarray_eltptr(array, *p_index);
488
		if (!wrapper->condition) {
Brice Videau's avatar
Brice Videau committed
489
			continue;
490
		}
Brice Videau's avatar
Brice Videau committed
491
492
493
		UT_array *parents = wrapper->parents;
		size_t *p_parent = NULL;
		while ( (p_parent = (size_t*)utarray_next(parents, p_parent)) ) {
494
495
			if (values[*p_parent].type == CCS_INACTIVE) {
				values[*p_index] = ccs_inactive;
Brice Videau's avatar
Brice Videau committed
496
497
498
				break;
			}
		}
499
		if (values[*p_index].type == CCS_INACTIVE)
Brice Videau's avatar
Brice Videau committed
500
501
502
			continue;
		ccs_datum_t result;
		ccs_error_t err;
503
504
		err = ccs_expression_eval(wrapper->condition,
		                          (ccs_context_t)configuration_space,
Brice Videau's avatar
Brice Videau committed
505
506
507
		                          values, &result);
		if (err)
			return err;
508
509
		if (!(result.type == CCS_BOOLEAN && result.value.i == CCS_TRUE))
			values[*p_index] = ccs_inactive;
510
511
512
513
	}
	return CCS_SUCCESS;
}

514
515
516
517
518
519
520
521
522
523
524
static ccs_error_t
_test_forbidden(ccs_configuration_space_t  configuration_space,
                ccs_datum_t               *values,
		ccs_bool_t                *is_valid) {
	ccs_error_t err;
	UT_array *array = configuration_space->data->forbidden_clauses;
	ccs_expression_t *p_expression = NULL;
	*is_valid = CCS_FALSE;
	while ( (p_expression = (ccs_expression_t *)
	               utarray_next(array, p_expression)) ) {
		ccs_datum_t result;
525
526
		err = ccs_expression_eval(*p_expression,
		                          (ccs_context_t)configuration_space,
527
		                          values, &result);
528
		if (err == -CCS_INACTIVE_HYPERPARAMETER)
529
530
531
532
533
534
535
536
537
538
			continue;
		else if (err)
			return err;
		if (result.type == CCS_BOOLEAN && result.value.i == CCS_TRUE)
			return CCS_SUCCESS;
	}
	*is_valid = CCS_TRUE;
	return CCS_SUCCESS;
}

539
540
541
static inline ccs_error_t
_check_configuration(ccs_configuration_space_t  configuration_space,
                     size_t                     num_values,
542
                     ccs_datum_t               *values) {
543
	ccs_error_t err;
544
545
546
547
548
549
550
551
552
553
554
555
556
	UT_array *indexes = configuration_space->data->sorted_indexes;
	UT_array *array = configuration_space->data->hyperparameters;
	if (num_values != utarray_len(array))
		return -CCS_INVALID_CONFIGURATION;
	size_t *p_index = NULL;
	while ( (p_index = (size_t *)utarray_next(indexes, p_index)) ) {
		ccs_bool_t active = CCS_TRUE;
		_ccs_hyperparameter_wrapper_t *wrapper = NULL;
		wrapper = (_ccs_hyperparameter_wrapper_t *)utarray_eltptr(array, *p_index);
		if (wrapper->condition) {
			UT_array *parents = wrapper->parents;
			size_t *p_parent = NULL;
			while ( (p_parent = (size_t*)utarray_next(parents, p_parent)) ) {
557
				if (values[*p_parent].type == CCS_INACTIVE) {
558
559
560
561
562
563
564
					active = CCS_FALSE;
					break;
				}
			}
			if (active) {
				ccs_datum_t result;
				ccs_error_t err;
565
566
567
568
				err = ccs_expression_eval(
					wrapper->condition,
					(ccs_context_t)configuration_space,
					values, &result);
569
570
571
572
573
574
575
				if (err)
					return err;
				if (!(result.type == CCS_BOOLEAN && result.value.i == CCS_TRUE)) {
					active = CCS_FALSE;
				}
			}
		}
576
		if (active != (values[*p_index].type == CCS_INACTIVE ? CCS_FALSE : CCS_TRUE))
577
578
579
580
581
582
583
584
585
			return -CCS_INVALID_CONFIGURATION;
		if (active) {
			ccs_bool_t res;
			err = ccs_hyperparameter_check_value(wrapper->hyperparameter,
		                                             values[*p_index], &res);
			if (unlikely(err))
				return err;
			if (res == CCS_FALSE)
				return -CCS_INVALID_CONFIGURATION;
Brice Videau's avatar
Brice Videau committed
586
587
		}
	}
588
589
590
591
592
593
	ccs_bool_t valid;
	err = _test_forbidden(configuration_space, values, &valid);
	if (err)
		return err;
	if (!valid)
		return -CCS_INVALID_CONFIGURATION;
Brice Videau's avatar
Brice Videau committed
594
595
596
	return CCS_SUCCESS;
}

597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
ccs_error_t
ccs_configuration_space_check_configuration(ccs_configuration_space_t configuration_space,
                                            ccs_configuration_t       configuration) {
	if (!configuration_space || !configuration_space->data)
		return -CCS_INVALID_OBJECT;
	if (!configuration || !configuration->data)
		return -CCS_INVALID_OBJECT;
	if (configuration->data->configuration_space != configuration_space)
		return -CCS_INVALID_CONFIGURATION;
	if (!configuration_space->data->graph_ok) {
		ccs_error_t err;
		err = _generate_constraints(configuration_space);
		if (err)
			return err;
	}
	return _check_configuration(configuration_space,
	                            configuration->data->num_values,
614
	                            configuration->data->values);
615
616
617
618
619
}

ccs_error_t
ccs_configuration_space_check_configuration_values(ccs_configuration_space_t  configuration_space,
                                                   size_t                     num_values,
620
                                                   ccs_datum_t               *values) {
621
622
	if (!configuration_space || !configuration_space->data)
		return -CCS_INVALID_OBJECT;
623
	if (!values)
624
625
626
627
628
629
630
		return -CCS_INVALID_VALUE;
	if (!configuration_space->data->graph_ok) {
		ccs_error_t err;
		err = _generate_constraints(configuration_space);
		if (err)
			return err;
	}
631
	return _check_configuration(configuration_space, num_values, values);
632
633
}

634
635
636
637
638
639
640
641
642
643
644
645
646
647
648

static ccs_error_t
_sample(ccs_configuration_space_t  configuration_space,
	ccs_configuration_t        config,
	ccs_bool_t                *found) {
	ccs_error_t err;
	ccs_rng_t rng = configuration_space->data->rng;
	UT_array *array = configuration_space->data->hyperparameters;
	_ccs_hyperparameter_wrapper_t *wrapper = NULL;
	ccs_datum_t *values = config->data->values;
	while ( (wrapper = (_ccs_hyperparameter_wrapper_t *)
	                       utarray_next(array, wrapper)) ) {
		err = ccs_hyperparameter_sample(wrapper->hyperparameter,
		                                wrapper->distribution->distribution,
		                                rng, values++);
649
		if (unlikely(err))
650
651
652
			return err;
	}
	err = _set_actives(configuration_space, config);
653
	if (err)
654
655
		return err;
	err = _test_forbidden(configuration_space, config->data->values, found);
656
	if (err)
657
658
659
660
		return err;
	return CCS_SUCCESS;
}

661
662
663
664
665
666
667
668
669
ccs_error_t
ccs_configuration_space_sample(ccs_configuration_space_t  configuration_space,
                               ccs_configuration_t       *configuration_ret) {
	if (!configuration_space || !configuration_space->data)
		return -CCS_INVALID_OBJECT;
	if (!configuration_ret)
		return -CCS_INVALID_VALUE;
	ccs_error_t err;
	ccs_configuration_t config;
Brice Videau's avatar
Brice Videau committed
670
671
672
673
674
	if (!configuration_space->data->graph_ok) {
		err = _generate_constraints(configuration_space);
		if (err)
			return err;
	}
675
	err = ccs_create_configuration(configuration_space, 0, NULL, NULL, &config);
676
677
	if (err)
		return err;
678
679
680
	ccs_bool_t found;
	int counter = 0;
	do {
681
		err = _sample(configuration_space, config, &found);
682
683
684
685
686
687
688
		if (err) {
			ccs_release_object(config);
			return err;
		}
		counter++;
	} while (!found && counter < 100);
	if (!found) {
Brice Videau's avatar
Brice Videau committed
689
		ccs_release_object(config);
690
		return -CCS_SAMPLING_UNSUCCESSFUL;
Brice Videau's avatar
Brice Videau committed
691
	}
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
	*configuration_ret = config;
	return CCS_SUCCESS;
}

ccs_error_t
ccs_configuration_space_samples(ccs_configuration_space_t  configuration_space,
                                size_t                     num_configurations,
                                ccs_configuration_t       *configurations) {
	if (!configuration_space || !configuration_space->data)
		return -CCS_INVALID_OBJECT;
	if (num_configurations && !configurations)
		return -CCS_INVALID_VALUE;
	if (!num_configurations)
		return CCS_SUCCESS;
	ccs_error_t err;
707
708
709
710
711
	if (!configuration_space->data->graph_ok) {
		err = _generate_constraints(configuration_space);
		if (err)
			return err;
	}
712
713
714
715
716
717
718
719
720
721
722
723
724
	size_t     counter = 0;
	size_t     count = 0;
	ccs_bool_t found;
	ccs_configuration_t config = NULL;
	// Naive implementation
	//See below for more efficient ideas...
	for (size_t i = 0; i < num_configurations; i++)
		configurations[i] = NULL;
	while (count < num_configurations && counter < 100 * num_configurations) {
		if (!config) {
			err = ccs_create_configuration(configuration_space, 0, NULL, NULL, &config);
			if (err)
				return err;
725
		}
726
		err = _sample(configuration_space, config, &found);
Brice Videau's avatar
Brice Videau committed
727
		if (err) {
728
			ccs_release_object(config);
Brice Videau's avatar
Brice Videau committed
729
730
			return err;
		}
731
732
733
734
735
		counter++;
		if (found) {
			configurations[count++] = config;
			config = NULL;
		}
Brice Videau's avatar
Brice Videau committed
736
	}
737
738
	if (count < num_configurations)
		return -CCS_SAMPLING_UNSUCCESSFUL;
739
	return CCS_SUCCESS;
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
//	UT_array *array = configuration_space->data->hyperparameters;
//	size_t num_hyper = utarray_len(array);
//	ccs_datum_t *values = (ccs_datum_t *)calloc(1, sizeof(ccs_datum_t)*num_configurations*num_hyper);
//	ccs_datum_t *p_values = values;
//	ccs_rng_t rng = configuration_space->data->rng;
//	_ccs_hyperparameter_wrapper_t *wrapper = NULL;
//	while ( (wrapper = (_ccs_hyperparameter_wrapper_t *)utarray_next(array, wrapper)) ) {
//		err = ccs_hyperparameter_samples(wrapper->hyperparameter,
//		                                 wrapper->distribution->distribution,
//						 rng, num_configurations, p_values);
//		if (unlikely(err)) {
//			free(values);
//			return err;
//		}
//		p_values += num_configurations;
//	}
//	size_t i;
//	for(i = 0; i < num_configurations; i++) {
//		err = ccs_create_configuration(configuration_space, 0, NULL, NULL, configurations + i);
//		if (unlikely(err)) {
//			free(values);
//			for(size_t j = 0; j < i; j++)
//				ccs_release_object(configurations + j);
//			return err;
//		}
//	}
//	for(i = 0; i < num_configurations; i++) {
//		for(size_t j = 0; j < num_hyper; j++)
//			configurations[i]->data->values[j] =
//				values[j*num_configurations + i];
//
//		err = _set_actives(configuration_space, configurations[i]);
//		if (err) {
//			free(values);
//			for(size_t j = 0; j < num_configurations; j++)
//				ccs_release_object(configurations + j);
//			return err;
//		}
//	}
//	free(values);
//	return CCS_SUCCESS;
781
782
}

Brice Videau's avatar
Brice Videau committed
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
static int _size_t_sort(const void *a, const void *b) {
	const size_t sa = *(const size_t *)a;
	const size_t sb = *(const size_t *)b;
	return sa < sb ? -1 : sa > sb ? 1 : 0;
}

static void _uniq_size_t_array(UT_array *array) {
	size_t count = utarray_len(array);
	if (count == 0)
		return;
	utarray_sort(array, &_size_t_sort);
	size_t real_count = 0;
	size_t *p = (size_t *)utarray_front(array);
	size_t *p2 = p;
	real_count++;
	while ( (p = (size_t *)utarray_next(array, p)) ) {
		if (*p != *p2) {
			p2 = (size_t *)utarray_next(array, p2);
			*p2 = *p;
			real_count++;
		}
	}
	utarray_resize(array, real_count);
}

struct _hyper_list_s;
struct _hyper_list_s {
	size_t in_edges;
	size_t index;
	struct _hyper_list_s *next;
	struct _hyper_list_s *prev;
};

#undef  utarray_oom
#define utarray_oom() { \
	free((void *)list); \
	return -CCS_ENOMEM; \
}
static ccs_error_t
_topological_sort(ccs_configuration_space_t configuration_space) {
Brice Videau's avatar
Brice Videau committed
823
	utarray_clear(configuration_space->data->sorted_indexes);
Brice Videau's avatar
Brice Videau committed
824
825
826
	UT_array *array = configuration_space->data->hyperparameters;
	size_t count = utarray_len(array);

Brice Videau's avatar
Brice Videau committed
827
828
	struct _hyper_list_s *list = (struct _hyper_list_s *)calloc(1,
		sizeof(struct _hyper_list_s) * count);
Brice Videau's avatar
Brice Videau committed
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
	if (!list)
		return -CCS_ENOMEM;
	struct _hyper_list_s *queue = NULL;

	_ccs_hyperparameter_wrapper_t *wrapper = NULL;
	size_t index = 0;
	while ( (wrapper = (_ccs_hyperparameter_wrapper_t *)utarray_next(array, wrapper)) ) {
		size_t in_edges = utarray_len(wrapper->parents);
		list[index].in_edges = in_edges;
		list[index].index = index;
		if (in_edges == 0)
			DL_APPEND(queue, list + index);
		index++;
	}
	size_t processed = 0;
	while(queue) {
		struct _hyper_list_s *e = queue;
		DL_DELETE(queue, queue);
		wrapper = (_ccs_hyperparameter_wrapper_t *)
			utarray_eltptr(array, e->index);
		size_t *child = NULL;
		while ( (child = (size_t *)utarray_next(wrapper->children, child)) ) {
			list[*child].in_edges--;
			if (list[*child].in_edges == 0) {
				DL_APPEND(queue, list + *child);
			}
		}
		utarray_push_back(configuration_space->data->sorted_indexes, &(e->index));
		processed++;
	};
	free(list);
	if (processed < count)
		return -CCS_INVALID_GRAPH;
	return CCS_SUCCESS;
}

#undef  utarray_oom
#define utarray_oom() { \
	free((void *)mem); \
	return -CCS_ENOMEM; \
}
static ccs_error_t
_recompute_graph(ccs_configuration_space_t configuration_space) {
	_ccs_hyperparameter_wrapper_t *wrapper = NULL;
	UT_array *array = configuration_space->data->hyperparameters;
	while ( (wrapper = (_ccs_hyperparameter_wrapper_t *)utarray_next(array, wrapper)) ) {
		utarray_clear(wrapper->parents);
		utarray_clear(wrapper->children);
	}
	wrapper = NULL;
	while ( (wrapper = (_ccs_hyperparameter_wrapper_t *)utarray_next(array, wrapper)) ) {
		if (!wrapper->condition)
			continue;
		size_t count;
		ccs_error_t err;
		err = ccs_expression_get_hyperparameters(wrapper->condition, 0, NULL, &count);
		if (err)
			return err;
		if (count == 0)
			continue;
		ccs_hyperparameter_t *parents = NULL;
		size_t               *parents_index = NULL;
		_ccs_hyperparameter_wrapper_t *parent_wrapper = NULL;
		intptr_t mem = (intptr_t)malloc(count *
			(sizeof(ccs_hyperparameter_t) + sizeof(size_t)));
		if (!mem)
			return -CCS_ENOMEM;
		parents = (ccs_hyperparameter_t *)mem;
		parents_index = (size_t *)(mem + count * sizeof(ccs_hyperparameter_t));
		err = ccs_expression_get_hyperparameters(wrapper->condition, count, parents, NULL);
		if (err) {
			free((void *)mem);
			return err;
		}
		err = ccs_configuration_space_get_hyperparameter_indexes(
			configuration_space, count, parents, parents_index);
		if (err) {
			free((void *)mem);
			return err;
		}
		for (size_t i = 0; i < count; i++) {
			utarray_push_back(wrapper->parents, parents_index + i);
			parent_wrapper = (_ccs_hyperparameter_wrapper_t *)utarray_eltptr(array, parents_index[i]);
			utarray_push_back(parent_wrapper->children, &(wrapper->index));
		}
Brice Videau's avatar
Brice Videau committed
914
		free((void *)mem);
Brice Videau's avatar
Brice Videau committed
915
916
917
918
919
920
921
922
923
924
925
926
927
	}
	wrapper = NULL;
	while ( (wrapper = (_ccs_hyperparameter_wrapper_t *)utarray_next(array, wrapper)) ) {
		_uniq_size_t_array(wrapper->parents);
		_uniq_size_t_array(wrapper->children);
        }
	return CCS_SUCCESS;
}
#undef  utarray_oom
#define utarray_oom() { \
	exit(-1); \
}

Brice Videau's avatar
Brice Videau committed
928
929
930
931
932
933
934
935
936
937
938
939
940
static ccs_error_t
_generate_constraints(ccs_configuration_space_t configuration_space) {
	ccs_error_t err;
	err = _recompute_graph(configuration_space);
	if (err)
		return err;
	err = _topological_sort(configuration_space);
	if (err)
		return err;
	configuration_space->data->graph_ok = CCS_TRUE;
	return CCS_SUCCESS;
}

941
942
943
944
945
946
947
948
949
950
951
952
953
ccs_error_t
ccs_configuration_space_set_condition(ccs_configuration_space_t configuration_space,
                                      size_t                    hyperparameter_index,
                                      ccs_expression_t          expression) {
	if (!configuration_space || !configuration_space->data || !expression)
		return -CCS_INVALID_OBJECT;
	_ccs_hyperparameter_wrapper_t *wrapper = (_ccs_hyperparameter_wrapper_t*)
	    utarray_eltptr(configuration_space->data->hyperparameters,
	                   (unsigned int)hyperparameter_index);
	if (!wrapper)
		return -CCS_OUT_OF_BOUNDS;
	if (wrapper->condition)
		return -CCS_INVALID_HYPERPARAMETER;
954
955
	ccs_error_t err;
	err = ccs_retain_object(expression);
956
957
958
	if (err)
		return err;
	wrapper->condition = expression;
Brice Videau's avatar
Brice Videau committed
959
	// Recompute the whole graph for now
Brice Videau's avatar
Brice Videau committed
960
961
	configuration_space->data->graph_ok = CCS_FALSE;
	err = _generate_constraints(configuration_space);
Brice Videau's avatar
Brice Videau committed
962
963
964
965
966
	if (err) {
		ccs_release_object(expression);
		wrapper->condition = NULL;
		return err;
	}
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
1001
1002
1003
1004
1005
1006
1007
1008
1009
1010
1011
1012
1013
	return CCS_SUCCESS;
}

ccs_error_t
ccs_configuration_space_get_condition(ccs_configuration_space_t  configuration_space,
                                      size_t                     hyperparameter_index,
                                      ccs_expression_t          *expression_ret) {
	if (!configuration_space || !configuration_space->data)
		return -CCS_INVALID_OBJECT;
	if (!expression_ret)
		return -CCS_INVALID_VALUE;
	_ccs_hyperparameter_wrapper_t *wrapper = (_ccs_hyperparameter_wrapper_t*)
	    utarray_eltptr(configuration_space->data->hyperparameters,
	                   (unsigned int)hyperparameter_index);
	if (!wrapper)
		return -CCS_OUT_OF_BOUNDS;
	*expression_ret = wrapper->condition;
	return CCS_SUCCESS;
}

ccs_error_t
ccs_configuration_space_get_conditions(ccs_configuration_space_t  configuration_space,
                                       size_t                     num_expressions,
                                       ccs_expression_t          *expressions,
                                       size_t                    *num_expressions_ret) {
	if (!configuration_space || !configuration_space->data)
		return -CCS_INVALID_OBJECT;
	if (num_expressions && !expressions)
		return -CCS_INVALID_VALUE;
	if (!expressions && !num_expressions_ret)
		return -CCS_INVALID_VALUE;
	UT_array *array = configuration_space->data->hyperparameters;
	size_t size = utarray_len(array);
	if (expressions) {
		if (num_expressions < size)
			return -CCS_INVALID_VALUE;
		_ccs_hyperparameter_wrapper_t *wrapper = NULL;
		size_t index = 0;
		while ( (wrapper = (_ccs_hyperparameter_wrapper_t *)utarray_next(array, wrapper)) )
			expressions[index++] = wrapper->condition;
		for (size_t i = size; i < num_expressions; i++)
			expressions[i] = NULL;
	}
	if (num_expressions_ret)
		*num_expressions_ret = size;
	return CCS_SUCCESS;
}
1014

1015
1016
1017
1018
1019
1020
1021
1022
1023
#undef  utarray_oom
#define utarray_oom() { \
	return -CCS_ENOMEM; \
}
ccs_error_t
ccs_configuration_space_add_forbidden_clause(ccs_configuration_space_t configuration_space,
                                             ccs_expression_t          expression) {
	if (!configuration_space || !configuration_space->data)
		return -CCS_INVALID_OBJECT;
1024
	ccs_error_t err;
1025
1026
	err = ccs_expression_check_context(expression,
	                                   (ccs_context_t)configuration_space);
1027
1028
1029
	if (err)
		return err;
	err = ccs_retain_object(expression);
1030
1031
1032
1033
1034
1035
1036
1037
1038
1039
1040
1041
1042
1043
1044
	if (err)
		return err;
	utarray_push_back(configuration_space->data->forbidden_clauses, &expression);
	return CCS_SUCCESS;
}

ccs_error_t
ccs_configuration_space_add_forbidden_clauses(ccs_configuration_space_t  configuration_space,
                                              size_t                     num_expressions,
                                              ccs_expression_t          *expressions) {
	if (!configuration_space || !configuration_space->data)
		return -CCS_INVALID_OBJECT;
	if (num_expressions && !expressions)
		return -CCS_INVALID_VALUE;
	for (size_t i = 0; i < num_expressions; i++) {
1045
		ccs_error_t err;
1046
1047
		err = ccs_expression_check_context(expressions[i],
		                                   (ccs_context_t)configuration_space);
1048
1049
1050
		if (err)
			return err;
		err = ccs_retain_object(expressions[i]);
1051
1052
1053
1054
1055
1056
1057
1058
1059
1060
1061
1062
1063
1064
1065
1066
1067
1068
1069
1070
1071
1072
1073
1074
1075
1076
1077
1078
1079
1080
1081
1082
1083
1084
1085
1086
1087
1088
1089
1090
1091
1092
1093
1094
1095
1096
1097
1098
1099
1100
1101
1102
1103
		if (err)
			return err;
		utarray_push_back(configuration_space->data->forbidden_clauses, expressions + i);
	}
	return CCS_SUCCESS;
}
#undef  utarray_oom
#define utarray_oom() exit(-1)

ccs_error_t
ccs_configuration_space_get_forbidden_clause(ccs_configuration_space_t  configuration_space,
                                             size_t                     index,
                                             ccs_expression_t          *expression_ret) {
	if (!configuration_space || !configuration_space->data)
		return -CCS_INVALID_OBJECT;
	if (!expression_ret)
		return -CCS_INVALID_VALUE;
	ccs_expression_t *p_expr = (ccs_expression_t*)
	    utarray_eltptr(configuration_space->data->forbidden_clauses,
	                   (unsigned int)index);
	if (!p_expr)
		return -CCS_OUT_OF_BOUNDS;
	*expression_ret = *p_expr;
	return CCS_SUCCESS;
}

ccs_error_t
ccs_configuration_space_get_forbidden_clauses(ccs_configuration_space_t  configuration_space,
                                              size_t                     num_expressions,
                                              ccs_expression_t          *expressions,
                                              size_t                    *num_expressions_ret) {
	if (!configuration_space || !configuration_space->data)
		return -CCS_INVALID_OBJECT;
	if (num_expressions && !expressions)
		return -CCS_INVALID_VALUE;
	if (!expressions && !num_expressions_ret)
		return -CCS_INVALID_VALUE;
	UT_array *array = configuration_space->data->forbidden_clauses;
	size_t size = utarray_len(array);
	if (expressions) {
		if (num_expressions < size)
			return -CCS_INVALID_VALUE;
		ccs_expression_t *p_expr = NULL;
		size_t index = 0;
		while ( (p_expr = (ccs_expression_t *)utarray_next(array, p_expr)) )
			expressions[index++] = *p_expr;
		for (size_t i = size; i < num_expressions; i++)
			expressions[i] = NULL;
	}
	if (num_expressions_ret)
		*num_expressions_ret = size;
	return CCS_SUCCESS;
}