Coverage Report - net.sourceforge.combean.samples.mathprog.lp.matrixrounding.MatrixRoundingMain
 
Classes in this File Line Coverage Branch Coverage Complexity
MatrixRoundingMain
79%
22/28
50%
1/2
1,143
 
 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 04.08.2006
 20  
  *
 21  
  */
 22  
 package net.sourceforge.combean.samples.mathprog.lp.matrixrounding;
 23  
 
 24  
 import java.io.BufferedReader;
 25  
 import java.io.InputStream;
 26  
 import java.io.InputStreamReader;
 27  
 
 28  
 import net.sourceforge.combean.interfaces.mathprog.linalg.Matrix;
 29  
 import net.sourceforge.combean.interfaces.mathprog.lp.LPSolver;
 30  
 import net.sourceforge.combean.mathprog.linalg.DoubleMatrix;
 31  
 import net.sourceforge.combean.mathprog.lp.model.ConstructableLPModel;
 32  
 import net.sourceforge.combean.mathprog.lp.model.LPModelSolverWithSequentialLoading;
 33  
 import net.sourceforge.combean.mathprog.lp.model.statics.LPSolverAdvisor;
 34  
 
 35  
 import org.apache.commons.logging.Log;
 36  
 import org.apache.commons.logging.LogFactory;
 37  
 
 38  
 /**
 39  
  * @author schickin
 40  
  *
 41  
  */
 42  
 public class MatrixRoundingMain {
 43  
     
 44  3
     private static Log log =
 45  
         LogFactory.getLog(MatrixRoundingMain.class);
 46  
     
 47  
     private ConstructableLPModel lpModel;
 48  
     private LPModelSolverWithSequentialLoading modelSolver;
 49  
     private MatrixToRoundAsLP problemAsLP;
 50  
     
 51  6
     private LPSolver lpSolver = null;
 52  
 
 53  
     /**
 54  
      * 
 55  
      */
 56  
     public MatrixRoundingMain() {
 57  6
         super();
 58  
         
 59  6
         this.lpSolver = LPSolverAdvisor.getGeneralPurposeLPSolver();
 60  6
         this.modelSolver = new LPModelSolverWithSequentialLoading();
 61  6
         this.modelSolver.setLPSolver(this.lpSolver);
 62  6
     }
 63  
 
 64  
     /**
 65  
      * @param args
 66  
      */
 67  
     public static void main(String[] args) {
 68  0
         MatrixRoundingMain alg = new MatrixRoundingMain();
 69  
         
 70  0
         alg.roundMatrix(System.in);
 71  0
     }
 72  
     
 73  
     /**
 74  
      * Round a matrix that is read from an InputStream.
 75  
      * 
 76  
      * @param is
 77  
      */
 78  
     public void roundMatrix(InputStream is) {
 79  0
         roundMatrix(new BufferedReader(new InputStreamReader(is)));
 80  0
     }
 81  
     
 82  
     /**
 83  
      * Round a matrix that is read from a Readable
 84  
      * 
 85  
      * @param dataSource
 86  
      */
 87  
     public void roundMatrix(Readable dataSource) {
 88  6
         MatrixToRoundLoader loader = new MatrixToRoundLoader(dataSource);
 89  
         
 90  6
         roundMatrix(loader.getM());
 91  6
     }
 92  
 
 93  
     /**
 94  
      * Round a given matrix such that the row sums and column sums are also
 95  
      * rounded to one of the two nearest integers, i.e., row sum 12.6 may
 96  
      * only become 12 or 13 but not 14.
 97  
      * 
 98  
      * @param doubleArr the matrix to be rounded
 99  
      */
 100  
     public void roundMatrix(double[][] doubleArr) {
 101  6
         Matrix m = new DoubleMatrix(doubleArr);
 102  
         
 103  6
         this.problemAsLP = new MatrixToRoundAsLP(m);
 104  6
         this.lpModel = new ConstructableLPModel();
 105  6
         this.lpModel.addVariableSequence(this.problemAsLP);
 106  6
         this.lpModel.addConstraintSequence(this.problemAsLP);
 107  6
         this.lpModel.appendModelComponent(this.problemAsLP);
 108  
         
 109  6
         if (log.isDebugEnabled()) {
 110  0
             log.debug("model to be loaded: " + this.lpModel);
 111  
         }
 112  
         
 113  6
         this.modelSolver.loadModel(this.lpModel);
 114  6
         this.modelSolver.solve();
 115  6
     }
 116  
     
 117  
     /**
 118  
      * Returns the rounded matrix.
 119  
      * 
 120  
      * @return the rounded matrix
 121  
      */
 122  
     public double[][] getResult() {
 123  6
         return this.problemAsLP.getRoundedMatrix(this.modelSolver);
 124  
     }
 125  
 
 126  
     /**
 127  
      * @return Returns the lpSolver.
 128  
      */
 129  
     public final LPSolver getLpSolver() {
 130  12
         return this.lpSolver;
 131  
     }
 132  
 }