From b683523603f8df618441634e39ed193864bf4b42 Mon Sep 17 00:00:00 2001 From: "lperron@google.com" Date: Tue, 22 Apr 2014 14:46:38 +0000 Subject: [PATCH] more work on fz2 constraints --- makefiles/Makefile.cpp.mk | 8 ++++++- src/flatzinc2/model.cc | 6 +++-- src/flatzinc2/parser_main.cc | 43 +++++++++++++++++++----------------- src/flatzinc2/presolve.cc | 14 ++++++++---- 4 files changed, 44 insertions(+), 27 deletions(-) diff --git a/makefiles/Makefile.cpp.mk b/makefiles/Makefile.cpp.mk index 6818ef1869..648a73f7d8 100644 --- a/makefiles/Makefile.cpp.mk +++ b/makefiles/Makefile.cpp.mk @@ -981,11 +981,17 @@ $(LIB_DIR)/$(LIBPREFIX)fz2.$(DYNAMIC_LIB_SUFFIX): $(FLATZINC2_LIB_OBJS) $(OBJ_DIR)/flatzinc2/fz.$O:$(SRC_DIR)/flatzinc2/fz.cc $(SRC_DIR)/flatzinc2/model.h $(SRC_DIR)/flatzinc2/solver.h $(CCC) $(CFLAGS) -c $(SRC_DIR)$Sflatzinc2$Sfz.cc $(OBJ_OUT)$(OBJ_DIR)$Sflatzinc2$Sfz.$O -fz2 : $(BIN_DIR)/fz2$E +$(OBJ_DIR)/flatzinc2/parser_main.$O:$(SRC_DIR)/flatzinc2/parser_main.cc $(SRC_DIR)/flatzinc2/model.h $(SRC_DIR)/flatzinc2/solver.h + $(CCC) $(CFLAGS) -c $(SRC_DIR)$Sflatzinc2$Sparser_main.cc $(OBJ_OUT)$(OBJ_DIR)$Sflatzinc2$Sparser_main.$O + +fz2 : $(BIN_DIR)/fz2$E $(BIN_DIR)/parser_main$E $(BIN_DIR)/fz2$E: $(OBJ_DIR)/flatzinc2/fz.$O $(DYNAMIC_FLATZINC2_DEPS) $(CCC) $(CFLAGS) $(OBJ_DIR)$Sflatzinc2$Sfz.$O $(STATIC_FZ) $(DYNAMIC_FLATZINC2_LNK) $(DYNAMIC_LD_FLAGS) $(EXE_OUT)$(BIN_DIR)$Sfz2$E +$(BIN_DIR)/parser_main$E: $(OBJ_DIR)/flatzinc2/parser_main.$O $(DYNAMIC_FLATZINC2_DEPS) + $(CCC) $(CFLAGS) $(OBJ_DIR)$Sflatzinc2$Sparser_main.$O $(STATIC_FZ) $(DYNAMIC_FLATZINC2_LNK) $(DYNAMIC_LD_FLAGS) $(EXE_OUT)$(BIN_DIR)$Sparser_main$E + # Flow and linear assignment cpp $(OBJ_DIR)/linear_assignment_api.$O:$(EX_DIR)/cpp/linear_assignment_api.cc diff --git a/src/flatzinc2/model.cc b/src/flatzinc2/model.cc index 138f7f0bd8..9bf7e617a3 100644 --- a/src/flatzinc2/model.cc +++ b/src/flatzinc2/model.cc @@ -250,12 +250,14 @@ std::string FzIntegerVariable::DebugString() const { std::string FzConstraint::DebugString() const { const std::string strong = strong_propagation ? ", strong propagation" : ""; + const std::string trivially_true = is_trivially_true ? "[trivially true]" + : ""; const std::string target = target_var != nullptr ? StringPrintf(" => %s", target_var->name.c_str()) : ""; - return StringPrintf("%s([%s]%s)%s", type.c_str(), + return StringPrintf("%s([%s]%s)%s %s", type.c_str(), JoinDebugString(arguments, ", ").c_str(), strong.c_str(), - target.c_str()); + target.c_str(), trivially_true.c_str()); } // ----- FzAnnotation ----- diff --git a/src/flatzinc2/parser_main.cc b/src/flatzinc2/parser_main.cc index 5e54324568..bffee4e2a3 100644 --- a/src/flatzinc2/parser_main.cc +++ b/src/flatzinc2/parser_main.cc @@ -20,29 +20,32 @@ #include "base/commandlineflags.h" #include "flatzinc2/model.h" #include "flatzinc2/parser.h" +#include "flatzinc2/presolve.h" -DEFINE_string(file, "", "Input file in the flatzinc format"); +DEFINE_string(file, "", "Input file in the flatzinc format."); +DEFINE_bool(presolve, false, "Presolve loaded file."); + +namespace operations_research { +void ParseFile(const string& filename, bool presolve) { + std::string problem_name(filename); + problem_name.resize(problem_name.size() - 4); + size_t found = problem_name.find_last_of("/\\"); + if (found != std::string::npos) { + problem_name = problem_name.substr(found + 1); + } + FzModel model(problem_name); + CHECK(ParseFlatzincFile(filename, &model)); + if (presolve) { + FzPresolver presolve; + presolve.Run(&model); + } + LOG(INFO) << model.DebugString(); +} +} // namespace operations_research int main(int argc, char** argv) { FLAGS_log_prefix = false; - InitGoogle( - "Parses a flatzinc .fzn file and prints it in human-readable " - "format", - &argc, &argv, /*remove_flags=*/true); - std::string problem_name = FLAGS_file; - // Remove the .fzn extension. - QCHECK(problem_name.size() > 4 && - problem_name.substr(problem_name.size() - 4) == ".fzn") - << "Please supply a valid data file name (ending with .fzn) with --file."; - problem_name.resize(problem_name.size() - 4); - // Remove the leading path if present. - size_t basename_offset = problem_name.find_last_of("/\\"); - if (basename_offset != std::string::npos) { - problem_name = problem_name.substr(basename_offset + 1); - } - // Parse the model and print it out. - operations_research::FzModel model(problem_name); - operations_research::ParseFlatzincFile(FLAGS_file, &model); - LOG(INFO) << model.DebugString(); + google::ParseCommandLineFlags(&argc, &argv, /*remove_flags=*/true); + operations_research::ParseFile(FLAGS_file, FLAGS_presolve); return 0; } diff --git a/src/flatzinc2/presolve.cc b/src/flatzinc2/presolve.cc index 72c907505f..42503e25c3 100644 --- a/src/flatzinc2/presolve.cc +++ b/src/flatzinc2/presolve.cc @@ -48,14 +48,19 @@ bool FzPresolver::PresolveBool2Int(FzConstraint* input) { bool FzPresolver::PresolveIntEq(FzConstraint* input) { if (input->arguments[0].type == FzArgument::INT_VAR_REF) { if (input->arguments[1].type == FzArgument::INT_VAR_REF) { - MarkVariablesAsEquivalent(input->arguments[0].variable, - input->arguments[1].variable); + if (input->arguments[0].variable->defining_constraint == nullptr || + input->arguments[1].variable->defining_constraint == nullptr) { + MarkVariablesAsEquivalent(input->arguments[0].variable, + input->arguments[1].variable); + MarkAsTriviallyTrue(input); + return true; + } } else { const int64 value = input->arguments[1].integer_value; input->arguments[0].variable->domain.ReduceDomain(value, value); + MarkAsTriviallyTrue(input); + return true; } - MarkAsTriviallyTrue(input); - return true; } else { // Arg0 is an integer value. const int64 value = input->arguments[0].integer_value; if (input->arguments[1].type == FzArgument::INT_VAR_REF) { @@ -73,6 +78,7 @@ bool FzPresolver::PresolveIntEq(FzConstraint* input) { } } } + return false; } bool FzPresolver::PresolveOneConstraint(FzConstraint* ct) {