1 | |
|
2 | |
|
3 | |
|
4 | |
|
5 | |
|
6 | |
|
7 | |
|
8 | |
|
9 | |
|
10 | |
|
11 | |
|
12 | |
|
13 | |
|
14 | |
|
15 | |
|
16 | |
|
17 | |
|
18 | |
|
19 | |
|
20 | |
|
21 | |
|
22 | |
package net.sourceforge.combean.graph.alg.lp; |
23 | |
|
24 | |
import java.util.Iterator; |
25 | |
|
26 | |
import net.sourceforge.combean.graph.except.UnsupportedGraphProperty; |
27 | |
import net.sourceforge.combean.graph.prop.statics.GraphPropertyInitializer; |
28 | |
import net.sourceforge.combean.interfaces.graph.Edge; |
29 | |
import net.sourceforge.combean.interfaces.graph.Graph; |
30 | |
import net.sourceforge.combean.interfaces.graph.Node; |
31 | |
import net.sourceforge.combean.interfaces.graph.prop.GlobalNodesGraphProp; |
32 | |
import net.sourceforge.combean.interfaces.graph.prop.GlobalNumberedGraphProp; |
33 | |
import net.sourceforge.combean.interfaces.graph.prop.NeighborhoodGraphProp; |
34 | |
import net.sourceforge.combean.interfaces.mathprog.linalg.VectorIterator; |
35 | |
import net.sourceforge.combean.interfaces.mathprog.linalg.VectorOrientation; |
36 | |
import net.sourceforge.combean.interfaces.mathprog.linalg.VectorValue; |
37 | |
import net.sourceforge.combean.interfaces.mathprog.lp.model.LPModelColumns; |
38 | |
import net.sourceforge.combean.interfaces.mathprog.lp.model.LPModelIndex; |
39 | |
import net.sourceforge.combean.interfaces.mathprog.lp.model.LPSparseVector; |
40 | |
import net.sourceforge.combean.interfaces.mathprog.lp.model.LPVectorLabel; |
41 | |
import net.sourceforge.combean.mathprog.linalg.DoubleVectorValue; |
42 | |
import net.sourceforge.combean.mathprog.lp.model.LPModelIndexImpl; |
43 | |
import net.sourceforge.combean.mathprog.lp.model.LPVectorLabelImpl; |
44 | |
import net.sourceforge.combean.util.except.UnsupportedMethodException; |
45 | |
|
46 | |
import org.apache.commons.collections.iterators.SingletonIterator; |
47 | |
|
48 | |
|
49 | |
|
50 | |
|
51 | |
|
52 | |
|
53 | |
|
54 | |
|
55 | |
|
56 | |
|
57 | |
|
58 | 3 | public class GraphAsLPModelColumns implements LPModelColumns { |
59 | |
|
60 | 30 | private NeighborhoodGraphProp neigh = null; |
61 | 30 | private GlobalNumberedGraphProp numberedGraph = null; |
62 | |
|
63 | 30 | private String edgeColumnsOffsetId = ""; |
64 | 30 | protected String nodeRowsOffsetId = ""; |
65 | |
|
66 | |
|
67 | |
|
68 | |
|
69 | |
|
70 | |
|
71 | |
|
72 | |
|
73 | |
|
74 | |
|
75 | |
public GraphAsLPModelColumns( |
76 | |
String edgeColumnsOffsetId, String nodeRowOffsetId, |
77 | |
Graph g) { |
78 | 30 | super(); |
79 | |
|
80 | 30 | setGraph(g); |
81 | 30 | this.edgeColumnsOffsetId = edgeColumnsOffsetId; |
82 | 30 | this.nodeRowsOffsetId = nodeRowOffsetId; |
83 | 30 | } |
84 | |
|
85 | |
private void setGraph(Graph g) throws UnsupportedGraphProperty { |
86 | 30 | GraphPropertyInitializer.setGraphProperties(this, g); |
87 | |
|
88 | 30 | assert this.neigh != null; |
89 | 30 | } |
90 | |
|
91 | |
private class EdgeAsSparseVector implements LPSparseVector { |
92 | |
|
93 | 150 | private GlobalNodesGraphProp g = null; |
94 | |
|
95 | 150 | private Edge e = null; |
96 | |
|
97 | |
|
98 | |
|
99 | |
|
100 | 150 | public EdgeAsSparseVector(GlobalNodesGraphProp g, Edge e) { |
101 | 150 | super(); |
102 | |
|
103 | 150 | this.g = g; |
104 | 150 | this.e = e; |
105 | 150 | } |
106 | |
|
107 | 300 | private class EdgeAsVectorIterator |
108 | |
implements VectorIterator<LPVectorLabel> { |
109 | |
|
110 | 150 | private EdgeAsSparseVector edgeVec = null; |
111 | 150 | private int currNodeNum = 0; |
112 | 150 | private LPVectorLabel columnLabel = null; |
113 | |
|
114 | |
|
115 | |
|
116 | |
|
117 | 150 | public EdgeAsVectorIterator(EdgeAsSparseVector edgeVec) { |
118 | 150 | super(); |
119 | |
|
120 | 150 | this.edgeVec = edgeVec; |
121 | 150 | this.currNodeNum = 0; |
122 | 150 | this.columnLabel = new LPVectorLabelImpl( |
123 | |
GraphAsLPModelColumns.this.nodeRowsOffsetId, |
124 | |
VectorOrientation.COLUMNWISE); |
125 | 150 | } |
126 | |
|
127 | |
|
128 | |
|
129 | |
|
130 | |
public boolean hasNext() { |
131 | 450 | return this.currNodeNum < 2; |
132 | |
} |
133 | |
|
134 | |
|
135 | |
|
136 | |
|
137 | |
public VectorValue<LPVectorLabel> next() { |
138 | 300 | NeighborhoodGraphProp neigh = |
139 | |
(NeighborhoodGraphProp) this.edgeVec.getGraph(); |
140 | 300 | GlobalNumberedGraphProp numberedGraph = |
141 | |
(GlobalNumberedGraphProp) this.edgeVec.getGraph(); |
142 | |
|
143 | 300 | Node resultNode = null; |
144 | 300 | double resultVal = 0.0; |
145 | 300 | if (this.currNodeNum == 0) { |
146 | 150 | resultNode = neigh.getFirstNode(this.edgeVec.getEdge()); |
147 | 150 | resultVal = -1.0; |
148 | |
} |
149 | 150 | else if (this.currNodeNum == 1) { |
150 | 150 | resultNode = neigh.getSecondNode(this.edgeVec.getEdge()); |
151 | 150 | resultVal = 1.0; |
152 | |
} |
153 | |
else { |
154 | 0 | throw new IllegalArgumentException("this code may not be reached"); |
155 | |
} |
156 | 300 | this.currNodeNum++; |
157 | |
|
158 | 300 | return new DoubleVectorValue<LPVectorLabel>( |
159 | |
numberedGraph.getNodeNumber(resultNode), |
160 | |
resultVal, this.columnLabel); |
161 | |
} |
162 | |
|
163 | |
|
164 | |
|
165 | |
|
166 | |
public void remove() { |
167 | 0 | throw new UnsupportedMethodException(); |
168 | |
} |
169 | |
} |
170 | |
|
171 | |
|
172 | |
|
173 | |
|
174 | |
public int getDimension() { |
175 | 0 | return this.g.getNumNodes(); |
176 | |
} |
177 | |
|
178 | |
|
179 | |
|
180 | |
|
181 | |
public VectorIterator<LPVectorLabel> iterator() { |
182 | 150 | return new EdgeAsVectorIterator(this); |
183 | |
} |
184 | |
|
185 | |
|
186 | |
|
187 | |
|
188 | |
public int getNumIterations() { |
189 | 150 | return 2; |
190 | |
} |
191 | |
|
192 | |
|
193 | |
|
194 | |
|
195 | |
public final Edge getEdge() { |
196 | 300 | return this.e; |
197 | |
} |
198 | |
|
199 | |
|
200 | |
|
201 | |
|
202 | |
public final Graph getGraph() { |
203 | 600 | return this.g; |
204 | |
} |
205 | |
|
206 | |
|
207 | |
|
208 | |
|
209 | |
public String toString() { |
210 | 0 | StringBuffer result = new StringBuffer(); |
211 | |
|
212 | 0 | result.append(getClass().getName()); |
213 | 0 | result.append("{ edge " + getEdge() + "}"); |
214 | |
|
215 | 0 | return result.toString(); |
216 | |
} |
217 | |
|
218 | |
|
219 | |
|
220 | |
|
221 | |
public Object getLabel() { |
222 | 0 | return null; |
223 | |
} |
224 | |
} |
225 | |
|
226 | |
|
227 | |
|
228 | |
|
229 | |
public LPSparseVector getColumnVector(int localColumn) { |
230 | 150 | return new EdgeAsSparseVector(this.numberedGraph, |
231 | |
this.numberedGraph.getEdge(localColumn)); |
232 | |
} |
233 | |
|
234 | |
|
235 | |
|
236 | |
|
237 | |
public int getNumColumns() { |
238 | 180 | return this.numberedGraph.getNumEdges(); |
239 | |
} |
240 | |
|
241 | |
|
242 | |
|
243 | |
|
244 | |
public int getNumRows() { |
245 | 0 | return this.numberedGraph.getNumNodes(); |
246 | |
} |
247 | |
|
248 | |
|
249 | |
|
250 | |
|
251 | |
public LPModelIndex getRowModelIndex(int localRow) { |
252 | 0 | return new LPModelIndexImpl(localRow, this.nodeRowsOffsetId); |
253 | |
} |
254 | |
|
255 | |
|
256 | |
|
257 | |
|
258 | |
public LPModelIndex getColumnModelIndex(int localColumn) { |
259 | 150 | return new LPModelIndexImpl(localColumn, this.edgeColumnsOffsetId); |
260 | |
} |
261 | |
|
262 | |
|
263 | |
|
264 | |
|
265 | |
public VectorOrientation getOrientation() { |
266 | 30 | return VectorOrientation.COLUMNWISE; |
267 | |
} |
268 | |
|
269 | |
|
270 | |
|
271 | |
|
272 | |
public Iterator getColumnOffsetIds() { |
273 | 0 | return new SingletonIterator(this.edgeColumnsOffsetId); |
274 | |
} |
275 | |
|
276 | |
|
277 | |
|
278 | |
|
279 | |
public Iterator getRowOffsetIds() { |
280 | 30 | return new SingletonIterator(this.nodeRowsOffsetId); |
281 | |
} |
282 | |
|
283 | |
|
284 | |
|
285 | |
|
286 | |
public final void setEdgeColumnsOffsetId(String edgeColumnsOffsetId) { |
287 | 0 | this.edgeColumnsOffsetId = edgeColumnsOffsetId; |
288 | 0 | } |
289 | |
|
290 | |
|
291 | |
|
292 | |
|
293 | |
public String getNodeRowsOffsetId() { |
294 | 0 | return this.nodeRowsOffsetId; |
295 | |
} |
296 | |
|
297 | |
|
298 | |
|
299 | |
|
300 | |
public final void setNodeRowsOffsetId(String nodeRowsOffsetId) { |
301 | 0 | this.nodeRowsOffsetId = nodeRowsOffsetId; |
302 | 0 | } |
303 | |
} |