Commit 11677e38 authored by Brice Videau's avatar Brice Videau
Browse files

Add abstract context class, configuration_space are context.

parent 67b67245
......@@ -6,6 +6,7 @@ include_ccs_HEADERS = \
ccs/rng.h \
ccs/distribution.h \
ccs/interval.h \
ccs/context.h \
ccs/configuration_space.h \
ccs/hyperparameter.h \
ccs/expression.h \
......
......@@ -10,6 +10,7 @@
#include "ccs/distribution.h"
#include "ccs/hyperparameter.h"
#include "ccs/expression.h"
#include "ccs/context.h"
#include "ccs/configuration_space.h"
#include "ccs/configuration.h"
......
......@@ -25,6 +25,7 @@ typedef struct _ccs_rng_s *ccs_rng_t;
typedef struct _ccs_distribution_s *ccs_distribution_t;
typedef struct _ccs_hyperparameter_s *ccs_hyperparameter_t;
typedef struct _ccs_expression_s *ccs_expression_t;
typedef struct _ccs_context_s *ccs_context_t;
typedef struct _ccs_configuration_space_s *ccs_configuration_space_t;
typedef struct _ccs_configuration_s *ccs_configuration_t;
......
#ifndef _CCS_CONTEXT_H
#define _CCS_CONTEXT_H
#ifdef __cplusplus
extern "C" {
#endif
extern ccs_error_t
ccs_context_get_hyperparameter_index(
ccs_context_t context,
ccs_hyperparameter_t hyperparameter,
size_t *index_ret);
#ifdef __cplusplus
}
#endif
#endif //_CCS_CONTEXT_H
......@@ -83,17 +83,17 @@ ccs_expression_get_nodes(ccs_expression_t expression,
size_t *num_nodes_ret);
extern ccs_error_t
ccs_expression_eval(ccs_expression_t expression,
ccs_configuration_space_t context,
ccs_datum_t *values,
ccs_datum_t *result);
ccs_expression_eval(ccs_expression_t expression,
ccs_context_t context,
ccs_datum_t *values,
ccs_datum_t *result);
extern ccs_error_t
ccs_expression_list_eval_node(ccs_expression_t expression,
ccs_configuration_space_t context,
ccs_datum_t *values,
size_t index,
ccs_datum_t *result);
ccs_expression_list_eval_node(ccs_expression_t expression,
ccs_context_t context,
ccs_datum_t *values,
size_t index,
ccs_datum_t *result);
//uniq and sorted list of hyperparameters handle
extern ccs_error_t
......@@ -103,8 +103,8 @@ ccs_expression_get_hyperparameters(ccs_expression_t expression,
size_t *num_hyperparameters_ret);
extern ccs_error_t
ccs_expression_check_context(ccs_expression_t expression,
ccs_configuration_space_t context);
ccs_expression_check_context(ccs_expression_t expression,
ccs_context_t context);
#ifdef __cplusplus
}
#endif
......
......@@ -23,6 +23,8 @@ libcconfigspace_la_SOURCES = \
hyperparameter_categorical.c \
hyperparameter_ordinal.c \
utarray.h \
context.c \
context_internal.h \
configuration_space.c \
configuration_space_internal.h \
configuration.c \
......
......@@ -36,7 +36,9 @@ 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));
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);
......
......@@ -43,8 +43,24 @@ _ccs_configuration_space_del(ccs_object_t object) {
return CCS_SUCCESS;
}
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;
}
static _ccs_configuration_space_ops_t _configuration_space_ops =
{ {&_ccs_configuration_space_del} };
{ { {&_ccs_configuration_space_del},
&_ccs_configuration_space_get_hyperparameter_index} };
static const UT_icd _hyperparameter_wrapper_icd = {
sizeof(_ccs_hyperparameter_wrapper_t),
......@@ -488,7 +504,8 @@ _set_actives(ccs_configuration_space_t configuration_space,
continue;
ccs_datum_t result;
ccs_error_t err;
err = ccs_expression_eval(wrapper->condition, configuration_space,
err = ccs_expression_eval(wrapper->condition,
(ccs_context_t)configuration_space,
values, &result);
if (err)
return err;
......@@ -509,7 +526,8 @@ _test_forbidden(ccs_configuration_space_t configuration_space,
while ( (p_expression = (ccs_expression_t *)
utarray_next(array, p_expression)) ) {
ccs_datum_t result;
err = ccs_expression_eval(*p_expression, configuration_space,
err = ccs_expression_eval(*p_expression,
(ccs_context_t)configuration_space,
values, &result);
if (err == -CCS_INACTIVE_HYPERPARAMETER)
continue;
......@@ -548,9 +566,10 @@ _check_configuration(ccs_configuration_space_t configuration_space,
if (active) {
ccs_datum_t result;
ccs_error_t err;
err = ccs_expression_eval(wrapper->condition,
configuration_space,
values, &result);
err = ccs_expression_eval(
wrapper->condition,
(ccs_context_t)configuration_space,
values, &result);
if (err)
return err;
if (!(result.type == CCS_BOOLEAN && result.value.i == CCS_TRUE)) {
......@@ -1007,7 +1026,8 @@ ccs_configuration_space_add_forbidden_clause(ccs_configuration_space_t configura
if (!configuration_space || !configuration_space->data)
return -CCS_INVALID_OBJECT;
ccs_error_t err;
err = ccs_expression_check_context(expression, configuration_space);
err = ccs_expression_check_context(expression,
(ccs_context_t)configuration_space);
if (err)
return err;
err = ccs_retain_object(expression);
......@@ -1027,7 +1047,8 @@ ccs_configuration_space_add_forbidden_clauses(ccs_configuration_space_t configu
return -CCS_INVALID_VALUE;
for (size_t i = 0; i < num_expressions; i++) {
ccs_error_t err;
err = ccs_expression_check_context(expressions[i], configuration_space);
err = ccs_expression_check_context(expressions[i],
(ccs_context_t)configuration_space);
if (err)
return err;
err = ccs_retain_object(expressions[i]);
......
......@@ -4,6 +4,7 @@
#define HASH_NONFATAL_OOM 1
#include "uthash.h"
#include "utlist.h"
#include "context_internal.h"
struct _ccs_distribution_wrapper_s;
typedef struct _ccs_distribution_wrapper_s _ccs_distribution_wrapper_t;
......@@ -34,7 +35,7 @@ struct _ccs_configuration_space_data_s;
typedef struct _ccs_configuration_space_data_s _ccs_configuration_space_data_t;
struct _ccs_configuration_space_ops_s {
_ccs_object_ops_t obj_ops;
_ccs_context_ops_t ops;
};
typedef struct _ccs_configuration_space_ops_s _ccs_configuration_space_ops_t;
......
#include "cconfigspace_internal.h"
#include "context_internal.h"
static inline _ccs_context_ops_t *
ccs_context_get_ops(ccs_context_t context) {
return (_ccs_context_ops_t *)context->obj.ops;
}
ccs_error_t
ccs_context_get_hyperparameter_index(
ccs_context_t context,
ccs_hyperparameter_t hyperparameter,
size_t *index_ret) {
if (!context || !context->data)
return -CCS_INVALID_OBJECT;
if (!hyperparameter)
return -CCS_INVALID_HYPERPARAMETER;
if (!index_ret)
return -CCS_INVALID_VALUE;
_ccs_context_ops_t *ops = ccs_context_get_ops(context);
return ops->get_hyperparameter_index(context->data, hyperparameter, index_ret);
}
#ifndef _CONTEXT_INTERNAL_H
#define _CONTEXT_INTERNAL_H
typedef struct _ccs_context_data_s _ccs_context_data_t;
struct _ccs_context_ops_s {
_ccs_object_ops_t obj_ops;
ccs_error_t (*get_hyperparameter_index)(
_ccs_context_data_t *data,
ccs_hyperparameter_t hyperparameter,
size_t *index_ret);
};
typedef struct _ccs_context_ops_s _ccs_context_ops_t;
struct _ccs_context_s {
_ccs_object_internal_t obj;
_ccs_context_data_t *data;
};
#endif //_CONTEXT_INTERNAL_H
......@@ -58,7 +58,7 @@ _ccs_expression_del(ccs_object_t o) {
static inline ccs_error_t
_ccs_expr_datum_eval(ccs_datum_t *d,
ccs_configuration_space_t context,
ccs_context_t context,
ccs_datum_t *values,
ccs_datum_t *result,
ccs_hyperparameter_type_t *ht) {
......@@ -83,7 +83,7 @@ _ccs_expr_datum_eval(ccs_datum_t *d,
context, values, result);
break;
case CCS_HYPERPARAMETER:
err = ccs_configuration_space_get_hyperparameter_index(
err = ccs_context_get_hyperparameter_index(
context, (ccs_hyperparameter_t)(d->value.o), &index);
if (err)
return err;
......@@ -126,9 +126,9 @@ _ccs_expr_datum_eval(ccs_datum_t *d,
static ccs_error_t
_ccs_expr_or_eval(_ccs_expression_data_t *data,
ccs_configuration_space_t context,
ccs_datum_t *values,
ccs_datum_t *result) {
ccs_context_t context,
ccs_datum_t *values,
ccs_datum_t *result) {
ccs_datum_t left;
ccs_datum_t right;
eval_left_right(data, context, values, left, right, NULL, NULL);
......@@ -146,9 +146,9 @@ static _ccs_expression_ops_t _ccs_expr_or_ops = {
static ccs_error_t
_ccs_expr_and_eval(_ccs_expression_data_t *data,
ccs_configuration_space_t context,
ccs_datum_t *values,
ccs_datum_t *result) {
ccs_context_t context,
ccs_datum_t *values,
ccs_datum_t *result) {
ccs_datum_t left;
ccs_datum_t right;
eval_left_right(data, context, values, left, right, NULL, NULL);
......@@ -255,9 +255,9 @@ _ccs_datum_cmp_generic(ccs_datum_t *a, ccs_datum_t *b, ccs_int_t *cmp) {
static ccs_error_t
_ccs_expr_equal_eval(_ccs_expression_data_t *data,
ccs_configuration_space_t context,
ccs_datum_t *values,
ccs_datum_t *result) {
ccs_context_t context,
ccs_datum_t *values,
ccs_datum_t *result) {
ccs_datum_t left;
ccs_datum_t right;
ccs_hyperparameter_type_t htl = CCS_HYPERPARAMETER_TYPE_MAX;
......@@ -285,10 +285,10 @@ static _ccs_expression_ops_t _ccs_expr_equal_ops = {
};
static ccs_error_t
_ccs_expr_not_equal_eval(_ccs_expression_data_t *data,
ccs_configuration_space_t context,
ccs_datum_t *values,
ccs_datum_t *result) {
_ccs_expr_not_equal_eval(_ccs_expression_data_t *data,
ccs_context_t context,
ccs_datum_t *values,
ccs_datum_t *result) {
ccs_datum_t left;
ccs_datum_t right;
ccs_hyperparameter_type_t htl = CCS_HYPERPARAMETER_TYPE_MAX;
......@@ -317,9 +317,9 @@ static _ccs_expression_ops_t _ccs_expr_not_equal_ops = {
static ccs_error_t
_ccs_expr_less_eval(_ccs_expression_data_t *data,
ccs_configuration_space_t context,
ccs_datum_t *values,
ccs_datum_t *result) {
ccs_context_t context,
ccs_datum_t *values,
ccs_datum_t *result) {
ccs_datum_t left;
ccs_datum_t right;
ccs_hyperparameter_type_t htl = CCS_HYPERPARAMETER_TYPE_MAX;
......@@ -369,9 +369,9 @@ static _ccs_expression_ops_t _ccs_expr_less_ops = {
static ccs_error_t
_ccs_expr_greater_eval(_ccs_expression_data_t *data,
ccs_configuration_space_t context,
ccs_datum_t *values,
ccs_datum_t *result) {
ccs_context_t context,
ccs_datum_t *values,
ccs_datum_t *result) {
ccs_datum_t left;
ccs_datum_t right;
ccs_hyperparameter_type_t htl = CCS_HYPERPARAMETER_TYPE_MAX;
......@@ -421,9 +421,9 @@ static _ccs_expression_ops_t _ccs_expr_greater_ops = {
static ccs_error_t
_ccs_expr_less_or_equal_eval(_ccs_expression_data_t *data,
ccs_configuration_space_t context,
ccs_datum_t *values,
ccs_datum_t *result) {
ccs_context_t context,
ccs_datum_t *values,
ccs_datum_t *result) {
ccs_datum_t left;
ccs_datum_t right;
ccs_hyperparameter_type_t htl = CCS_HYPERPARAMETER_TYPE_MAX;
......@@ -473,9 +473,9 @@ static _ccs_expression_ops_t _ccs_expr_less_or_equal_ops = {
static ccs_error_t
_ccs_expr_greater_or_equal_eval(_ccs_expression_data_t *data,
ccs_configuration_space_t context,
ccs_datum_t *values,
ccs_datum_t *result) {
ccs_context_t context,
ccs_datum_t *values,
ccs_datum_t *result) {
ccs_datum_t left;
ccs_datum_t right;
ccs_hyperparameter_type_t htl = CCS_HYPERPARAMETER_TYPE_MAX;
......@@ -525,9 +525,9 @@ static _ccs_expression_ops_t _ccs_expr_greater_or_equal_ops = {
static ccs_error_t
_ccs_expr_in_eval(_ccs_expression_data_t *data,
ccs_configuration_space_t context,
ccs_datum_t *values,
ccs_datum_t *result) {
ccs_context_t context,
ccs_datum_t *values,
ccs_datum_t *result) {
if (data->nodes[1].type != CCS_OBJECT)
return -CCS_INVALID_VALUE;
ccs_object_type_t type;
......@@ -588,9 +588,9 @@ static _ccs_expression_ops_t _ccs_expr_in_ops = {
static ccs_error_t
_ccs_expr_add_eval(_ccs_expression_data_t *data,
ccs_configuration_space_t context,
ccs_datum_t *values,
ccs_datum_t *result) {
ccs_context_t context,
ccs_datum_t *values,
ccs_datum_t *result) {
ccs_datum_t left;
ccs_datum_t right;
eval_left_right(data, context, values, left, right, NULL, NULL);
......@@ -629,9 +629,9 @@ static _ccs_expression_ops_t _ccs_expr_add_ops = {
static ccs_error_t
_ccs_expr_substract_eval(_ccs_expression_data_t *data,
ccs_configuration_space_t context,
ccs_datum_t *values,
ccs_datum_t *result) {
ccs_context_t context,
ccs_datum_t *values,
ccs_datum_t *result) {
ccs_datum_t left;
ccs_datum_t right;
eval_left_right(data, context, values, left, right, NULL, NULL);
......@@ -669,10 +669,10 @@ static _ccs_expression_ops_t _ccs_expr_substract_ops = {
};
static ccs_error_t
_ccs_expr_multiply_eval(_ccs_expression_data_t *data,
ccs_configuration_space_t context,
ccs_datum_t *values,
ccs_datum_t *result) {
_ccs_expr_multiply_eval(_ccs_expression_data_t *data,
ccs_context_t context,
ccs_datum_t *values,
ccs_datum_t *result) {
ccs_datum_t left;
ccs_datum_t right;
eval_left_right(data, context, values, left, right, NULL, NULL);
......@@ -710,10 +710,10 @@ static _ccs_expression_ops_t _ccs_expr_multiply_ops = {
};
static ccs_error_t
_ccs_expr_divide_eval(_ccs_expression_data_t *data,
ccs_configuration_space_t context,
ccs_datum_t *values,
ccs_datum_t *result) {
_ccs_expr_divide_eval(_ccs_expression_data_t *data,
ccs_context_t context,
ccs_datum_t *values,
ccs_datum_t *result) {
ccs_datum_t left;
ccs_datum_t right;
eval_left_right(data, context, values, left, right, NULL, NULL);
......@@ -759,10 +759,10 @@ static _ccs_expression_ops_t _ccs_expr_divide_ops = {
};
static ccs_error_t
_ccs_expr_modulo_eval(_ccs_expression_data_t *data,
ccs_configuration_space_t context,
ccs_datum_t *values,
ccs_datum_t *result) {
_ccs_expr_modulo_eval(_ccs_expression_data_t *data,
ccs_context_t context,
ccs_datum_t *values,
ccs_datum_t *result) {
ccs_datum_t left;
ccs_datum_t right;
eval_left_right(data, context, values, left, right, NULL, NULL);
......@@ -808,10 +808,10 @@ static _ccs_expression_ops_t _ccs_expr_modulo_ops = {
};
static ccs_error_t
_ccs_expr_positive_eval(_ccs_expression_data_t *data,
ccs_configuration_space_t context,
ccs_datum_t *values,
ccs_datum_t *result) {
_ccs_expr_positive_eval(_ccs_expression_data_t *data,
ccs_context_t context,
ccs_datum_t *values,
ccs_datum_t *result) {
ccs_datum_t node;
eval_node(data, context, values, node, NULL);
if (node.type != CCS_INTEGER && node.type != CCS_FLOAT) {
......@@ -827,10 +827,10 @@ static _ccs_expression_ops_t _ccs_expr_positive_ops = {
};
static ccs_error_t
_ccs_expr_negative_eval(_ccs_expression_data_t *data,
ccs_configuration_space_t context,
ccs_datum_t *values,
ccs_datum_t *result) {
_ccs_expr_negative_eval(_ccs_expression_data_t *data,
ccs_context_t context,
ccs_datum_t *values,
ccs_datum_t *result) {
ccs_datum_t node;
eval_node(data, context, values, node, NULL);
if (node.type == CCS_INTEGER) {
......@@ -850,10 +850,10 @@ static _ccs_expression_ops_t _ccs_expr_negative_ops = {
};
static ccs_error_t
_ccs_expr_not_eval(_ccs_expression_data_t *data,
ccs_configuration_space_t context,
ccs_datum_t *values,
ccs_datum_t *result) {
_ccs_expr_not_eval(_ccs_expression_data_t *data,
ccs_context_t context,
ccs_datum_t *values,
ccs_datum_t *result) {
ccs_datum_t node;
eval_node(data, context, values, node, NULL);
if (node.type == CCS_BOOLEAN) {
......@@ -870,10 +870,10 @@ static _ccs_expression_ops_t _ccs_expr_not_ops = {
};
static ccs_error_t
_ccs_expr_list_eval(_ccs_expression_data_t *data,
ccs_configuration_space_t context,
ccs_datum_t *values,
ccs_datum_t *result) {
_ccs_expr_list_eval(_ccs_expression_data_t *data,
ccs_context_t context,
ccs_datum_t *values,
ccs_datum_t *result) {
return -CCS_UNSUPPORTED_OPERATION;
}
......@@ -1031,10 +1031,10 @@ ccs_create_unary_expression(ccs_expression_type_t type,
}
ccs_error_t
ccs_expression_eval(ccs_expression_t expression,
ccs_configuration_space_t context,
ccs_datum_t *values,
ccs_datum_t *result) {
ccs_expression_eval(ccs_expression_t expression,
ccs_context_t context,
ccs_datum_t *values,
ccs_datum_t *result) {
if (!expression || !expression->data)
return -CCS_INVALID_OBJECT;
if (!result)
......@@ -1081,11 +1081,11 @@ ccs_expression_get_nodes(ccs_expression_t expression,
}
ccs_error_t
ccs_expression_list_eval_node(ccs_expression_t expression,
ccs_configuration_space_t context,
ccs_datum_t *values,
size_t index,
ccs_datum_t *result) {
ccs_expression_list_eval_node(ccs_expression_t expression,
ccs_context_t context,
ccs_datum_t *values,
size_t index,
ccs_datum_t *result) {
if (!expression || !expression->data)
return -CCS_INVALID_OBJECT;
if (!result)
......@@ -1206,8 +1206,8 @@ ccs_expression_get_hyperparameters(ccs_expression_t expression,
}
ccs_error_t
ccs_expression_check_context(ccs_expression_t expression,
ccs_configuration_space_t context) {
ccs_expression_check_context(ccs_expression_t expression,
ccs_context_t context) {
ccs_error_t err;
UT_array *array;
utarray_new(array, &_hyperparameter_icd);
......@@ -1227,7 +1227,7 @@ ccs_expression_check_context(ccs_expression_t expression,
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(
err = ccs_context_get_hyperparameter_index(
context, *p_h, &index);
if (err) {
utarray_free(array);
......
......@@ -8,10 +8,10 @@ struct _ccs_expression_ops_s {
_ccs_object_ops_t obj_ops;
ccs_error_t (*eval)(
_ccs_expression_data_t *data,
ccs_configuration_space_t context,
ccs_datum_t *values,
ccs_datum_t *result);
_ccs_expression_data_t *data,
ccs_context_t context,
ccs_datum_t *values,
ccs_datum_t *result);
};
typedef struct _ccs_expression_ops_s _ccs_expression_ops_t;
......
......@@ -63,7 +63,7 @@ void test_expression_wrapper(ccs_expression_type_t type,
err = ccs_create_expression(type, count, nodes, &expression);
assert( err == CCS_SUCCESS );
err = ccs_expression_eval(expression, context, inputs, &result);
err = ccs_expression_eval(expression, (ccs_context_t)context, inputs, &result);
assert( err == eerr );
if (eerr != CCS_SUCCESS) {
err = ccs_release_object(expression);
......@@ -795,7 +795,7 @@ void test_check_context() {
err = ccs_expression_check_context(expression2, NULL);
assert( err == -CCS_INVALID_VALUE );
err = ccs_expression_check_context(expression2, space);
err = ccs_expression_check_context(expression2, (ccs_context_t)space);
assert( err == CCS_SUCCESS );
err = ccs_release_object(expression2);
......@@ -804,7 +804,7 @@ void test_check_context() {
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);
err = ccs_expression_check_context(expression2, (ccs_context_t)space);
assert( err == -CCS_INVALID_HYPERPARAMETER );
err = ccs_release_object(hyperparameter1);
......
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