source: src/main/java/agents/anac/y2019/harddealer/math3/linear/IterativeLinearSolver.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: 7.6 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 * This abstract class defines an iterative solver for the linear system A
27 * &middot; x = b. In what follows, the <em>residual</em> r is defined as r = b
28 * - A &middot; x, where A is the linear operator of the linear system, b is the
29 * right-hand side vector, and x the current estimate of the solution.
30 *
31 * @since 3.0
32 */
33public abstract class IterativeLinearSolver {
34
35 /** The object in charge of managing the iterations. */
36 private final IterationManager manager;
37
38 /**
39 * Creates a new instance of this class, with default iteration manager.
40 *
41 * @param maxIterations the maximum number of iterations
42 */
43 public IterativeLinearSolver(final int maxIterations) {
44 this.manager = new IterationManager(maxIterations);
45 }
46
47 /**
48 * Creates a new instance of this class, with custom iteration manager.
49 *
50 * @param manager the custom iteration manager
51 * @throws NullArgumentException if {@code manager} is {@code null}
52 */
53 public IterativeLinearSolver(final IterationManager manager)
54 throws NullArgumentException {
55 MathUtils.checkNotNull(manager);
56 this.manager = manager;
57 }
58
59 /**
60 * Performs all dimension checks on the parameters of
61 * {@link #solve(RealLinearOperator, RealVector, RealVector) solve} and
62 * {@link #solveInPlace(RealLinearOperator, RealVector, RealVector) solveInPlace},
63 * and throws an exception if one of the checks fails.
64 *
65 * @param a the linear operator A of the system
66 * @param b the right-hand side vector
67 * @param x0 the initial guess of the solution
68 * @throws NullArgumentException if one of the parameters is {@code null}
69 * @throws NonSquareOperatorException if {@code a} is not square
70 * @throws DimensionMismatchException if {@code b} or {@code x0} have
71 * dimensions inconsistent with {@code a}
72 */
73 protected static void checkParameters(final RealLinearOperator a,
74 final RealVector b, final RealVector x0) throws
75 NullArgumentException, NonSquareOperatorException,
76 DimensionMismatchException {
77 MathUtils.checkNotNull(a);
78 MathUtils.checkNotNull(b);
79 MathUtils.checkNotNull(x0);
80 if (a.getRowDimension() != a.getColumnDimension()) {
81 throw new NonSquareOperatorException(a.getRowDimension(),
82 a.getColumnDimension());
83 }
84 if (b.getDimension() != a.getRowDimension()) {
85 throw new DimensionMismatchException(b.getDimension(),
86 a.getRowDimension());
87 }
88 if (x0.getDimension() != a.getColumnDimension()) {
89 throw new DimensionMismatchException(x0.getDimension(),
90 a.getColumnDimension());
91 }
92 }
93
94 /**
95 * Returns the iteration manager attached to this solver.
96 *
97 * @return the manager
98 */
99 public IterationManager getIterationManager() {
100 return manager;
101 }
102
103 /**
104 * Returns an estimate of the solution to the linear system A &middot; x =
105 * b.
106 *
107 * @param a the linear operator A of the system
108 * @param b the right-hand side vector
109 * @return a new vector containing the solution
110 * @throws NullArgumentException if one of the parameters is {@code null}
111 * @throws NonSquareOperatorException if {@code a} is not square
112 * @throws DimensionMismatchException if {@code b} has dimensions
113 * inconsistent with {@code a}
114 * @throws MaxCountExceededException at exhaustion of the iteration count,
115 * unless a custom
116 * {@link agents.anac.y2019.harddealer.math3.util.Incrementor.MaxCountExceededCallback callback}
117 * has been set at construction of the {@link IterationManager}
118 */
119 public RealVector solve(final RealLinearOperator a, final RealVector b)
120 throws NullArgumentException, NonSquareOperatorException,
121 DimensionMismatchException, MaxCountExceededException {
122 MathUtils.checkNotNull(a);
123 final RealVector x = new ArrayRealVector(a.getColumnDimension());
124 x.set(0.);
125 return solveInPlace(a, b, x);
126 }
127
128 /**
129 * Returns an estimate of the solution to the linear system A &middot; x =
130 * b.
131 *
132 * @param a the linear operator A of the system
133 * @param b the right-hand side vector
134 * @param x0 the initial guess of the solution
135 * @return a new vector containing the solution
136 * @throws NullArgumentException if one of the parameters is {@code null}
137 * @throws NonSquareOperatorException if {@code a} is not square
138 * @throws DimensionMismatchException if {@code b} or {@code x0} have
139 * dimensions inconsistent with {@code a}
140 * @throws MaxCountExceededException at exhaustion of the iteration count,
141 * unless a custom
142 * {@link agents.anac.y2019.harddealer.math3.util.Incrementor.MaxCountExceededCallback callback}
143 * has been set at construction of the {@link IterationManager}
144 */
145 public RealVector solve(RealLinearOperator a, RealVector b, RealVector x0)
146 throws NullArgumentException, NonSquareOperatorException,
147 DimensionMismatchException, MaxCountExceededException {
148 MathUtils.checkNotNull(x0);
149 return solveInPlace(a, b, x0.copy());
150 }
151
152 /**
153 * Returns an estimate of the solution to the linear system A &middot; x =
154 * b. The solution is computed in-place (initial guess is modified).
155 *
156 * @param a the linear operator A of the system
157 * @param b the right-hand side vector
158 * @param x0 initial guess of the solution
159 * @return a reference to {@code x0} (shallow copy) updated with the
160 * solution
161 * @throws NullArgumentException if one of the parameters is {@code null}
162 * @throws NonSquareOperatorException if {@code a} is not square
163 * @throws DimensionMismatchException if {@code b} or {@code x0} have
164 * dimensions inconsistent with {@code a}
165 * @throws MaxCountExceededException at exhaustion of the iteration count,
166 * unless a custom
167 * {@link agents.anac.y2019.harddealer.math3.util.Incrementor.MaxCountExceededCallback callback}
168 * has been set at construction of the {@link IterationManager}
169 */
170 public abstract RealVector solveInPlace(RealLinearOperator a, RealVector b,
171 RealVector x0) throws NullArgumentException, NonSquareOperatorException,
172 DimensionMismatchException, MaxCountExceededException;
173}
Note: See TracBrowser for help on using the repository browser.