source: src/main/java/agents/anac/y2019/harddealer/math3/linear/PreconditionedIterativeLinearSolver.java

Last change on this file was 204, checked in by Katsuhide Fujita, 5 years ago

Fixed errors of ANAC2019 agents

  • Property svn:executable set to *
File size: 9.2 KB
Line 
1/*
2 * Licensed to the Apache Software Foundation (ASF) under one or more
3 * contributor license agreements. See the NOTICE file distributed with
4 * this work for additional information regarding copyright ownership.
5 * The ASF licenses this file to You under the Apache License, Version 2.0
6 * (the "License"); you may not use this file except in compliance with
7 * the License. You may obtain a copy of the License at
8 *
9 * http://www.apache.org/licenses/LICENSE-2.0
10 *
11 * Unless required by applicable law or agreed to in writing, software
12 * distributed under the License is distributed on an "AS IS" BASIS,
13 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14 * See the License for the specific language governing permissions and
15 * limitations under the License.
16 */
17package agents.anac.y2019.harddealer.math3.linear;
18
19import agents.anac.y2019.harddealer.math3.exception.DimensionMismatchException;
20import agents.anac.y2019.harddealer.math3.exception.MaxCountExceededException;
21import agents.anac.y2019.harddealer.math3.exception.NullArgumentException;
22import agents.anac.y2019.harddealer.math3.util.IterationManager;
23import agents.anac.y2019.harddealer.math3.util.MathUtils;
24
25/**
26 * <p>
27 * This abstract class defines preconditioned iterative solvers. When A is
28 * ill-conditioned, instead of solving system A &middot; x = b directly, it is
29 * preferable to solve either
30 * <center>
31 * (M &middot; A) &middot; x = M &middot; b
32 * </center>
33 * (left preconditioning), or
34 * <center>
35 * (A &middot; M) &middot; y = b, &nbsp;&nbsp;&nbsp;&nbsp;followed by
36 * M &middot; y = x
37 * </center>
38 * (right preconditioning), where M approximates in some way A<sup>-1</sup>,
39 * while matrix-vector products of the type M &middot; y remain comparatively
40 * easy to compute. In this library, M (not M<sup>-1</sup>!) is called the
41 * <em>preconditionner</em>.
42 * </p>
43 * <p>
44 * Concrete implementations of this abstract class must be provided with the
45 * preconditioner M, as a {@link RealLinearOperator}.
46 * </p>
47 *
48 * @since 3.0
49 */
50public abstract class PreconditionedIterativeLinearSolver
51 extends IterativeLinearSolver {
52
53 /**
54 * Creates a new instance of this class, with default iteration manager.
55 *
56 * @param maxIterations the maximum number of iterations
57 */
58 public PreconditionedIterativeLinearSolver(final int maxIterations) {
59 super(maxIterations);
60 }
61
62 /**
63 * Creates a new instance of this class, with custom iteration manager.
64 *
65 * @param manager the custom iteration manager
66 * @throws NullArgumentException if {@code manager} is {@code null}
67 */
68 public PreconditionedIterativeLinearSolver(final IterationManager manager)
69 throws NullArgumentException {
70 super(manager);
71 }
72
73 /**
74 * Returns an estimate of the solution to the linear system A &middot; x =
75 * b.
76 *
77 * @param a the linear operator A of the system
78 * @param m the preconditioner, M (can be {@code null})
79 * @param b the right-hand side vector
80 * @param x0 the initial guess of the solution
81 * @return a new vector containing the solution
82 * @throws NullArgumentException if one of the parameters is {@code null}
83 * @throws NonSquareOperatorException if {@code a} or {@code m} is not
84 * square
85 * @throws DimensionMismatchException if {@code m}, {@code b} or
86 * {@code x0} have dimensions inconsistent with {@code a}
87 * @throws MaxCountExceededException at exhaustion of the iteration count,
88 * unless a custom
89 * {@link agents.anac.y2019.harddealer.math3.util.Incrementor.MaxCountExceededCallback callback}
90 * has been set at construction of the {@link IterationManager}
91 */
92 public RealVector solve(final RealLinearOperator a,
93 final RealLinearOperator m, final RealVector b, final RealVector x0)
94 throws NullArgumentException, NonSquareOperatorException,
95 DimensionMismatchException, MaxCountExceededException {
96 MathUtils.checkNotNull(x0);
97 return solveInPlace(a, m, b, x0.copy());
98 }
99
100 /** {@inheritDoc} */
101 @Override
102 public RealVector solve(final RealLinearOperator a, final RealVector b)
103 throws NullArgumentException, NonSquareOperatorException,
104 DimensionMismatchException, MaxCountExceededException {
105 MathUtils.checkNotNull(a);
106 final RealVector x = new ArrayRealVector(a.getColumnDimension());
107 x.set(0.);
108 return solveInPlace(a, null, b, x);
109 }
110
111 /** {@inheritDoc} */
112 @Override
113 public RealVector solve(final RealLinearOperator a, final RealVector b,
114 final RealVector x0)
115 throws NullArgumentException, NonSquareOperatorException,
116 DimensionMismatchException, MaxCountExceededException {
117 MathUtils.checkNotNull(x0);
118 return solveInPlace(a, null, b, x0.copy());
119 }
120
121 /**
122 * Performs all dimension checks on the parameters of
123 * {@link #solve(RealLinearOperator, RealLinearOperator, RealVector, RealVector) solve}
124 * and
125 * {@link #solveInPlace(RealLinearOperator, RealLinearOperator, RealVector, RealVector) solveInPlace},
126 * and throws an exception if one of the checks fails.
127 *
128 * @param a the linear operator A of the system
129 * @param m the preconditioner, M (can be {@code null})
130 * @param b the right-hand side vector
131 * @param x0 the initial guess of the solution
132 * @throws NullArgumentException if one of the parameters is {@code null}
133 * @throws NonSquareOperatorException if {@code a} or {@code m} is not
134 * square
135 * @throws DimensionMismatchException if {@code m}, {@code b} or
136 * {@code x0} have dimensions inconsistent with {@code a}
137 */
138 protected static void checkParameters(final RealLinearOperator a,
139 final RealLinearOperator m, final RealVector b, final RealVector x0)
140 throws NullArgumentException, NonSquareOperatorException,
141 DimensionMismatchException {
142 checkParameters(a, b, x0);
143 if (m != null) {
144 if (m.getColumnDimension() != m.getRowDimension()) {
145 throw new NonSquareOperatorException(m.getColumnDimension(),
146 m.getRowDimension());
147 }
148 if (m.getRowDimension() != a.getRowDimension()) {
149 throw new DimensionMismatchException(m.getRowDimension(),
150 a.getRowDimension());
151 }
152 }
153 }
154
155 /**
156 * Returns an estimate of the solution to the linear system A &middot; x =
157 * b.
158 *
159 * @param a the linear operator A of the system
160 * @param m the preconditioner, M (can be {@code null})
161 * @param b the right-hand side vector
162 * @return a new vector containing the solution
163 * @throws NullArgumentException if one of the parameters is {@code null}
164 * @throws NonSquareOperatorException if {@code a} or {@code m} is not
165 * square
166 * @throws DimensionMismatchException if {@code m} or {@code b} have
167 * dimensions inconsistent with {@code a}
168 * @throws MaxCountExceededException at exhaustion of the iteration count,
169 * unless a custom
170 * {@link agents.anac.y2019.harddealer.math3.util.Incrementor.MaxCountExceededCallback callback}
171 * has been set at construction of the {@link IterationManager}
172 */
173 public RealVector solve(RealLinearOperator a, RealLinearOperator m,
174 RealVector b) throws NullArgumentException, NonSquareOperatorException,
175 DimensionMismatchException, MaxCountExceededException {
176 MathUtils.checkNotNull(a);
177 final RealVector x = new ArrayRealVector(a.getColumnDimension());
178 return solveInPlace(a, m, b, x);
179 }
180
181 /**
182 * Returns an estimate of the solution to the linear system A &middot; x =
183 * b. The solution is computed in-place (initial guess is modified).
184 *
185 * @param a the linear operator A of the system
186 * @param m the preconditioner, M (can be {@code null})
187 * @param b the right-hand side vector
188 * @param x0 the initial guess of the solution
189 * @return a reference to {@code x0} (shallow copy) updated with the
190 * solution
191 * @throws NullArgumentException if one of the parameters is {@code null}
192 * @throws NonSquareOperatorException if {@code a} or {@code m} is not
193 * square
194 * @throws DimensionMismatchException if {@code m}, {@code b} or
195 * {@code x0} have dimensions inconsistent with {@code a}
196 * @throws MaxCountExceededException at exhaustion of the iteration count,
197 * unless a custom
198 * {@link agents.anac.y2019.harddealer.math3.util.Incrementor.MaxCountExceededCallback callback}
199 * has been set at construction of the {@link IterationManager}
200 */
201 public abstract RealVector solveInPlace(RealLinearOperator a,
202 RealLinearOperator m, RealVector b, RealVector x0) throws
203 NullArgumentException, NonSquareOperatorException,
204 DimensionMismatchException, MaxCountExceededException;
205
206 /** {@inheritDoc} */
207 @Override
208 public RealVector solveInPlace(final RealLinearOperator a,
209 final RealVector b, final RealVector x0) throws
210 NullArgumentException, NonSquareOperatorException,
211 DimensionMismatchException, MaxCountExceededException {
212 return solveInPlace(a, null, b, x0);
213 }
214}
Note: See TracBrowser for help on using the repository browser.