DoubleBaseDiscreteDynamicSystem.java

/*
 * $Id: BaseDiscreteDynamicSystem.java,v 1.16 2008/06/30 08:29:30 koga Exp $
 *
 * Copyright (C) 2004 Koga Laboratory. All rights reserved.
 *
 */

package org.mklab.tool.control.system.discrete;

import org.mklab.nfc.matrix.DoubleMatrix;
import org.mklab.nfc.ode.SolverStopException;
import org.mklab.tool.control.system.DoubleSystemOperator;
import org.mklab.tool.control.system.sampled.DoubleSampler;


/**
 * 差分方程式で表現される離散時間動的システムを表わすクラスです。
 * 
 * @author Koga Laboratory
 * @version $Revision: 1.16 $, 2004/11/09
 */
public abstract class DoubleBaseDiscreteDynamicSystem extends DoubleSystemOperator implements DoubleDiscreteDynamicSystem, DoubleSampler {

  /** 初期状態 */
  private DoubleMatrix initialState;
  /** 状態 */
  private DoubleMatrix state;

  /** 前サンプリング点での出力 */
  private DoubleMatrix previousOutput;

  /** 結合システムにおける状態の順番 */
  private int stateNumber;

  /** サンプリング周期 */
  private double samplingInterval;
  /** サンプリング点ならばtrue */
  private boolean atSamplingPoint;

  /**
   * 新しく生成された<code>BaseDiscreteDynamicSystem</code>オブジェクトを初期化します。
   * 
   * @param inputSize 入力の数
   * @param outputSize 出力の数
   * @param stateSize 状態の数
   */
  public DoubleBaseDiscreteDynamicSystem(final int inputSize, final int outputSize, final int stateSize) {
    super();
    setInputSize(inputSize);
    setOutputSize(outputSize);
    setStateSize(stateSize);
    setDynamic(true);
    if (inputSize == 0) {
      setForcedSystem(false);
    }

    this.initialState = new DoubleMatrix(stateSize, 1);
    this.state = this.initialState.createClone();
  }

  /**
   * @see org.mklab.tool.control.system.SystemOperator#initialize()
   */
  @Override
  public void initialize() {
    setState(getInitialState());
    this.atSamplingPoint = false;
  }

  /**
   * 初期状態を設定します。
   * 
   * @param initialState 初期状態
   */
  public void setInitialState(final DoubleMatrix initialState) {
    this.initialState = initialState;
  }

  /**
   * 初期状態を返します。
   * 
   * @return 初期状態
   */
  public DoubleMatrix getInitialState() {
    return this.initialState.createClone();
  }

  /**
   * 現在の状態を返します。
   * 
   * @return 現在の状態
   */
  public DoubleMatrix getState() {
    return this.state.createClone();
  }

  /**
   * 現在の状態の設定します。
   * 
   * @param state 現在の状態
   */
  public void setState(final DoubleMatrix state) {
    this.state = state;
  }

  /**
   * {@inheritDoc}
   */
  public DoubleMatrix differenceEquation(final int k, final DoubleMatrix x, final DoubleMatrix inputOutput) throws SolverStopException {
    final DoubleMatrix u = inputOutput.getRowVectors(1, getInputSize());
    return stateEquation(k, x, u);
  }

  /**
   * {@inheritDoc}
   */
  public DoubleMatrix differenceEquation(final double t, final DoubleMatrix x, final DoubleMatrix inputOutput) throws SolverStopException {
    final int k = (int)Math.floor(t / getSamplingInterval());
    return differenceEquation(k, x, inputOutput);
  }

  /**
   * {@inheritDoc}
   */
  public DoubleMatrix inputOutputEquation(final int k, final DoubleMatrix x) throws SolverStopException {
    if (hasDirectFeedthrough()) {
      throw new RuntimeException(Messages.getString("BaseDiscreteDynamicSystem.0")); //$NON-NLS-1$
    }

    final DoubleMatrix u = new DoubleMatrix(getInputSize(), 1);
    final DoubleMatrix y = outputEquation(k, x);
    return u.appendDown(y);
  }

  /**
   * {@inheritDoc}
   */
  public DoubleMatrix inputOutputEquation(final double t, final DoubleMatrix x) throws SolverStopException {
    if (hasDirectFeedthrough()) {
      throw new RuntimeException(Messages.getString("BaseDiscreteDynamicSystem.1")); //$NON-NLS-1$
    }

    return outputEquation(t, x);
  }

  /**
   * {@inheritDoc}
   */
  public DoubleMatrix outputEquation( final int k,  final DoubleMatrix x,  final DoubleMatrix u) throws SolverStopException {
    throw new SolverStopException(Messages.getString("BaseDiscreteDynamicSystem.2")); //$NON-NLS-1$
  }

