minimize.c 8.65 KB
/*
 * minimize.c
 *
 *  Created on: 30/01/2017
 *      Author: pedro
 */

#ifndef __OPENCL_VERSION__

#include <stddef.h>
#include <stdio.h>
#include <limits.h>

#include "minimize.h"

#include "../bitmaps.h"
#include "../config.h"
#include "../variables.h"

#endif

#include "../kernels/cl_aux_functions.h"
#if CL_D_TYPE == CL_BITMAP
#include "../kernels/cl_bitmaps.h"
#elif CL_D_TYPE == CL_INTERVAL
#include  "../kernels/cl_intervals.h"
#endif
#include "../kernels/cl_constraints.h"
#include "../kernels/cl_variables.h"
#include "../kernels/cl_ttl.h"

#ifndef __OPENCL_VERSION__

/*
 * Creates a new constraint of the min type and return the constraint ID. For optimization
 * Minimize the value of x variable
 * var_to_opt_id - ID of variable to optimize
 */
unsigned int c_minimize(unsigned int var_to_opt_id) {

	if (WORK != OPT) {
		return 0;
	}

	if (USE_CS[MINIMIZE] == 1 || USE_CS[MAXIMIZE] == 1) {
		fprintf(stderr, "\nYou are trying to set more that one optimization constraint. Please use only one MINIMIZE or MAXIMIZE constraint.\n");
		exit(-1);
	}

	// set to include in kernel compilation
	USE_CS[MINIMIZE] = 1;
	USE_NON_CS_REIFI[MINIMIZE] = 1;
	OPT_MODE = DECREASE;
	REV = 1;

	// current value to minimize
	VAL_TO_OPT = VS[var_to_opt_id].max;
	VAR_ID_TO_OPT = var_to_opt_id;

	// creates a new generic constraint
	unsigned int c_id = c_new(&var_to_opt_id, 1, NULL, 0, -1);

	// pointers to this type of constraint functions
	CS[c_id].kind = MINIMIZE;
	CS[c_id].check_sol_f = &minimize_check;
	CS[c_id].constant_val = 0;

	return c_id;
}

/*
 * Creates a new reified constraint of the min type and return the constraint ID. For optimization
 * Minimize the value of x variable
 * var_to_opt_id - ID of variable to optimize
 * reif_v_id - ID of the reification variable
 */
unsigned int c_minimize_reif(unsigned int var_to_opt_id, int reif_v_id) {

	if (VS[reif_v_id].max > 1) {
		v_del_gt(&VS[reif_v_id], 1);

		if (VS[reif_v_id].n_vals == 0) {
			fprintf(stderr, "\nError: Constraint MINIMIZE_REIF makes model inconsistent at creation:\n");
			exit(-1);
		}
	}

	if (WORK != OPT) {
		return 0;
	}

	if (USE_CS[MINIMIZE] == 1 || USE_CS[MAXIMIZE] == 1) {
		fprintf(stderr, "\nYou are trying to set more that one optimization constraint. Please use only one MINIMIZE or MAXIMIZE constraint.\n");
		exit(-1);
	}

	// set to include in kernel compilation
	USE_CS[MINIMIZE] = 1;
	USE_CS_REIFI[MINIMIZE] = 1;
	OPT_MODE = DECREASE;
	REV = 1;

	// current value to minimize
	VAL_TO_OPT = VS[var_to_opt_id].max;
	VAR_ID_TO_OPT = var_to_opt_id;

	// creates a new generic constraint
	unsigned int c_id = c_new(&var_to_opt_id, 1, NULL, 0, reif_v_id);

	// pointers to this type of constraint functions
	CS[c_id].kind = MINIMIZE;
	CS[c_id].check_sol_f = &minimize_check;
	CS[c_id].constant_val = 0;

	return c_id;
}

/*
 * Return true if the min constraint is respected or false if not
 * c - constraint to check if is respected
 * explored - if the CSP was already explored, which mean that all the variables must already be singletons
 * */

