-
Notifications
You must be signed in to change notification settings - Fork 2
Expand file tree
/
Copy pathlp.lalrpop
More file actions
362 lines (324 loc) · 13.3 KB
/
lp.lalrpop
File metadata and controls
362 lines (324 loc) · 13.3 KB
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
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
use std::borrow::Cow;
use crate::lexer::{Token, LexerError, RawCoefficient, RawConstraint, RawObjective, SosEntryKind, ConstraintCont, OptionalSection, ParseResult};
use crate::model::{
ComparisonOp, SOSType, Sense, VariableType,
};
grammar<'input>;
extern {
type Location = usize;
type Error = LexerError;
enum Token<'input> {
// Keywords
"sense" => Token::SenseKw(<Sense>),
"subject to" => Token::SubjectTo,
"bounds" => Token::Bounds,
"generals" => Token::Generals,
"integers" => Token::Integers,
"binaries" => Token::Binaries,
"semi-continuous" => Token::SemiContinuous,
"sos" => Token::Sos,
"end" => Token::End,
"free" => Token::Free,
"sos_type" => Token::SosType(<SOSType>),
// Values
"infinity" => Token::Infinity(<f64>),
"number" => Token::Number(<f64>),
"identifier" => Token::Identifier(<&'input str>),
// Operators
"<=" => Token::Lte,
">=" => Token::Gte,
"<" => Token::Lt,
">" => Token::Gt,
"=" => Token::Eq,
"+" => Token::Plus,
"-" => Token::Minus,
":" => Token::Colon,
"::" => Token::DoubleColon,
}
}
pub LpProblem: ParseResult<'input> = {
<sense:"sense">
<objectives:ObjectivesSection>
<constraints:ConstraintsSection>
<sections:AnySection*>
"end"?
=> {
let mut bounds: Vec<(&'input str, VariableType)> = Vec::new();
let mut generals: Vec<&'input str> = Vec::new();
let mut integers: Vec<&'input str> = Vec::new();
let mut binaries: Vec<&'input str> = Vec::new();
let mut semi_continuous: Vec<&'input str> = Vec::new();
let mut sos: Vec<RawConstraint<'input>> = Vec::new();
for section in sections {
match section {
OptionalSection::Bounds(b) => bounds.extend(b),
OptionalSection::Generals(g) => generals.extend(g),
OptionalSection::Integers(i) => integers.extend(i),
OptionalSection::Binaries(b) => binaries.extend(b),
OptionalSection::SemiContinuous(s) => semi_continuous.extend(s),
OptionalSection::SOS(s) => sos.extend(s),
}
}
ParseResult { sense, objectives, constraints, bounds, generals, integers, binaries, semi_continuous, sos }
}
};
// Any optional section in any order
AnySection: OptionalSection<'input> = {
<b:BoundsSection> => OptionalSection::Bounds(b),
<g:GeneralsSection> => OptionalSection::Generals(g),
<i:IntegersSection> => OptionalSection::Integers(i),
<b:BinariesSection> => OptionalSection::Binaries(b),
<s:SemiSection> => OptionalSection::SemiContinuous(s),
<s:SosSection> => OptionalSection::SOS(s),
};
ObjectivesSection: Vec<RawObjective<'input>> = {
// Starts with sign: definitely unnamed objective
<loc:@L> <sign:OptSign> <first:UnsignedCoeff> <rest:CoeffTail*> => {
let mut coeffs = vec![RawCoefficient { name: first.name, value: sign * first.value }];
for (s, c) in rest {
coeffs.push(RawCoefficient { name: c.name, value: s * c.value });
}
vec![RawObjective { name: Cow::Borrowed("__obj__"), coefficients: coeffs, byte_offset: Some(loc) }]
},
// Starts with number: definitely unnamed objective
<loc:@L> <num:"number"> <var:"identifier"> <rest:CoeffTail*> => {
let mut coeffs = vec![RawCoefficient { name: var, value: num }];
for (s, c) in rest {
coeffs.push(RawCoefficient { name: c.name, value: s * c.value });
}
vec![RawObjective { name: Cow::Borrowed("__obj__"), coefficients: coeffs, byte_offset: Some(loc) }]
},
// Starts with infinity: definitely unnamed objective (includes sign in token)
<loc:@L> <inf:"infinity"> <var:"identifier"> <rest:CoeffTail*> => {
let mut coeffs = vec![RawCoefficient { name: var, value: inf }];
for (s, c) in rest {
coeffs.push(RawCoefficient { name: c.name, value: s * c.value });
}
vec![RawObjective { name: Cow::Borrowed("__obj__"), coefficients: coeffs, byte_offset: Some(loc) }]
},
// Starts with bare identifier: parse first, then decide
<first_obj:FirstObjective> <more:NamedObjective*> => {
let mut objs = vec![first_obj];
objs.extend(more);
objs
},
};
// First objective starting with identifier - uses left-factoring
FirstObjective: RawObjective<'input> = {
// identifier followed by ":" means named objective
<loc:@L> <name:"identifier"> ":" <coeffs:CoeffList> => {
RawObjective { name: Cow::Borrowed(name), coefficients: coeffs, byte_offset: Some(loc) }
},
// identifier followed by +/- or end of section means unnamed objective
<loc:@L> <var:"identifier"> <rest:CoeffTail*> => {
let mut coeffs = vec![RawCoefficient { name: var, value: 1.0 }];
for (s, c) in rest {
coeffs.push(RawCoefficient { name: c.name, value: s * c.value });
}
RawObjective { name: Cow::Borrowed("__obj__"), coefficients: coeffs, byte_offset: Some(loc) }
},
};
NamedObjective: RawObjective<'input> = {
<loc:@L> <name:"identifier"> ":" <coeffs:CoeffList> => {
RawObjective { name: Cow::Borrowed(name), coefficients: coeffs, byte_offset: Some(loc) }
},
};
// Tail of coefficient list
CoeffTail: (f64, RawCoefficient<'input>) = {
"+" <c:UnsignedCoeff> => (1.0, c),
"-" <c:UnsignedCoeff> => (-1.0, c),
};
// Optional sign
OptSign: f64 = {
"+" => 1.0,
"-" => -1.0,
};
ConstraintsSection: Vec<RawConstraint<'input>> = {
"subject to" <constraints:ConstraintEntry*> => constraints,
};
// A constraint entry can be named or unnamed
ConstraintEntry: RawConstraint<'input> = {
// Starts with sign: definitely unnamed constraint
<loc:@L> <sign:OptSign> <first:UnsignedCoeff> <rest:CoeffTail*> <op:CompOp> <rhs:NumericValue> => {
let mut coeffs = vec![RawCoefficient { name: first.name, value: sign * first.value }];
for (s, c) in rest {
coeffs.push(RawCoefficient { name: c.name, value: s * c.value });
}
RawConstraint::Standard {
name: Cow::Borrowed("__c__"),
coefficients: coeffs,
operator: op,
rhs: rhs,
byte_offset: Some(loc),
}
},
// Starts with number: definitely unnamed constraint
<loc:@L> <num:"number"> <var:"identifier"> <rest:CoeffTail*> <op:CompOp> <rhs:NumericValue> => {
let mut coeffs = vec![RawCoefficient { name: var, value: num }];
for (s, c) in rest {
coeffs.push(RawCoefficient { name: c.name, value: s * c.value });
}
RawConstraint::Standard {
name: Cow::Borrowed("__c__"),
coefficients: coeffs,
operator: op,
rhs: rhs,
byte_offset: Some(loc),
}
},
// Starts with infinity: definitely unnamed constraint (includes sign in token)
<loc:@L> <inf:"infinity"> <var:"identifier"> <rest:CoeffTail*> <op:CompOp> <rhs:NumericValue> => {
let mut coeffs = vec![RawCoefficient { name: var, value: inf }];
for (s, c) in rest {
coeffs.push(RawCoefficient { name: c.name, value: s * c.value });
}
RawConstraint::Standard {
name: Cow::Borrowed("__c__"),
coefficients: coeffs,
operator: op,
rhs: rhs,
byte_offset: Some(loc),
}
},
// Starts with identifier: could be named or unnamed - use left-factoring
<loc:@L> <id:"identifier"> <cont:ConstraintContinuation> => cont.into_constraint(id, Some(loc)),
};
// Constraint continuation after initial identifier
ConstraintContinuation: ConstraintCont<'input> = {
// Named constraint: saw ":" or "::" after identifier
":" <coeffs:CoeffList> <op:CompOp> <rhs:NumericValue> => ConstraintCont::Named(coeffs, op, rhs),
"::" <coeffs:CoeffList> <op:CompOp> <rhs:NumericValue> => ConstraintCont::Named(coeffs, op, rhs),
// Unnamed constraint: identifier was a variable, continue with more coefficients
<rest:CoeffTail*> <op:CompOp> <rhs:NumericValue> => ConstraintCont::Unnamed(rest, op, rhs),
};
CoeffList: Vec<RawCoefficient<'input>> = {
<c:Coeff> => vec![c],
<mut list:CoeffList> "+" <c:UnsignedCoeff> => {
list.push(c);
list
},
<mut list:CoeffList> "-" <c:UnsignedCoeff> => {
list.push(RawCoefficient { name: c.name, value: -c.value });
list
},
};
// Single coefficient (optionally signed at the start)
Coeff: RawCoefficient<'input> = {
<c:UnsignedCoeff> => c,
"+" <c:UnsignedCoeff> => c,
"-" <c:UnsignedCoeff> => RawCoefficient { name: c.name, value: -c.value },
};
// Unsigned coefficient
UnsignedCoeff: RawCoefficient<'input> = {
<var:"identifier"> => RawCoefficient { name: var, value: 1.0 },
<num:"number"> <var:"identifier"> => RawCoefficient { name: var, value: num },
<inf:"infinity"> <var:"identifier"> => RawCoefficient { name: var, value: inf },
};
BoundsSection: Vec<(&'input str, VariableType)> = {
"bounds" <bounds:BoundSpec*> => bounds,
};
BoundSpec: (&'input str, VariableType) = {
// Free variable: "x1 free"
<var:"identifier"> "free" => (var, VariableType::Free),
// Double bound: "0 <= x1 <= 5"
<lb:NumericValue> "<=" <var:"identifier"> "<=" <ub:NumericValue> => {
(var, VariableType::DoubleBound(lb, ub))
},
<lb:NumericValue> "<" <var:"identifier"> "<" <ub:NumericValue> => {
(var, VariableType::DoubleBound(lb, ub))
},
<lb:NumericValue> "<=" <var:"identifier"> "<" <ub:NumericValue> => {
(var, VariableType::DoubleBound(lb, ub))
},
<lb:NumericValue> "<" <var:"identifier"> "<=" <ub:NumericValue> => {
(var, VariableType::DoubleBound(lb, ub))
},
// Lower bound: "x1 >= 5" or "5 <= x1"
<var:"identifier"> ">=" <bound:NumericValue> => (var, VariableType::LowerBound(bound)),
<bound:NumericValue> "<=" <var:"identifier"> => (var, VariableType::LowerBound(bound)),
// Upper bound: "x1 <= 5" or "5 >= x1"
<var:"identifier"> "<=" <bound:NumericValue> => (var, VariableType::UpperBound(bound)),
<bound:NumericValue> ">=" <var:"identifier"> => (var, VariableType::UpperBound(bound)),
// Fixed value (equality bound): "x1 = 5" means x1 is fixed at 5
<var:"identifier"> "=" <bound:NumericValue> => (var, VariableType::DoubleBound(bound, bound)),
<bound:NumericValue> "=" <var:"identifier"> => (var, VariableType::DoubleBound(bound, bound)),
};
GeneralsSection: Vec<&'input str> = {
"generals" <vars:"identifier"*> => vars,
};
IntegersSection: Vec<&'input str> = {
"integers" <vars:"identifier"*> => vars,
};
BinariesSection: Vec<&'input str> = {
"binaries" <vars:"identifier"*> => vars,
};
SemiSection: Vec<&'input str> = {
"semi-continuous" <vars:"identifier"*> => vars,
};
SosSection: Vec<RawConstraint<'input>> = {
"sos" <entries:SosEntry*> => {
// Group entries into constraints
let mut constraints = Vec::new();
let mut current_name: Option<&'input str> = None;
let mut current_type: Option<SOSType> = None;
let mut current_offset: Option<usize> = None;
let mut current_weights: Vec<RawCoefficient<'input>> = Vec::new();
for entry in entries {
match entry {
SosEntryKind::Header(name, sos_type, offset) => {
// Save previous constraint if exists
if let (Some(n), Some(t)) = (current_name.take(), current_type.take()) {
if !current_weights.is_empty() {
constraints.push(RawConstraint::SOS {
name: Cow::Borrowed(n),
sos_type: t,
weights: std::mem::take(&mut current_weights),
byte_offset: current_offset,
});
}
}
current_name = Some(name);
current_type = Some(sos_type);
current_offset = Some(offset);
}
SosEntryKind::Weight(coeff) => {
current_weights.push(coeff);
}
}
}
// Save last constraint
if let (Some(n), Some(t)) = (current_name, current_type) {
if !current_weights.is_empty() {
constraints.push(RawConstraint::SOS {
name: Cow::Borrowed(n),
sos_type: t,
weights: current_weights,
byte_offset: current_offset,
});
}
}
constraints
},
};
// Entry in SOS section - either a constraint header or a weight
SosEntry: SosEntryKind<'input> = {
// Header: "name: S1::" or "name: S2::"
<loc:@L> <name:"identifier"> ":" <sos_type:"sos_type"> "::" => SosEntryKind::Header(name, sos_type, loc),
// Weight: "var:value"
<var:"identifier"> ":" <weight:NumericValue> => SosEntryKind::Weight(RawCoefficient { name: var, value: weight }),
};
CompOp: ComparisonOp = {
"<=" => ComparisonOp::LTE,
">=" => ComparisonOp::GTE,
"<" => ComparisonOp::LT,
">" => ComparisonOp::GT,
"=" => ComparisonOp::EQ,
};
NumericValue: f64 = {
<n:"number"> => n,
<n:"infinity"> => n,
"+" <n:"number"> => n,
"+" <n:"infinity"> => n,
"-" <n:"number"> => -n,
"-" <n:"infinity"> => -n,
};