Commit 12a06682 authored by Brice Videau's avatar Brice Videau
Browse files

Inactive is now a ccs_datum type.

parent 12108eb8
......@@ -44,6 +44,7 @@ enum ccs_error_e {
CCS_INVALID_BOUNDS,
CCS_OUT_OF_BOUNDS,
CCS_SAMPLING_UNSUCCESSFUL,
CCS_INACTIVE_HYPERPARAMETER,
CCS_ENOMEM,
CCS_UNSUPPORTED_OPERATION,
CCS_ERROR_MAX,
......@@ -67,6 +68,7 @@ typedef enum ccs_object_type_e ccs_object_type_t;
enum ccs_data_type_e {
CCS_NONE,
CCS_INACTIVE,
CCS_INTEGER,
CCS_FLOAT,
CCS_BOOLEAN,
......@@ -146,9 +148,51 @@ struct ccs_datum_s {
typedef struct ccs_datum_s ccs_datum_t;
static inline ccs_datum_t
ccs_bool(ccs_bool_t v) {
ccs_datum_t d;
d.type = CCS_BOOLEAN;
d.value.i = v;
return d;
}
static inline ccs_datum_t
ccs_float(ccs_float_t v) {
ccs_datum_t d;
d.type = CCS_FLOAT;
d.value.f = v;
return d;
}
static inline ccs_datum_t
ccs_int(ccs_int_t v) {
ccs_datum_t d;
d.type = CCS_INTEGER;
d.value.i = v;
return d;
}
static inline ccs_datum_t
ccs_object(ccs_object_t v) {
ccs_datum_t d;
d.type = CCS_OBJECT;
d.value.o = v;
return d;
}
static inline ccs_datum_t
ccs_string(const char *v) {
ccs_datum_t d;
d.type = CCS_STRING;
d.value.s = v;
return d;
}
extern const ccs_datum_t ccs_none;
extern const ccs_datum_t ccs_inactive;
#define CCS_NONE_VAL {{0}, CCS_NONE}
#define CCS_INACTIVE_VAL {{0}, CCS_INACTIVE}
extern ccs_error_t
ccs_init();
......
......@@ -12,7 +12,6 @@ extern ccs_error_t
ccs_create_configuration(ccs_configuration_space_t configuration_space,
size_t num_values,
ccs_datum_t *values,
ccs_bool_t *actives,
void *user_data,
ccs_configuration_t *configuration_ret);
......@@ -46,22 +45,6 @@ ccs_configuration_get_value_by_name(ccs_configuration_t configuration,
const char *name,
ccs_datum_t *value_ret);
extern ccs_error_t
ccs_configuration_get_active(ccs_configuration_t configuration,
size_t index,
ccs_bool_t *active_ret);
extern ccs_error_t
ccs_configuration_set_active(ccs_configuration_t configuration,
size_t index,
ccs_bool_t active);
extern ccs_error_t
ccs_configuration_get_actives(ccs_configuration_t configuration,
size_t num_actives,
ccs_bool_t *actives,
size_t *num_actives_ret);
extern ccs_error_t
ccs_configuration_check(ccs_configuration_t configuration);
......
......@@ -126,8 +126,7 @@ ccs_configuration_space_check_configuration(ccs_configuration_space_t configurat
extern ccs_error_t
ccs_configuration_space_check_configuration_values(ccs_configuration_space_t configuration_space,
size_t num_values,
ccs_datum_t *values,
ccs_bool_t *actives);
ccs_datum_t *values);
extern ccs_error_t
ccs_configuration_space_get_default_configuration(ccs_configuration_space_t configuration_space,
......
......@@ -3,6 +3,7 @@
#include <gsl/gsl_rng.h>
const ccs_datum_t ccs_none = CCS_NONE_VAL;
const ccs_datum_t ccs_inactive = CCS_INACTIVE_VAL;
ccs_error_t
ccs_init() {
......
......@@ -21,7 +21,6 @@ ccs_error_t
ccs_create_configuration(ccs_configuration_space_t configuration_space,
size_t num_values,
ccs_datum_t *values,
ccs_bool_t *actives,
void *user_data,
ccs_configuration_t *configuration_ret) {
if (!configuration_ret)
......@@ -37,7 +36,7 @@ ccs_create_configuration(ccs_configuration_space_t configuration_space,
return err;
if (values && num != num_values)
return -CCS_INVALID_VALUE;
uintptr_t mem = (uintptr_t)calloc(1, sizeof(struct _ccs_configuration_s) + sizeof(struct _ccs_configuration_data_s) + num * sizeof(ccs_datum_t) + num * sizeof(ccs_bool_t));
uintptr_t mem = (uintptr_t)calloc(1, sizeof(struct _ccs_configuration_s) + sizeof(struct _ccs_configuration_data_s) + num * sizeof(ccs_datum_t));
if (!mem)
return CCS_ENOMEM;
err = ccs_retain_object(configuration_space);
......@@ -52,13 +51,8 @@ ccs_create_configuration(ccs_configuration_space_t configuration_space,
config->data->num_values = num;
config->data->configuration_space = configuration_space;
config->data->values = (ccs_datum_t *)(mem + sizeof(struct _ccs_configuration_s) + sizeof(struct _ccs_configuration_data_s));
config->data->actives = (ccs_bool_t *)(config->data->values+num);
if (values)
memcpy(config->data->values, values, num*sizeof(ccs_datum_t));
if (actives)
memcpy(config->data->actives, actives, num*sizeof(ccs_bool_t));
else for (size_t i = 0; i < num; i++)
config->data->actives[i] = CCS_TRUE;
*configuration_ret = config;
return CCS_SUCCESS;
}
......@@ -157,58 +151,6 @@ ccs_configuration_get_value_by_name(ccs_configuration_t configuration,
return CCS_SUCCESS;
}
ccs_error_t
ccs_configuration_get_active(ccs_configuration_t configuration,
size_t index,
ccs_bool_t *active_ret) {
if (!configuration || !configuration->data)
return -CCS_INVALID_OBJECT;
if (!active_ret)
return -CCS_INVALID_VALUE;
if (index >= configuration->data->num_values)
return -CCS_OUT_OF_BOUNDS;
*active_ret = configuration->data->actives[index];
return CCS_SUCCESS;
}
ccs_error_t
ccs_configuration_set_active(ccs_configuration_t configuration,
size_t index,
ccs_bool_t active) {
if (!configuration || !configuration->data)
return -CCS_INVALID_OBJECT;
if (index >= configuration->data->num_values)
return -CCS_OUT_OF_BOUNDS;
configuration->data->actives[index] = active;
return CCS_SUCCESS;
}
ccs_error_t
ccs_configuration_get_actives(ccs_configuration_t configuration,
size_t num_actives,
ccs_bool_t *actives,
size_t *num_actives_ret) {
if (!configuration || !configuration->data)
return -CCS_INVALID_OBJECT;
if (!num_actives_ret && !actives)
return -CCS_INVALID_VALUE;
if (num_actives && !actives)
return -CCS_INVALID_VALUE;
if (!num_actives && actives)
return -CCS_INVALID_VALUE;
size_t num = configuration->data->num_values;
if (actives) {
if (num_actives < num)
return -CCS_INVALID_VALUE;
memcpy(actives, configuration->data->actives, num*sizeof(ccs_bool_t));
for (size_t i = num; i < num_actives; i++)
actives[i] = CCS_FALSE;
}
if (num_actives_ret)
*num_actives_ret = num;
return CCS_SUCCESS;
}
ccs_error_t
ccs_configuration_check(ccs_configuration_t configuration) {
if (!configuration || !configuration->data)
......
......@@ -19,7 +19,6 @@ struct _ccs_configuration_data_s {
ccs_configuration_space_t configuration_space;
size_t num_values;
ccs_datum_t *values;
ccs_bool_t *actives;
};
#endif //_CONFIGURATION_INTERNAL_H
......@@ -445,7 +445,7 @@ ccs_configuration_space_get_default_configuration(ccs_configuration_space_t con
return -CCS_INVALID_VALUE;
ccs_error_t err;
ccs_configuration_t config;
err = ccs_create_configuration(configuration_space, 0, NULL, NULL, NULL, &config);
err = ccs_create_configuration(configuration_space, 0, NULL, NULL, &config);
if (err)
return err;
UT_array *array = configuration_space->data->hyperparameters;
......@@ -469,25 +469,22 @@ _set_actives(ccs_configuration_space_t configuration_space,
size_t *p_index = NULL;
UT_array *indexes = configuration_space->data->sorted_indexes;
UT_array *array = configuration_space->data->hyperparameters;
ccs_bool_t *actives = configuration->data->actives;
ccs_datum_t *values = configuration->data->values;
while ( (p_index = (size_t *)utarray_next(indexes, p_index)) ) {
_ccs_hyperparameter_wrapper_t *wrapper = NULL;
wrapper = (_ccs_hyperparameter_wrapper_t *)utarray_eltptr(array, *p_index);
if (!wrapper->condition) {
actives[*p_index] = CCS_TRUE;
continue;
}
UT_array *parents = wrapper->parents;
size_t *p_parent = NULL;
while ( (p_parent = (size_t*)utarray_next(parents, p_parent)) ) {
if (!actives[*p_parent]) {
actives[*p_index] = CCS_FALSE;
values[*p_index] = ccs_none;
if (values[*p_parent].type == CCS_INACTIVE) {
values[*p_index] = ccs_inactive;
break;
}
}
if (!actives[*p_index])
if (values[*p_index].type == CCS_INACTIVE)
continue;
ccs_datum_t result;
ccs_error_t err;
......@@ -495,11 +492,8 @@ _set_actives(ccs_configuration_space_t configuration_space,
values, &result);
if (err)
return err;
if (!(result.type == CCS_BOOLEAN && result.value.i == CCS_TRUE)) {
actives[*p_index] = CCS_FALSE;
values[*p_index] = ccs_none;
} else
actives[*p_index] = CCS_TRUE;
if (!(result.type == CCS_BOOLEAN && result.value.i == CCS_TRUE))
values[*p_index] = ccs_inactive;
}
return CCS_SUCCESS;
}
......@@ -507,8 +501,7 @@ _set_actives(ccs_configuration_space_t configuration_space,
static inline ccs_error_t
_check_configuration(ccs_configuration_space_t configuration_space,
size_t num_values,
ccs_datum_t *values,
ccs_bool_t *actives) {
ccs_datum_t *values) {
UT_array *indexes = configuration_space->data->sorted_indexes;
UT_array *array = configuration_space->data->hyperparameters;
if (num_values != utarray_len(array))
......@@ -523,7 +516,7 @@ _check_configuration(ccs_configuration_space_t configuration_space,
UT_array *parents = wrapper->parents;
size_t *p_parent = NULL;
while ( (p_parent = (size_t*)utarray_next(parents, p_parent)) ) {
if (!actives[*p_parent]) {
if (values[*p_parent].type == CCS_INACTIVE) {
active = CCS_FALSE;
break;
}
......@@ -541,7 +534,7 @@ _check_configuration(ccs_configuration_space_t configuration_space,
}
}
}
if (active != actives[*p_index])
if (active != (values[*p_index].type == CCS_INACTIVE ? CCS_FALSE : CCS_TRUE))
return -CCS_INVALID_CONFIGURATION;
if (active) {
ccs_bool_t res;
......@@ -573,18 +566,16 @@ ccs_configuration_space_check_configuration(ccs_configuration_space_t configurat
}
return _check_configuration(configuration_space,
configuration->data->num_values,
configuration->data->values,
configuration->data->actives);
configuration->data->values);
}
ccs_error_t
ccs_configuration_space_check_configuration_values(ccs_configuration_space_t configuration_space,
size_t num_values,
ccs_datum_t *values,
ccs_bool_t *actives) {
ccs_datum_t *values) {
if (!configuration_space || !configuration_space->data)
return -CCS_INVALID_OBJECT;
if (!values || !actives)
if (!values)
return -CCS_INVALID_VALUE;
if (!configuration_space->data->graph_ok) {
ccs_error_t err;
......@@ -592,7 +583,7 @@ ccs_configuration_space_check_configuration_values(ccs_configuration_space_t co
if (err)
return err;
}
return _check_configuration(configuration_space, num_values, values, actives);
return _check_configuration(configuration_space, num_values, values);
}
ccs_error_t
......@@ -609,7 +600,7 @@ ccs_configuration_space_sample(ccs_configuration_space_t configuration_space,
if (err)
return err;
}
err = ccs_create_configuration(configuration_space, 0, NULL, NULL, NULL, &config);
err = ccs_create_configuration(configuration_space, 0, NULL, NULL, &config);
if (err)
return err;
ccs_rng_t rng = configuration_space->data->rng;
......@@ -668,7 +659,7 @@ ccs_configuration_space_samples(ccs_configuration_space_t configuration_space,
}
size_t i;
for(i = 0; i < num_configurations; i++) {
err = ccs_create_configuration(configuration_space, 0, NULL, NULL, NULL, 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++)
......
......@@ -88,6 +88,8 @@ _ccs_expr_datum_eval(ccs_datum_t *d,
if (err)
return err;
*result = values[index];
if (result->type == CCS_INACTIVE)
return -CCS_INACTIVE_HYPERPARAMETER;
if (ht) {
err = ccs_hyperparameter_get_type(
(ccs_hyperparameter_t)(d->value.o), ht);
......
......@@ -4,46 +4,6 @@
#include <string.h>
#include <math.h>
static inline ccs_datum_t
ccs_bool(ccs_bool_t v) {
ccs_datum_t d;
d.type = CCS_BOOLEAN;
d.value.i = v;
return d;
}
static inline ccs_datum_t
ccs_float(ccs_float_t v) {
ccs_datum_t d;
d.type = CCS_FLOAT;
d.value.f = v;
return d;
}
static inline ccs_datum_t
ccs_int(ccs_int_t v) {
ccs_datum_t d;
d.type = CCS_INTEGER;
d.value.i = v;
return d;
}
static inline ccs_datum_t
ccs_object(ccs_object_t v) {
ccs_datum_t d;
d.type = CCS_OBJECT;
d.value.o = v;
return d;
}
static inline ccs_datum_t
ccs_string(const char *v) {
ccs_datum_t d;
d.type = CCS_STRING;
d.value.s = v;
return d;
}
ccs_hyperparameter_t create_numerical(const char * name) {
ccs_hyperparameter_t hyperparameter;
ccs_error_t err;
......@@ -57,7 +17,7 @@ ccs_hyperparameter_t create_numerical(const char * name) {
void
test_simple() {
ccs_hyperparameter_t hyperparameter1, hyperparameter2;
ccs_hyperparameter_t hyperparameter1, hyperparameter2;
ccs_configuration_space_t space;
ccs_expression_t expression;
ccs_configuration_t configuration;
......@@ -90,10 +50,10 @@ test_simple() {
assert( values[0].type == CCS_FLOAT );
f = values[0].value.f;
assert( f >= -1.0 && f < 1.0 );
if ( f < 0.0 )
if (f < 0.0)
assert( values[1].type == CCS_FLOAT );
else
assert( values[1].type == CCS_NONE );
assert( values[1].type == CCS_INACTIVE );
err = ccs_configuration_space_check_configuration(space, configuration);
assert( err == CCS_SUCCESS );
err = ccs_release_object(configuration);
......@@ -110,10 +70,10 @@ test_simple() {
assert( values[0].type == CCS_FLOAT );
f = values[0].value.f;
assert( f >= -1.0 && f < 1.0 );
if ( f < 0.0 )
if (f < 0.0)
assert( values[1].type == CCS_FLOAT );
else
assert( values[1].type == CCS_NONE );
assert( values[1].type == CCS_INACTIVE );
err = ccs_configuration_space_check_configuration(space, configurations[i]);
assert( err == CCS_SUCCESS );
err = ccs_release_object(configurations[i]);
......
......@@ -4,46 +4,6 @@
#include <string.h>
#include <math.h>
static inline ccs_datum_t
ccs_bool(ccs_bool_t v) {
ccs_datum_t d;
d.type = CCS_BOOLEAN;
d.value.i = v;
return d;
}
static inline ccs_datum_t
ccs_float(ccs_float_t v) {
ccs_datum_t d;
d.type = CCS_FLOAT;
d.value.f = v;
return d;
}
static inline ccs_datum_t
ccs_int(ccs_int_t v) {
ccs_datum_t d;
d.type = CCS_INTEGER;
d.value.i = v;
return d;
}
static inline ccs_datum_t
ccs_object(ccs_object_t v) {
ccs_datum_t d;
d.type = CCS_OBJECT;
d.value.o = v;
return d;
}
static inline ccs_datum_t
ccs_string(const char *v) {
ccs_datum_t d;
d.type = CCS_STRING;
d.value.s = v;
return d;
}
double d = -2.0;
ccs_hyperparameter_t create_dummy_numerical(const char * name) {
......
......@@ -74,7 +74,7 @@ static void test_create_normal_distribution_errors() {
// check wrong data_type
err = ccs_create_normal_distribution(
(ccs_numeric_type_t)3,
(ccs_numeric_type_t)CCS_STRING,
1.0,
2.0,
CCS_LINEAR,
......
Markdown is supported
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment