actions.h 6.47 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"

#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_BOOL2INT 1	// Set to 1 to ignore bool2int 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_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
#define FZN_MAX_ARRAYS 50
#define FZN_MAX_ANNOTATIONS 50

extern int FZN_OPTIMIZE;	// if the the optimization is for minimize or maximize
#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 idx
	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 {
	int* values;
	int* values_aux;
	int max_values;
	int next_position;
	int min;
	int max;
	unsigned int id;
	char* name;
	bool to_label;
	bool var_is_introduced;
	bool is_defined_var;
	int defined_by_constr_idx;
	bool output_var;
	bool ignore;
	int replace_by_v_id;
	bool range;
	bool created;
};

// Flatzinc representation of a CSP constraint
struct fzn_constr {
	char* name;
	unsigned int* vars_id;
	unsigned int* vars_id_aux;
	int next_var_to_add;
	int max_vars;
	int* consts;
	int* consts_aux;
	int next_const_to_add;
	int max_consts;
	int defines_var_idx;
	bool ignore;
	bool boundsZ;
	bool boundsR;
	bool boundsD;
	bool domain;
	bool priority;
	bool priority_v_id;
	unsigned int id;
	int pos_init_2_array;		// position of elements where second array of elements begins (for bool_clause constraint only)
};

// Array for storing values that may be the ID of variables or constant values
struct fzn_array {
	int* values;
	int* values_aux;
	int max_values;
	int next_position;
	char* name;
	bool output_array;
	bool to_label;
	bool var_is_introduced;
	bool is_defined_var;
	bool output_var;
	bool var_array;
};

// Output arrays
struct fzn_output_array {
	unsigned int* vars_id;
	unsigned int* vars_id_aux;
	char *name;
	int max_vars;
	int next_position;
};

// Search annotations
struct fzn_search_annotat {
	char* search_type;
	char* label_array_name;
	char* label_heur;
	char* assign_heur;
	char* search_strategy;
};

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_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 /* SRC_UTILS_FLATZINC_ACTIONS_H */