actions.h 9.45 KB
/*
 * actions.h
 *
 *  Created on: 15/12/2017
 *      Author: Pedro
 */

#ifndef SRC_UTILS_FLATZINC_ACTIONS_H
#define SRC_UTILS_FLATZINC_ACTIONS_H

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

#if COMPILE_FZN

#define FZN_NE2ALL_DIFF 1		// Try to detect MiniZinc ALL DIFFERENT constraints that were changed to FlatZinc NE constraints, and reverse the change
#define FZN_REPLACE_INT_EQ 1	// Set to 1 to ignore bool2int and bool_eq and int_eq constraints and merge both variables

#define FZN_VERBOSE 0		// Print some information during the interpretation process
#define FZN_DEBUG 0			// Print some more information from yacc and bison
#define FZN_PRINT_CSP 0		// Print the CSP model loaded from the FZN file
#define FZN_STATS 0			// Print the number of variables, constraints, types of constraints and maximum domain value of the FlatZinc model
#define FZN_MAP_FZN2PHACT 0		// Print PHACT variables and constraints ID followed by the FZN correspondent
#define FZN_LABEL_ALL_VARS 0	// Mark all the CSP variables for labeling
#define FZN_LABEL_DEFINED_VARS 0		// Set for labeling all the variables marked as "is_defined_var"
#define FZN_LABEL_OUTPUT_VS 0			// Set to 1 to mark for label only the variables marked for output, when no variables are marked for labeling
#define FZN_REMOVE_DUPLICATE_CONSTRS 0	// Remove some duplicated constraints (disabled due to much time increment when dealing with many constraints)
#define FZN_SORT_CONSTR_VARS 0			// For some constraints, sort their constrained variable for FZN file input order
#define FZN_REMOVE_ACHIEVED_CONSTRS 0	// Remove some constraints already meet (done by PHACT when initial filtering is enabled)

#define FZN_MAX_ELEMENTS 300	// Maximum initial number of elements for the FlatZinc interpreter structures
#define FZN_MAX_ARRAYS 50		// Maximum initial number of elements for FlatZinc interpreter arrays
#define FZN_MAX_ANNOTATIONS 50	// Maximum initial number of elements for FlatZinc interpreter annotations

// if the the optimization is for minimize or maximize
extern int FZN_OPTIMIZE;
#define FZN_MINIMIZE 1
#define FZN_MAXIMIZE 2

typedef struct fzn_object fzn_object;	// FlatZinc object used until identification as an array, constraint or variable
typedef struct fzn_var fzn_var;			// Flatzinc representation of a CSP variable
typedef struct fzn_var fzn_var;			// Flatzinc representation of a CSP variable
typedef struct fzn_array fzn_array;		// Array for storing values that may be the ID of variables or constant values
typedef struct fzn_constr fzn_constr;	// Flatzinc representation of a CSP constraint
typedef struct fzn_output_array fzn_output_array;		// output arrays
typedef struct fzn_search_annotat fzn_search_annotat;	// FZN search annotations

// FlatZinc object used until identification as an array, constraint or variable
struct fzn_object {
	char **annots;				// array with all the annotations
	char **annots_aux;			// to extend annotations array
	int next_annot;				// index of the next annotation to add to annotations array
	int max_annots;				// maximum current size of the annotations array
	int *elements;				// number of var_idx for arrays and constraints
	int *elements_aux;			// to extend the elements array
	bool *element_is_var;		// if each element on the elements array is a number or a variable ID
	bool *element_is_var_aux;	// to extend element_is_var array
	int next_element;			// next position to add an element to elements array
	int max_elements;			// current maximum elements in the elements array
	bool is_array;				// true if it is an array
	bool is_constraint;			// true if it is a constraint
	bool is_bool;				// true if it is a boolean
	bool range;					// true if the correspondent variable will have a range domain
	int pos_init_2_array;		// position of elements where second array of elements begins (for bool_clause constraint only)
};

// Flatzinc representation of a CSP variable
struct fzn_var {
	unsigned int *values;		// domain values
	unsigned int *values_aux;	// to increase domain values array
	int max_values;		// maximum number of values in values array
	int next_position;	// next position in values array where to put the next value
	int min;	// minimum domain value when range domain
	int max;	// maximum domain value when range domain
	bool range;	// if the variable domain its a range
	int id;		// id o the PHACT variable, after creating it in PHACT structures
	char *name;			// FlatZinc name of the variable
	bool to_label;		// if marked as labeled
	bool var_is_introduced;		// FlatZinc flag
	bool is_defined_var;		// FlatZinc flag
	int defined_by_constr_idx;	// number of the variable that de fines it if marked as is_defined_var
	bool output_var;		// FlatZinc flag. If to be output
	bool ignore;			// If must be ignored after analyzing the constraints
	int replace_by_v_id;	// If ignored, than must be replaced by variable with this ID
	bool replace_other_v;	// If this variable replaces other variables that were ignored
	bool created;			// if already created in PHACT variable structure
#if FZN_SEQ
	label_heur label_h;		// heuristic for labeling this variable, if exists and it is marked for labeling
	assign_heur assign_h;	// heuristic for assignment this variable, if exists and it is marked for labeling
#endif
};