#pragma GCC diagnostic push
#pragma GCC diagnostic ignored "-Wunused-parameter"
bool minimize_check(constr* c, bool explored) {
	// check if the variable to optimize has domain 0, more than one value or if a is not lower than VAL_TO_OPT. If so, return false. Else return true.
	if (
#if CHECK_SOL_N_VALS
			(VS[VAR_ID_TO_OPT].to_label && VS[VAR_ID_TO_OPT].n_vals != 1) ||
#endif
			VS[VAR_ID_TO_OPT].min > VAL_TO_OPT + 1) {

		if (explored) {
			fprintf(stderr, "\nError: Constraint MINIMIZE (%d) not respected:\n", c->c_id);
			fprintf(stderr, "Variable ID=%u -> minimum=%u, maximum=%u, number of values=%u, cost=%u\n\n", VS[VAR_ID_TO_OPT].v_id, b_get_min_val(&VS[VAR_ID_TO_OPT].domain_b),
					b_get_max_val(&VS[VAR_ID_TO_OPT].domain_b), b_cnt_vals(&VS[VAR_ID_TO_OPT].domain_b), VAL_TO_OPT + 1);
		}
		return false;
	}

	return true;
}
#pragma GCC diagnostic pop

#endif

#if CS_MINIMIZE == 1
/*
 * Update the values of the variable to minimize
 * prop_ok will be set to 1 if success or to 0 if any domain became empty
 * vs_per_c_idx - vector with all constrained variables ID per constraint, per constraint ID order
 * vs_prop_ - all CSP variables with current step values
 * prop_v_id - variable ID to propagate
 * current_cs - constraint that should be propagated for the variable with prop_v_id ID
 * vs_id_to_prop_ - circular vector with the ids of the variables to propagate
 * global value to optimize
 */
CUDA_FUNC void minimize_prop(CL_MEMORY VARS_PROP* vs_prop_, CL_MEMORY unsigned short* vs_id_to_prop_, __global unsigned int* val_to_opt, bool* prop_ok TTL_CTR) {

	unsigned int val_to_opt_aux = atomic_and(val_to_opt, UINT_MAX);
	bool changed = 0;

	cl_v_del_gt_m(&changed, &vs_prop_[CL_VAR_ID_TO_OPT], convert_int(val_to_opt_aux) TTL_CTR_V);
	if (changed) {

		// if the removal of the value resulted in an empty domain return 0
		if (V_IS_EMPTY(vs_prop_[CL_VAR_ID_TO_OPT])) {
			*prop_ok = 0;
			return;
		}
		// Add variable to the vector that contains the variables that must be propagated
		v_add_to_prop(vs_id_to_prop_, vs_prop_, CL_VAR_ID_TO_OPT);
	}

#if CL_FILTERING
	else {
		*val_to_opt = V_MAX(vs_prop_[CL_VAR_ID_TO_OPT]);
	}
#endif
}

#if CS_R_MINIMIZE == 1
/*
 * Validate min constraint to be normally propagated, when reified
 * vs_per_c_idx - vector with all constrained variables ID per constraint, per constraint ID order
 * vs_prop_ - all CSP variables with current step values
 * current_cs - constraint that should be propagated for the variable with prop_v_id ID
 * vs_id_to_prop_ - circular vector with the ids of the variables to propagate
 */
