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.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 | |
|
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 | |
|
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 | |
|
75 | |
|
76 | |
|
77 | |
|
78 | |
public void roundMatrix(InputStream is) { |
79 | 0 | roundMatrix(new BufferedReader(new InputStreamReader(is))); |
80 | 0 | } |
81 | |
|
82 | |
|
83 | |
|
84 | |
|
85 | |
|
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 | |
|
95 | |
|
96 | |
|
97 | |
|
98 | |
|
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 | |
|
119 | |
|
120 | |
|
121 | |
|
122 | |
public double[][] getResult() { |
123 | 6 | return this.problemAsLP.getRoundedMatrix(this.modelSolver); |
124 | |
} |
125 | |
|
126 | |
|
127 | |
|
128 | |
|
129 | |
public final LPSolver getLpSolver() { |
130 | 12 | return this.lpSolver; |
131 | |
} |
132 | |
} |