1 | |
|
2 | |
|
3 | |
|
4 | |
|
5 | |
|
6 | |
|
7 | |
|
8 | |
|
9 | |
|
10 | |
|
11 | |
|
12 | |
|
13 | |
|
14 | |
|
15 | |
|
16 | |
|
17 | |
|
18 | |
package net.sourceforge.combean.mathprog.grooml; |
19 | |
|
20 | |
import net.sourceforge.combean.interfaces.mathprog.lp.model.LPVectorValue; |
21 | |
import net.sourceforge.combean.interfaces.mathprog.lp.model.LPVectorLabel; |
22 | |
import net.sourceforge.combean.interfaces.mathprog.lp.model.LPModelComponent; |
23 | |
import net.sourceforge.combean.interfaces.mathprog.linalg.VectorOrientation; |
24 | |
import net.sourceforge.combean.mathprog.lp.model.LPVectorLabelImpl; |
25 | |
|
26 | |
import net.sourceforge.combean.except.GModelException; |
27 | |
|
28 | |
|
29 | |
|
30 | |
|
31 | |
|
32 | |
|
33 | |
|
34 | |
|
35 | |
|
36 | |
|
37 | |
|
38 | |
|
39 | |
|
40 | |
class GTerm { |
41 | |
|
42 | |
String var; |
43 | |
Object idx; |
44 | |
double coeff; |
45 | |
|
46 | |
|
47 | |
|
48 | |
|
49 | |
|
50 | |
|
51 | |
|
52 | |
|
53 | |
|
54 | |
public GTerm(String var, Object idx, double coeff) { |
55 | 7195 | assert var != null; |
56 | 7195 | assert coeff != 0.0; |
57 | |
|
58 | 7195 | this.var = var; |
59 | 7195 | this.idx = idx; |
60 | 7195 | this.coeff = coeff; |
61 | |
} |
62 | |
|
63 | |
public String toString() { |
64 | 645 | String idxStr; |
65 | 645 | if (idx == null) { |
66 | 35 | idxStr = ""; |
67 | |
} |
68 | 610 | else if (idx instanceof List) { |
69 | 375 | idxStr = idx.join(",") |
70 | |
} |
71 | |
else { |
72 | 235 | idxStr = idx.toString(); |
73 | |
} |
74 | 645 | return "$coeff $var[$idxStr]"; |
75 | |
} |
76 | |
|
77 | |
public double doubleValue() { |
78 | 1170 | return this.coeff; |
79 | |
} |
80 | |
|
81 | |
public Object index() { |
82 | 1175 | return this.idx; |
83 | |
} |
84 | |
|
85 | |
public void setIndex(Object idx) { |
86 | 1440 | this.idx = idx; |
87 | |
} |
88 | |
|
89 | |
public GTerm clone() { |
90 | 1440 | return new GTerm(this.var, this.idx, this.coeff); |
91 | |
} |
92 | |
|
93 | |
public GTerm scale(Number factor) { |
94 | 280 | return new GTerm(this.var, this.idx, this.coeff * factor); |
95 | |
} |
96 | |
|
97 | |
|
98 | |
|
99 | |
|
100 | |
|
101 | |
|
102 | |
|
103 | |
|
104 | |
|
105 | |
|
106 | |
|
107 | |
|
108 | |
public static GTerm parseTerm(String term) |
109 | |
throws GModelException |
110 | |
{ |
111 | 5290 | final String DOUBLENUM = /-?\s*\d*\.?\d*/; |
112 | 5290 | final String VARID = /[\w_]+/; |
113 | 5290 | final String INTEGER = /-?\s*\d+/; |
114 | 5290 | final String NAME = /[a-zA-Z_][\w\d_]*/; |
115 | |
|
116 | 5290 | String termStripped = term.trim(); |
117 | |
def match = |
118 | 5290 | termStripped =~ /^($DOUBLENUM)\s*\*?\s*($VARID)\s*\[([^]]*)\]$/; |
119 | |
|
120 | 5290 | if (match.count != 1) { |
121 | 0 | throw new GModelException( |
122 | |
"Cannot parse term definition '$termStripped'."); |
123 | |
} |
124 | |
|
125 | 5290 | String coeffStr = match[0][1].replaceAll(/\s+/, ""); |
126 | 5290 | double coeff = coeffStr.length() == 0 ? 1.0 : Double.parseDouble(coeffStr); |
127 | |
|
128 | 5290 | String var = match[0][2]; |
129 | |
|
130 | 5290 | String idxStr = match[0][3].trim(); |
131 | 5290 | Object idx = null; |
132 | 5290 | if (idxStr ==~ /\s*/) { |
133 | 45 | idx = null; |
134 | |
} |
135 | 5245 | else if (idxStr ==~ /$INTEGER/) { |
136 | 380 | idx = new Integer(idxStr); |
137 | |
} |
138 | 4865 | else if (idxStr ==~ /$NAME/) { |
139 | 80 | idx = idxStr; |
140 | |
} |
141 | 4785 | else if (idxStr ==~ /($INTEGER\s*,\s*)+$INTEGER/) { |
142 | 285 | idx = idxStr.split(/\s*,\s*/).collect{ |
143 | 570 | new Integer(it.replaceAll(/\s+/, "")); |
144 | |
} |
145 | |
} |
146 | 4500 | else if (idxStr ==~ /($NAME\s*,\s*)+$NAME/) { |
147 | 4500 | idx = idxStr.split(/\s*,\s*/).collect{ it; } |
148 | |
} |
149 | |
else { |
150 | 0 | throw new GModelException( |
151 | |
"Cannot parse index format '$idxStr' in term definition '$term'."); |
152 | |
} |
153 | |
|
154 | 5290 | return new GTerm(var, idx, coeff); |
155 | |
} |
156 | |
} |