  /**
   * {@inheritDoc}
   */
  public DoubleMatrix outputEquation(final double t, final DoubleMatrix x, final DoubleMatrix u) throws SolverStopException {
    int k = (int)Math.ceil(t / this.samplingInterval);

    if (!isAtSamplingPoint() && this.previousOutput != null) {
      return this.previousOutput.createClone();
    }

    if (!isAtSamplingPoint() && (t - k * this.samplingInterval < 0)) {
      k = k - 1;
    }

    this.previousOutput = outputEquation(k, x, u);

    return this.previousOutput.createClone();
  }

  /**
   * {@inheritDoc}
   */
  public DoubleMatrix outputEquation( final int k,  final DoubleMatrix x) throws SolverStopException {
    throw new SolverStopException(Messages.getString("BaseDiscreteDynamicSystem.3")); //$NON-NLS-1$
  }

  /**
   * {@inheritDoc}
   */
  public DoubleMatrix outputEquation(final double t, final DoubleMatrix x) throws SolverStopException {
    int k = (int)Math.ceil(t / this.samplingInterval);

    if (!isAtSamplingPoint() && this.previousOutput != null) {
      return this.previousOutput.createClone();
    }

    if (!isAtSamplingPoint() && (t - k * this.samplingInterval < 0)) {
      k = k - 1;
    }

    this.previousOutput = outputEquation(k, x);

    return this.previousOutput.createClone();
  }

  /**
   * @see org.mklab.tool.control.system.sampled.Sampler#getSamplingInterval()
   */
  public double getSamplingInterval() {
    return this.samplingInterval;
  }

  /**
   * {@inheritDoc}
   */
  public void setSamplingInterval(final double interval) {
    this.samplingInterval = interval;
  }

  /**
   * @see org.mklab.nfc.ode.Sampling#isAtSamplingPoint()
   */
  public boolean isAtSamplingPoint() {
    return this.atSamplingPoint;
  }

  /**
   * @see org.mklab.nfc.ode.Sampling#setAtSamplingPoint(boolean)
   */
  public void setAtSamplingPoint(final boolean samplingPoint) {
    this.atSamplingPoint = samplingPoint;
  }

  /**
   * {@inheritDoc}
   */
  public double getNextSamplingTime(final double t, final double tolerance) {
    double nextSamplingTime = Math.ceil(t / this.samplingInterval) * this.samplingInterval;
    if (Math.abs(t - nextSamplingTime) < tolerance) {
      nextSamplingTime = t + this.samplingInterval;
    }

    return nextSamplingTime;
  }

  /**
   * {@inheritDoc}
   */
  public DoubleMatrix stateEquation(final double t, final DoubleMatrix xd, final DoubleMatrix inputOutput) throws SolverStopException {
    final int k = (int)Math.floor(t / getSamplingInterval());
    return stateEquation(k, xd, inputOutput);
  }

  /**
   * @see org.mklab.tool.control.system.SystemOperator#hashCode()
   */
  @Override
  public int hashCode() {
    int hashCode = super.hashCode();
    hashCode = 31 * hashCode + (this.initialState == null ? 0 : this.initialState.hashCode());
    hashCode = 31 * hashCode + (this.state == null ? 0 : this.state.hashCode());
    hashCode = 31 * hashCode + (int)(Double.doubleToLongBits(this.samplingInterval) ^ (Double.doubleToLongBits(this.samplingInterval) >>> 32));
    hashCode = 31 * hashCode + (this.atSamplingPoint ? 1231 : 1237);
    return hashCode;
  }

  /**
   * @see org.mklab.tool.control.system.SystemOperator#equals(java.lang.Object)
   */
  @Override
  public boolean equals(Object o) {
    if (this == o) {
      return true;
    }
    if (!super.equals(o)) {
      return false;
    }
    if (o == null) {
      return false;
    }
    if (o.getClass() != getClass()) {
      return false;
    }
    DoubleBaseDiscreteDynamicSystem castedObj = (DoubleBaseDiscreteDynamicSystem)o;
    return ((this.initialState == null ? castedObj.initialState == null : this.initialState.equals(castedObj.initialState))
        && (this.state == null ? castedObj.state == null : this.state.equals(castedObj.state)) && (this.samplingInterval == castedObj.samplingInterval) && (this.atSamplingPoint == castedObj.atSamplingPoint));
  }

  /**
   * @see org.mklab.tool.control.system.DynamicSystem#getStateNumber()
   */
  public int getStateNumber() {
    return this.stateNumber;
  }

  /**
   * @see org.mklab.tool.control.system.DynamicSystem#setStateNumber(int)
   */
  public void setStateNumber(final int stateNumber) {
    this.stateNumber = stateNumber;
  }
}