forked from MiniZinc/libminizinc
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathminizinc.cpp
118 lines (103 loc) · 3.33 KB
/
minizinc.cpp
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
/* -*- mode: C++; c-basic-offset: 2; indent-tabs-mode: nil -*- */
/*
* Main authors:
* Guido Tack <[email protected]>
* Gleb Belov <[email protected]>
*/
/* This Source Code Form is subject to the terms of the Mozilla Public
* License, v. 2.0. If a copy of the MPL was not distributed with this
* file, You can obtain one at http://mozilla.org/MPL/2.0/. */
/* This (main) file coordinates flattening and solving.
* The corresponding modules are flexibly plugged in
* as derived classes, prospectively from DLLs.
* A flattening module should provide MinZinc::GetFlattener()
* A solving module should provide an object of a class derived from SolverFactory.
* Need to get more flexible for multi-pass & multi-solving stuff TODO
*/
#include <iostream>
#include <fstream>
#include <iomanip>
#include <cstdlib>
#include <minizinc/solver.hh>
#ifdef FLATTEN_ONLY
#define IS_MZN2FZN true
#else
#define IS_MZN2FZN false
#endif
using namespace std;
using namespace MiniZinc;
int main(int argc, const char** argv) {
/// Initializing specified solver factories
/// Gecode has to be 1st for multi-pass
#ifdef HAS_GECODE
static unique_ptr<SolverFactory>
pFactoryGECODE( SolverFactory::createF_GECODE() );
#endif
#ifdef HAS_FZN
static unique_ptr<SolverFactory>
pFactoryFZN( SolverFactory::createF_FZN() );
#endif
#ifdef HAS_CHUFFED
static unique_ptr<SolverFactory>
pFactoryCHUFFED( SolverFactory::createF_CHUFFED() );
#endif
#ifdef HAS_MIP
static unique_ptr<SolverFactory>
pFactoryMIP( SolverFactory::createF_MIP() );
#endif
clock_t starttime = std::clock(), endTime;
bool fSuccess = false;
MznSolver slv(IS_MZN2FZN);
try {
slv.addFlattener();
if (!slv.processOptions(argc, argv, cerr)) {
slv.printHelp(cerr);
exit(EXIT_FAILURE);
}
slv.flatten();
if (SolverInstance::UNKNOWN == slv.getFlt()->status)
{
fSuccess = true;
if ( !slv.ifMzn2Fzn() ) { // only then
GCLock lock;
slv.addSolverInterface();
slv.solve();
}
} else {
fSuccess = (SolverInstance::ERROR != slv.getFlt()->status);
if ( !slv.ifMzn2Fzn() )
slv.s2out.evalStatus( slv.getFlt()->status );
} // Add evalOutput() here? TODO
} catch (const LocationException& e) {
if (slv.get_flag_verbose())
std::cerr << std::endl;
std::cerr << e.loc() << ":" << std::endl;
std::cerr << e.what() << ": " << e.msg() << std::endl;
slv.s2out.evalStatus( SolverInstance::ERROR );
} catch (const Exception& e) {
if (slv.get_flag_verbose())
std::cerr << std::endl;
std::cerr << e.what() << ": " << e.msg() << std::endl;
slv.s2out.evalStatus( SolverInstance::ERROR );
}
catch (const exception& e) {
if (slv.get_flag_verbose())
std::cerr << std::endl;
std::cerr << e.what() << std::endl;
slv.s2out.evalStatus( SolverInstance::ERROR );
}
catch (...) {
if (slv.get_flag_verbose())
std::cerr << std::endl;
std::cerr << " UNKNOWN EXCEPTION." << std::endl;
slv.s2out.evalStatus( SolverInstance::ERROR );
}
if ( !slv.ifMzn2Fzn() ) {
endTime = clock();
if (slv.get_flag_verbose()) {
std::cerr << " Done (";
cerr << "overall time " << timeDiff(endTime, starttime) << ")." << std::endl;
}
}
return !fSuccess;
} // int main()