CUDA_FUNC void minimize_reif(CL_MEMORY VARS_PROP* vs_prop_, CL_CS_MEM cl_constr* current_cs, CL_MEMORY unsigned short* vs_id_to_prop_, __global unsigned int* val_to_opt TTL_CTR) {

	VARS_PROP v_to_opt;
	bool changed = 0;

	// constraint already fixed
	if (V_N_VALS(vs_prop_[CL_VAR_ID_TO_OPT]) == 1 && V_MIN(vs_prop_[CL_VAR_ID_TO_OPT]) < *val_to_opt) {
		cl_v_bool_del_val_m(&vs_prop_[current_cs->reif_var_id], 0 TTL_CTR_V);
		v_add_to_prop(vs_id_to_prop_, vs_prop_, convert_int(current_cs->reif_var_id));
		v_add_to_prop(vs_id_to_prop_, vs_prop_, CL_VAR_ID_TO_OPT);

		return;
	}

	cl_v_copy_pm(&v_to_opt, &vs_prop_[CL_VAR_ID_TO_OPT] TTL_CTR_V);

	cl_v_del_gt_n(&changed, &v_to_opt, convert_int(*val_to_opt) TTL_CTR_V);

	// if the removal of the value resulted in an empty domain return 0
	if (V_IS_EMPTY(v_to_opt)) {
		cl_v_bool_del_val_m(&vs_prop_[current_cs->reif_var_id], 1 TTL_CTR_V);
		v_add_to_prop(vs_id_to_prop_, vs_prop_, convert_int(current_cs->reif_var_id));
		v_add_to_prop(vs_id_to_prop_, vs_prop_, CL_VAR_ID_TO_OPT);
	}
}

/*
 * Update the values of the variable to maximize
 * vs_per_c_idx - vector with all constrained variables ID per constraint, per constraint ID order
 * vs_prop_ - all CSP variables with current step values
 * prop_v_id - variable ID to propagate
 * current_cs - constraint that should be propagated for the variable with prop_v_id ID
 * vs_id_to_prop_ - circular vector with the ids of the variables to propagate
 * global value to optimize
 */
CUDA_FUNC void minimize_prop_opposite(CL_MEMORY VARS_PROP* vs_prop_, CL_MEMORY unsigned short* vs_id_to_prop_, __global unsigned int* val_to_opt, bool* prop_ok TTL_CTR) {

	unsigned int val_to_opt_aux = atomic_and(val_to_opt, UINT_MAX);
	bool changed = 0;

	cl_v_del_lt_m(&changed, &vs_prop_[CL_VAR_ID_TO_OPT], convert_int(val_to_opt_aux) TTL_CTR_V);
	if (changed) {

		// if the removal of the value resulted in an empty domain return 0
		if (V_IS_EMPTY(vs_prop_[CL_VAR_ID_TO_OPT])) {
			*prop_ok = 0;
			return;
		}
		// Add variable to the vector that contains the variables that must be propagated
		v_add_to_prop(vs_id_to_prop_, vs_prop_, CL_VAR_ID_TO_OPT);
	}
}

#endif

CUDA_FUNC void minimize_propagate(CL_MEMORY VARS_PROP* vs_prop_, CL_CS_MEM cl_constr* current_cs, CL_MEMORY unsigned short* vs_id_to_prop_, __global unsigned int* val_to_opt_g,
		bool* prop_ok PROPAGATED_FUNC TTL_CTR) {

#if CS_R_MINIMIZE == 0
	minimize_prop(vs_prop_, vs_id_to_prop_, val_to_opt_g, prop_ok TTL_CTR_V);
#if CL_STATS == 1
	*propagated = true;
#endif
#elif CS_R_MINIMIZE == 1
	if (current_cs->reified == 1) {
		if (V_N_VALS(vs_prop_[current_cs->reif_var_id]) > 1) {
			minimize_reif(vs_prop_, current_cs, vs_id_to_prop_, val_to_opt_g TTL_CTR_V);
		}
		if (V_N_VALS(vs_prop_[current_cs->reif_var_id]) == 1) {
			if (V_MIN(vs_prop_[current_cs->reif_var_id]) == 1) {
				minimize_prop(vs_prop_, vs_id_to_prop_, val_to_opt_g, prop_ok TTL_CTR_V);
			} else {
				minimize_prop_opposite(vs_prop_, vs_id_to_prop_, val_to_opt_g, prop_ok TTL_CTR_V);
			}
#if CL_STATS == 1
			*propagated = true;
#endif
		}
	} else {
		minimize_prop(vs_prop_, vs_id_to_prop_, val_to_opt_g, prop_ok TTL_CTR_V);
#if CL_STATS == 1
		*propagated = true;
#endif
	}
#endif
}

#endif