// Flatzinc representation of a CSP constraint
struct fzn_constr {
	char *name;		// FlatZinc name of the constraint
	unsigned int *vars_id;		// array with the FlatZinc ID of the variables that it constrains
	unsigned int *vars_id_aux;	// to extend vars_id array
	int next_var_to_add;	// index of the vars_id to add the ID of the next constrained variable
	int max_vars;			// maximum variables that fit in vars_id
	int *consts;			// array with the constraint constants
	int *consts_aux;		// to extend consts array
	int next_const_to_add;	// index of the consts to add the next constant
	int max_consts;			// maximum constants that fit in consts
	int defines_var_id;		// FlatZinc ID of the variable that this constraint defines
	bool ignore;	// if this constraint must be ignored after analyzing it
	bool boundsZ;	// FlatZinc flag
	bool boundsR;	// FlatZinc flag
	bool boundsD;	// FlatZinc flag
	bool domain;	// FlatZinc flag
	bool priority;	// FlatZinc flag
	bool priority_v_id;		// FlatZinc flag
	int id;					// ID of the constraint after creating it in PHACT structures
	int pos_init_2_array;	// position of elements where second array of elements begins (for constraints that input to arrays of variables)
};

// Array for storing values that may be the ID of variables or constant values
struct fzn_array {
	int *values;		// array values
	int *values_aux;	// to extend values array
	int max_values;		// maximum values in values array
	int next_position;	// position to add the next value to values array
	char *name;			// FlatZinc name of the array
	bool output_array;	// if this array is marked to output
	bool to_label;		// if this array is marked to label
	bool var_is_introduced;	// FlatZinc flag
	bool is_defined_var;	// FlatZinc flag
	bool output_var;		// FlatZinc flag
	bool var_array;		// if its a variable domain
};

// Output arrays
struct fzn_output_array {
	unsigned int *vars_id;		// FlatZinc IDs of the variables to output
	unsigned int *vars_id_aux;	// To extend vars_id
	char *name;			// Name of the FlatZinc array
	int max_vars;		// Maximum elements id vars_id
	int next_position;	// Position in vars_id to add the next variable ID
};

// Search annotations
struct fzn_search_annotat {
	char *search_type;		// Flatzinc search type
	char *label_array_name;	// Name of the array contained the list of variables to label
	char *label_heur;		// labeling heuristic to use when solving the CSP
	char *assign_heur;		// assignment heuristic to use when solving the CSP
	char *search_strategy;	// strategy to use when solving the CSP
};

extern int yylex();
extern int yyparse();
extern FILE *yyin;
void yyerror(const char *s);

void parse(char *fzn_file_name);
void fzn_solve();

void fzn_add_val_to_object(int val);
void fzn_add_var_or_array_to_object(char *name);
void fzn_set_fzn_obj_range(bool range);
void fzn_add_annot_to_object(char *annot);
void fzn_set_2array_init_pos();
void fzn_extend_object_elements();
void fzn_reset_object_elements();

void fzn_new_fzn_obj_array();
void fzn_add_var_to_array(char *var_name);
void fzn_add_val_to_array(int value);
void fzn_array_copy(fzn_array *dest, fzn_array *src, int n_arrays);

int fzn_new_fzn_var_range(int min, int max);
int fzn_new_fzn_obj_var_value(int val);
int fzn_new_fzn_obj_var();
void fzn_vars_copy(fzn_var *dest, fzn_var *src, int n_vs);
void fzn_vars_extend();
void fzn_set_var_to_optimize(char *var_name);

void fzn_new_output_array();
void fzn_output_array_copy(fzn_output_array *dest, fzn_output_array *src, int n_arrays);
void fzn_add_var_id_to_output_array(unsigned int v_id);

void fzn_new_fzn_obj_constr();
void fzn_add_fzn_obj_elem_var_to_constr(int e_idx);
void fzn_add_fzn_obj_elem_val_to_constr(int e_idx);
void fzn_constrs_copy(fzn_constr *dest, fzn_constr *src, int n_constrs);

void fzn_add_fzn_obj_search_annot();
void fzn_search_annots_copy(fzn_search_annotat *dest, fzn_search_annotat *src, int n_search_annots);

void fzn_replace_fzn_v_id(int v_id_to_be_replaced, int v_id_to_replace_the_other);
void fzn_intersect_fzn_vars(int v_id_to_be_replaced, int v_id_to_replace_the_other);

void fzn_set_var_or_array_to_label(char *name);

void fzn_sort_constr_vars();

void fzn_new_csp_var(int var_idx);
void fzn_new_csp_constr(int constr_idx);

void fzn_print_csp();
void fzn_map_fzn2phact();
void fzn_print_stats();

void fzn_init_search_annots();

#endif

#endif /* SRC_UTILS_FLATZINC_ACTIONS_H */