Coverage Report - net.sourceforge.combean.graph.alg.lp.GraphAsLPModelColumns
 
Classes in this File Line Coverage Branch Coverage Complexity
GraphAsLPModelColumns
69%
18/26
25%
1/4
0
GraphAsLPModelColumns$EdgeAsSparseVector
65%
11/17
N/A
0
GraphAsLPModelColumns$EdgeAsSparseVector$EdgeAsVectorIterator
92%
23/25
83%
5/6
0
 
 1  
 /*
 2  
     This file is part of Combean.
 3  
 
 4  
     Combean is free software; you can redistribute it and/or modify
 5  
     it under the terms of the GNU General Public License as published by
 6  
     the Free Software Foundation; either version 2 of the License, or
 7  
     (at your option) any later version.
 8  
 
 9  
     Combean is distributed in the hope that it will be useful,
 10  
     but WITHOUT ANY WARRANTY; without even the implied warranty of
 11  
     MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 12  
     GNU General Public License for more details.
 13  
 
 14  
     You should have received a copy of the GNU General Public License
 15  
     along with Combean; if not, write to the Free Software
 16  
     Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA  02110-1301  USA
 17  
 */
 18  
 /*
 19  
  * Created on 07.08.2005
 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  
  * Converts a graph into an LP model based on the incidence matrix, i.e.,
 50  
  * the rows correspond to the nodes and the columns correspond to the edges.
 51  
  * The entry (v,e) is set to -1 if edge e is an outgoing edge of v, set to 1
 52  
  * if edge e is an incoming edge of v and zero otherwise. The orientation
 53  
  * of the resulting LP component is columnwise.
 54  
  * 
 55  
  * @author schickin
 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  
      * Constructor.
 68  
      * 
 69  
      * The graph must possess the neighborhood graph property and the
 70  
      * global numbered graph property. The edges are considered to be
 71  
      * directed.
 72  
      * 
 73  
      * @param g The graph to be represented by the LP model.
 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  
         // access graph property neigh in order to satisfy the compiler
 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  
          * @param e
 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  
              * @param edge
 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  
             /* (non-Javadoc)
 128  
              * @see net.sourceforge.combean.interfaces.mathprog.lp.VectorIterator#hasNext()
 129  
              */
 130  
             public boolean hasNext() {
 131  450
                  return this.currNodeNum < 2;
 132  
             }
 133  
             
 134  
             /* (non-Javadoc)
 135  
              * @see net.sourceforge.combean.interfaces.mathprog.lp.VectorIterator#next()
 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  
             /* (non-Javadoc)
 164  
              * @see java.util.Iterator#remove()
 165  
              */
 166  
             public void remove() {
 167  0
                 throw new UnsupportedMethodException();
 168  
             }
 169  
         }
 170  
         
 171  
         /* (non-Javadoc)
 172  
          * @see net.sourceforge.combean.interfaces.mathprog.SparseVector#getDimension()
 173  
          */
 174  
         public int getDimension() {
 175  0
              return this.g.getNumNodes();
 176  
         }
 177  
         
 178  
         /* (non-Javadoc)
 179  
          * @see net.sourceforge.combean.interfaces.mathprog.lp.SparseVector#getIterator()
 180  
          */
 181  
         public VectorIterator<LPVectorLabel> iterator() {
 182  150
              return new EdgeAsVectorIterator(this);
 183  
         }
 184  
         
 185  
         /* (non-Javadoc)
 186  
          * @see net.sourceforge.combean.interfaces.mathprog.lp.SparseVector#getNumIterations()
 187  
          */
 188  
         public int getNumIterations() {
 189  150
              return 2;
 190  
         }
 191  
         
 192  
         /**
 193  
          * @return Returns the edge.
 194  
          */
 195  
         public final Edge getEdge() {
 196  300
             return this.e;
 197  
         }
 198  
         
 199  
         /**
 200  
          * @return Returns the graph.
 201  
          */
 202  
         public final Graph getGraph() {
 203  600
             return this.g;
 204  
         }
 205  
         
 206  
         /* (non-Javadoc)
 207  
          * @see java.lang.Object#toString()
 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  
         /* (non-Javadoc)
 219  
          * @see net.sourceforge.combean.interfaces.mathprog.linalg.SparseVec#getLabel()
 220  
          */
 221  
         public Object getLabel() {
 222  0
              return null;
 223  
         }
 224  
     }
 225  
 
 226  
     /* (non-Javadoc)
 227  
      * @see net.sourceforge.combean.interfaces.mathprog.lp.model.LPModelColumns#getColumnVector(int)
 228  
      */
 229  
     public LPSparseVector getColumnVector(int localColumn) {
 230  150
         return new EdgeAsSparseVector(this.numberedGraph,
 231  
                 this.numberedGraph.getEdge(localColumn));
 232  
     }
 233  
     
 234  
     /* (non-Javadoc)
 235  
      * @see net.sourceforge.combean.interfaces.mathprog.lp.model.LPModelComponent#getNumColumns()
 236  
      */
 237  
     public int getNumColumns() {
 238  180
          return this.numberedGraph.getNumEdges();
 239  
     }
 240  
 
 241  
     /* (non-Javadoc)
 242  
      * @see net.sourceforge.combean.interfaces.mathprog.lp.model.LPModelComponent#getNumRows()
 243  
      */
 244  
     public int getNumRows() {
 245  0
          return this.numberedGraph.getNumNodes();
 246  
     }
 247  
     
 248  
     /* (non-Javadoc)
 249  
      * @see net.sourceforge.combean.interfaces.mathprog.lp.model.LPModelComponent#getRowModelIndex(int)
 250  
      */
 251  
     public LPModelIndex getRowModelIndex(int localRow) {
 252  0
          return new LPModelIndexImpl(localRow, this.nodeRowsOffsetId);
 253  
     }
 254  
     
 255  
     /* (non-Javadoc)
 256  
      * @see net.sourceforge.combean.interfaces.mathprog.lp.model.LPModelComponent#getColumnModelIndex(int)
 257  
      */
 258  
     public LPModelIndex getColumnModelIndex(int localColumn) {
 259  150
          return new LPModelIndexImpl(localColumn, this.edgeColumnsOffsetId);
 260  
     }
 261  
 
 262  
     /* (non-Javadoc)
 263  
      * @see net.sourceforge.combean.interfaces.mathprog.lp.model.LPModelComponent#getOrientation()
 264  
      */
 265  
     public VectorOrientation getOrientation() {
 266  30
          return VectorOrientation.COLUMNWISE;
 267  
     }
 268  
 
 269  
     /* (non-Javadoc)
 270  
      * @see net.sourceforge.combean.interfaces.mathprog.lp.model.LPModelComponent#getColumnOffsetIds()
 271  
      */
 272  
     public Iterator getColumnOffsetIds() {
 273  0
         return new SingletonIterator(this.edgeColumnsOffsetId);
 274  
     }
 275  
     
 276  
     /* (non-Javadoc)
 277  
      * @see net.sourceforge.combean.interfaces.mathprog.lp.model.LPModelComponent#getRowOffsetIds()
 278  
      */
 279  
     public Iterator getRowOffsetIds() {
 280  30
         return new SingletonIterator(this.nodeRowsOffsetId);
 281  
     }
 282  
     
 283  
     /**
 284  
      * @param edgeColumnsOffsetId The edgeColumnsOffsetId to set.
 285  
      */
 286  
     public final void setEdgeColumnsOffsetId(String edgeColumnsOffsetId) {
 287  0
         this.edgeColumnsOffsetId = edgeColumnsOffsetId;
 288  0
     }
 289  
 
 290  
     /**
 291  
      * @return the nodeRowsOffsetId
 292  
      */
 293  
     public String getNodeRowsOffsetId() {
 294  0
         return this.nodeRowsOffsetId;
 295  
     }
 296  
     
 297  
     /**
 298  
      * @param nodeRowsOffsetId The nodeRowsOffsetId to set.
 299  
      */
 300  
     public final void setNodeRowsOffsetId(String nodeRowsOffsetId) {
 301  0
         this.nodeRowsOffsetId = nodeRowsOffsetId;
 302  0
     }
 303  
 }