//
// Math.NET Numerics, part of the Math.NET Project
// http://numerics.mathdotnet.com
// http://github.com/mathnet/mathnet-numerics
// http://mathnetnumerics.codeplex.com
//
// Copyright (c) 2009-2013 Math.NET
//
// Permission is hereby granted, free of charge, to any person
// obtaining a copy of this software and associated documentation
// files (the "Software"), to deal in the Software without
// restriction, including without limitation the rights to use,
// copy, modify, merge, publish, distribute, sublicense, and/or sell
// copies of the Software, and to permit persons to whom the
// Software is furnished to do so, subject to the following
// conditions:
//
// The above copyright notice and this permission notice shall be
// included in all copies or substantial portions of the Software.
//
// THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
// EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES
// OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
// NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT
// HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY,
// WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
// FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR
// OTHER DEALINGS IN THE SOFTWARE.
//
using System;
using System.Collections.Generic;
using System.Linq;
using MathNet.Numerics.Properties;
namespace MathNet.Numerics.LinearAlgebra.Solvers
{
///
/// An iterator that is used to check if an iterative calculation should continue or stop.
///
public sealed class Iterator where T : struct, IEquatable, IFormattable
{
///
/// The collection that holds all the stop criteria and the flag indicating if they should be added
/// to the child iterators.
///
readonly List> _stopCriteria;
///
/// The status of the iterator.
///
IterationStatus _status = IterationStatus.Indetermined;
///
/// Initializes a new instance of the class with the specified stop criteria.
///
///
/// The specified stop criteria. Only one stop criterium of each type can be passed in. None
/// of the stop criteria will be passed on to child iterators.
///
public Iterator(params IIterationStopCriterium[] stopCriteria)
{
_stopCriteria = new List>(stopCriteria);
}
///
/// Initializes a new instance of the class with the specified stop criteria.
///
///
/// The specified stop criteria. Only one stop criterium of each type can be passed in. None
/// of the stop criteria will be passed on to child iterators.
///
public Iterator(IEnumerable> stopCriteria)
{
_stopCriteria = new List>(stopCriteria);
}
///
/// Gets the current calculation status.
///
public IterationStatus Status
{
get { return _status; }
}
///
/// True if the calculation has converged to the desired convergence levels.
///
public bool HasConverged
{
get { return _status == IterationStatus.Converged; }
}
///
/// True if the calculation has been stopped due to reaching the stopping limits but that convergence was not achieved.
///
public bool HasStoppedWithoutConvergence
{
get { return _status == IterationStatus.StoppedWithoutConvergence; }
}
///
/// Determines the status of the iterative calculation based on the stop criteria stored
/// by the current . Result is set into Status field.
///
/// The number of iterations that have passed so far.
/// The vector containing the current solution values.
/// The right hand side vector.
/// The vector containing the current residual vectors.
///
/// The individual iterators may internally track the progress of the calculation based
/// on the invocation of this method. Therefore this method should only be called if the
/// calculation has moved forwards at least one step.
///
public IterationStatus DetermineStatus(int iterationNumber, Vector solutionVector, Vector sourceVector, Vector residualVector)
{
if (_stopCriteria.Count == 0)
{
throw new ArgumentException(Resources.StopCriteriumMissing);
}
if (iterationNumber < 0)
{
throw new ArgumentOutOfRangeException("iterationNumber");
}
// While we're cancelled we don't call on the stop-criteria.
if (_status == IterationStatus.Cancelled)
{
return _status;
}
foreach (var stopCriterium in _stopCriteria)
{
var status = stopCriterium.DetermineStatus(iterationNumber, solutionVector, sourceVector, residualVector);
// Check if the status is:
// - Running --> keep going
// - Indetermined --> keep going
// Anything else:
// Stop looping and set that status
if ((status == IterationStatus.Running) || (status == IterationStatus.Indetermined))
{
continue;
}
_status = status;
return _status;
}
// Got all the way through
// So we're running because we had vectors passed to us.
_status = IterationStatus.Running;
return _status;
}
///
/// Indicates to the iterator that the iterative process has been cancelled.
///
///
/// Does not reset the stop-criteria.
///
public void Cancel()
{
_status = IterationStatus.Cancelled;
}
///
/// Resets the to the pre-calculation state.
///
public void Reset()
{
_status = IterationStatus.Indetermined;
foreach (var stopCriterium in _stopCriteria)
{
stopCriterium.Reset();
}
}
///
/// Creates a deep clone of the current iterator.
///
/// The deep clone of the current iterator.
public Iterator Clone()
{
return new Iterator(_stopCriteria.Select(sc => sc.Clone()));
}
}
}