Commit f2d91831 authored by Brice Videau's avatar Brice Videau
Browse files

Added a check_context to expression.

parent ebcc01e0
......@@ -94,6 +94,10 @@ ccs_expression_get_hyperparameters(ccs_expression_t expression,
size_t num_hyperparameters,
ccs_hyperparameter_t *hyperparameters,
size_t *num_hyperparameters_ret);
extern ccs_error_t
ccs_expression_check_context(ccs_expression_t expression,
ccs_configuration_space_t context);
#ifdef __cplusplus
}
#endif
......
......@@ -1177,3 +1177,39 @@ ccs_expression_get_hyperparameters(ccs_expression_t expression,
return CCS_SUCCESS;
}
ccs_error_t
ccs_expression_check_context(ccs_expression_t expression,
ccs_configuration_space_t context) {
ccs_error_t err;
UT_array *array;
utarray_new(array, &_hyperparameter_icd);
err = _get_hyperparameters(expression, array);
utarray_sort(array, &hyper_sort);
if (err) {
utarray_free(array);
return err;
}
if (utarray_len(array) > 0) {
if (!context) {
utarray_free(array);
return -CCS_INVALID_VALUE;
}
ccs_hyperparameter_t previous = NULL;
ccs_hyperparameter_t *p_h = NULL;
while ( (p_h = (ccs_hyperparameter_t *)utarray_next(array, p_h)) ) {
if (*p_h != previous) {
size_t index;
err = ccs_configuration_space_get_hyperparameter_index(
context, *p_h, &index);
if (err) {
utarray_free(array);
return err;
}
previous = *p_h;
}
}
}
utarray_free(array);
return CCS_SUCCESS;
}
......@@ -787,6 +787,8 @@ void test_get_hyperparameters() {
assert( hyperparameters[0] == hyperparameter2 );
assert( hyperparameters[1] == NULL );
assert( hyperparameters[2] == NULL );
err = ccs_expression_check_context(expression2, NULL);
assert( err == -CCS_INVALID_VALUE );
err = ccs_release_object(hyperparameter1);
assert( err == CCS_SUCCESS );
......@@ -798,6 +800,58 @@ void test_get_hyperparameters() {
assert( err == CCS_SUCCESS );
}
void test_check_context() {
ccs_expression_t expression1, expression2;
ccs_hyperparameter_t hyperparameter1, hyperparameter2, hyperparameter3;
ccs_configuration_space_t space;
ccs_error_t err;
hyperparameter1 = create_dummy_categorical("param1");
hyperparameter2 = create_dummy_numerical("param2");
hyperparameter3 = create_dummy_ordinal("param3");
err = ccs_create_configuration_space("space", NULL, &space);
assert( err == CCS_SUCCESS );
err = ccs_configuration_space_add_hyperparameter(space, hyperparameter1, NULL);
assert( err == CCS_SUCCESS );
err = ccs_configuration_space_add_hyperparameter(space, hyperparameter2, NULL);
assert( err == CCS_SUCCESS );
err = ccs_create_binary_expression(CCS_ADD, ccs_float(3.0), ccs_object(hyperparameter2), &expression1);
assert( err == CCS_SUCCESS );
err = ccs_create_binary_expression(CCS_EQUAL,
ccs_object(hyperparameter1), ccs_object(expression1), &expression2);
assert( err == CCS_SUCCESS );
err = ccs_expression_check_context(expression2, NULL);
assert( err == -CCS_INVALID_VALUE );
err = ccs_expression_check_context(expression2, space);
assert( err == CCS_SUCCESS );
err = ccs_release_object(expression2);
assert( err == CCS_SUCCESS );
err = ccs_create_binary_expression(CCS_EQUAL,
ccs_object(hyperparameter3), ccs_object(expression1), &expression2);
assert( err == CCS_SUCCESS );
err = ccs_expression_check_context(expression2, space);
assert( err == -CCS_INVALID_HYPERPARAMETER );
err = ccs_release_object(hyperparameter1);
assert( err == CCS_SUCCESS );
err = ccs_release_object(hyperparameter2);
assert( err == CCS_SUCCESS );
err = ccs_release_object(hyperparameter3);
assert( err == CCS_SUCCESS );
err = ccs_release_object(expression1);
assert( err == CCS_SUCCESS );
err = ccs_release_object(expression2);
assert( err == CCS_SUCCESS );
err = ccs_release_object(space);
assert( err == CCS_SUCCESS );
}
int main(int argc, char *argv[]) {
ccs_init();
test_equal_literal();
......@@ -821,4 +875,5 @@ int main(int argc, char *argv[]) {
test_compound();
test_in();
test_get_hyperparameters();
test_check_context();
}
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