-
Notifications
You must be signed in to change notification settings - Fork 0
/
ParserPartitions.h
130 lines (118 loc) · 4.27 KB
/
ParserPartitions.h
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
119
120
121
122
123
124
125
126
127
128
129
130
/*!
* \author Ruben Martins - rubenm@andrew.cmu.edu
*
* @section LICENSE
*
* MiniSat, Copyright (c) 2003-2006, Niklas Een, Niklas Sorensson
* Copyright (c) 2007-2010, Niklas Sorensson
* Open-WBO, Copyright (c) 2013-2022, Ruben Martins, Vasco Manquinho, Ines Lynce
* UpMax, Copyright (c) 2022, Pedro Orvalho, Vasco Manquinho, Ruben Martins
*
* Permission is hereby granted, free of charge, to any person obtaining a copy
* of this software and associated documentation files (the "Software"), to deal
* in the Software without restriction, including without limitation the rights
* to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
* copies of the Software, and to permit persons to whom the Software is
* furnished to do so, subject to the following conditions:
*
* The above copyright notice and this permission notice shall be included in
* all copies or substantial portions of the Software.
*
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
* FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
* AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
* LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
* OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
* SOFTWARE.
*
*/
#ifndef ParserPartition_h
#define ParserPartition_h
#include <stdio.h>
#include <set>
#include "MaxSATFormula.h"
#include "ParserMaxSAT.h"
#include "core/SolverTypes.h"
#include "utils/ParseUtils.h"
#ifdef HAS_EXTRA_STREAMBUFFER
#include "utils/StreamBuffer.h"
#endif
using NSPACE::mkLit;
using NSPACE::StreamBuffer;
namespace upmax {
//=================================================================================================
// DIMACS Parser:
template <class B, class MaxSATFormula>
static void parsePwcnf(B &in, MaxSATFormula *maxsat_formula) {
vec<Lit> lits;
std::set<int> ids;
uint64_t hard_weight = UINT64_MAX;
for (;;) {
skipWhitespace(in);
if (*in == EOF)
break;
else if (*in == 'p') {
if (eagerMatch(in, "p pwcnf")) {
maxsat_formula->setProblemType(_WEIGHTED_);
parseInt(in); // Variables
parseInt(in); // Clauses
if (*in != '\r' && *in != '\n') {
hard_weight = parseWeight(in);
maxsat_formula->setHardWeight(hard_weight);
}
skipWhitespace(in);
parseInt(in); // Partitions
} else
printf("c PARSE ERROR! Unexpected char: %c\n", *in),
printf("s UNKNOWN\n"), exit(_ERROR_);
} else if (*in == 'c' || *in == 'p')
skipLine(in);
else {
int partition = parseInt(in);
skipWhitespace(in);
uint64_t weight = readClause(in, maxsat_formula, lits);
if (weight < hard_weight ||
maxsat_formula->getProblemType() == _UNWEIGHTED_) {
assert(weight > 0);
// Updates the maximum weight of soft clauses.
maxsat_formula->setMaximumWeight(weight);
// Updates the sum of the weights of soft clauses.
maxsat_formula->updateSumWeights(weight);
maxsat_formula->addSoftClause(weight, lits);
maxsat_formula->setSoftClausePartition(partition);
ids.insert(partition);
} else {
maxsat_formula->addHardClause(lits);
maxsat_formula->setHardClausePartition(partition);
ids.insert(partition);
}
}
}
std::set<int>::iterator itr;
int max = 0;
for (itr = ids.begin();
itr != ids.end(); itr++)
{
//cout << *itr << " ";
if (*itr > max) max = *itr;
}
//maxsat_formula->setPartitions(ids.size());
maxsat_formula->setPartitions(max);
}
// Inserts problem into solver.
//
template <class MaxSATFormula>
static void parsePwcnfFormula(gzFile input_stream,
MaxSATFormula *maxsat_formula) {
StreamBuffer in(input_stream);
parsePwcnf(in, maxsat_formula);
if (maxsat_formula->getMaximumWeight() == 1)
maxsat_formula->setProblemType(_UNWEIGHTED_);
else
maxsat_formula->setProblemType(_WEIGHTED_);
// maxsat_formula->setInitialVars(maxsat_formula->nVars());
}
//=================================================================================================
} // namespace upmax
#endif