DoubleBacklash.java

/*
 * $Id: Backlash.java,v 1.16 2008/07/15 15:27:15 koga Exp $
 *
 * Copyright (C) 2004 Masanobu Koga. All rights reserved.
 */
package org.mklab.tool.control.system.discontinuous;

import java.util.ArrayList;
import java.util.List;

import org.mklab.nfc.matrix.DoubleMatrix;
import org.mklab.nfc.ode.DoublePiecewiseContinuousAlgebraicSystem;
import org.mklab.nfc.ode.EquationSolver;
import org.mklab.nfc.ode.PiecewiseUtil;
import org.mklab.tool.control.system.continuous.DoubleBaseContinuousStaticSystem;
import org.mklab.tool.control.system.parameter.Parameter;
import org.mklab.tool.control.system.parameter.StringExternalizable;


/**
 * バックラッシュ(遊びのある)システムを表すクラスです。
 * 
 * @author koga
 * @version $Revision: 1.16 $
 */
public class DoubleBacklash extends DoubleBaseContinuousStaticSystem implements DoublePiecewiseContinuousAlgebraicSystem, StringExternalizable {

  /** 遮断帯域の幅 */
  @Parameter(name = "deadBandWidth", description = "Backlash.1", internationalization = true)
  private double deadBandWidth;

  /** 初期出力 */
  @Parameter(name = "initialOutput", description = "Backlash.3", internationalization = true)
  private DoubleMatrix initialOutput = new DoubleMatrix(1, 1);

  /** 初期状態ならばtrue */
  private boolean initialState = true;

  /** 1刻み前の時刻 */
  private double previousOutputTime;

  /** 1刻み前の入力 */
  private DoubleMatrix previousInput;

  /** 1刻み前の変化率 */
  private DoubleMatrix previousRate;

  /** 1刻み前の出力 */
  private DoubleMatrix previousOutput;

  /** 保持された出力 */
  private DoubleMatrix holdOutput;

  /** 出力が保持されたときの入力の変化率の符号 */
  private int holdRateSign = 0;

  /**
   * 新しく生成された<code>Backlash</code>オブジェクトを初期化します。
   */
  public DoubleBacklash() {
    this(1, new DoubleMatrix(1, 1));
  }

  /**
   * 新しく生成された<code>Saturation</code>オブジェクトを初期化します。
   * 
   * @param rateLowerBound 出力の最小値
   */
  public DoubleBacklash(final double rateLowerBound) {
    this(rateLowerBound, new DoubleMatrix(1, 1));
  }

  /**
   * 新しく生成された<code>Saturation</code>オブジェクトを初期化します。
   * 
   * @param rateLowerBound 出力の最小値
   * @param initialOutput 初期出力
   */
  public DoubleBacklash(final double rateLowerBound, final DoubleMatrix initialOutput) {
    super(0, 0);
    setAutoSize(true);
    setHasDirectFeedthrough(true);
    setDeadBandWidth(rateLowerBound);
    this.initialOutput = initialOutput.createClone();
    initialize();
  }

  /**
   * 初期出力を設定します。
   * 
   * @param initialOutput 初期出力
   */
  public void setInitialOutput(final DoubleMatrix initialOutput) {
    this.initialOutput = initialOutput.createClone();
    final int size = initialOutput.getRowSize();
    setInputSize(size);
    setOutputSize(size);
  }

  /**
   * @see org.mklab.tool.control.system.continuous.BaseContinuousStaticSystem#initialize()
   */
  @Override
  public void initialize() {
    this.previousInput = this.initialOutput.createClone();
    this.previousOutput = this.initialOutput.createClone();
    this.holdOutput = this.initialOutput.createClone();
    this.previousRate = this.initialOutput.createZero();
    this.previousOutputTime = 0;
    this.initialState = true;

    final int size = this.initialOutput.getRowSize();

    setInputSize(size);
    setOutputSize(size);
  }

  /**
   * {@inheritDoc}
   */
  @Override
  public DoubleMatrix outputEquation(final double t, final DoubleMatrix u) {
    if (t == this.previousOutputTime) {
      return this.previousOutput.createClone();
    }

    final double pTime = this.previousOutputTime;
    final DoubleMatrix pInput = this.previousInput;
    final DoubleMatrix inputRate = u.subtract(pInput).divide(t - pTime);

    if (EquationSolver.isTrial() == false) {
      this.previousOutputTime = t;
      this.previousInput = u.createClone();
      this.previousRate = inputRate.createClone();

      if (this.deadBandWidth / 2 <= u.absElementWise().max().doubleValue()) {
        this.initialState = false;
      }
    }

    if (this.initialState) {
      this.previousOutput = this.initialOutput.createClone();
      return this.previousOutput;
    }

    final int size = u.getRowSize();
    this.previousOutput = new DoubleMatrix(size, 1);

    for (int i = 1; i <= size; i++) {
      final double uc = u.getDoubleElement(i, 1);
      final double rate = inputRate.getDoubleElement(i, 1);
      final double hoc = this.holdOutput.getDoubleElement(i, 1);

      if (0 <= rate) {
        if (this.holdRateSign < 0 && uc - hoc < this.deadBandWidth / 2) {
          this.previousOutput.setElement(i, 1, hoc);
        } else {
          this.previousOutput.setElement(i, 1, uc - this.deadBandWidth / 2);
          if (EquationSolver.isTrial() == false) {
            this.holdOutput = this.previousOutput.createClone();
            this.holdRateSign = 1;
          }
        }
      } else {
        if (this.holdRateSign > 0 && hoc - uc < this.deadBandWidth / 2) {
          this.previousOutput.setElement(i, 1, hoc);
        } else {
          this.previousOutput.setElement(i, 1, uc + this.deadBandWidth / 2);
          if (EquationSolver.isTrial() == false) {
            this.holdOutput = this.previousOutput.createClone();
            this.holdRateSign = -1;
          }
        }
      }
    }

    return this.previousOutput;
  }

  /**
   * 遮断帯域の幅を設定します。
   * 
   * @param deadBandWidth 遮断帯域の幅
   */
  public void setDeadBandWidth(final double deadBandWidth) {
    this.deadBandWidth = deadBandWidth;
  }

  /**
   * 遮断帯域の幅を返します。
   * 
   * @return 遮断帯域の幅
   */
  public double getDeadBandWidth() {
    return this.deadBandWidth;
  }

  /**
   * {@inheritDoc}
   */
  @SuppressWarnings("boxing")
  public List<Integer> getPiece(final double t, final DoubleMatrix u) {
    final int size = u.getRowSize();
    final List<Integer> piece = new ArrayList<>(size);

    final double pTime = this.previousOutputTime;
    final DoubleMatrix pInput = this.previousInput;
    final DoubleMatrix inputRate = (t == pTime ? this.previousRate : u.subtract(pInput).divide(t - pTime));

    for (int i = 1; i <= size; i++) {
      final double uu = u.getDoubleElement(i, 1);
      final double hh = this.holdOutput.getDoubleElement(i, 1);
      final double rate = inputRate.getDoubleElement(i, 1);

      if (this.initialState && u.absElementWise().max().doubleValue() < this.deadBandWidth / 2) {
        piece.add(0);
      } else if (0 < rate) {
        if (this.holdRateSign < 0 && uu - (hh - this.deadBandWidth / 2) < this.deadBandWidth / 2) {
          piece.add(4);
        } else {
          piece.add(1);
        }
      } else {
        if (this.holdRateSign > 0 && (hh + this.deadBandWidth / 2) - uu < this.deadBandWidth / 2) {
          piece.add(2);
        } else {
          piece.add(3);
        }
      }
    }

    return piece;
  }

  /**
   * {@inheritDoc}
   */
  @SuppressWarnings("boxing")
  public double getDiscontinuousPoint(final double t1, final DoubleMatrix u1, final double t2, final DoubleMatrix u2) {
    final List<Integer> pieces1 = getPiece(t1, u1);
    final List<Integer> pieces2 = getPiece(t2, u2);

    if (pieces1.equals(pieces2)) {
      return Double.NaN;
    }

    final int number = PiecewiseUtil.getDistinctPiece(pieces1, pieces2);

    final double uu1 = u1.getDoubleElement(number, 1);
    final double uu2 = u2.getDoubleElement(number, 1);

    final int piece1 = pieces1.get(number - 1);
    final int piece2 = pieces2.get(number - 1);

    final boolean fromZeroToOne = piece1 == 0 && piece2 == 1;

    if (fromZeroToOne) {
      return t1 + (this.deadBandWidth / 2 - uu1) / (uu2 - uu1) * (t2 - t1);
    }

    final boolean fromOneToTwo = piece1 == 1 && piece2 == 2;

    if (fromOneToTwo) {
      return t1 + (t2 - t1) / 2;
    }

    final boolean fromTwoToThree = piece1 == 2 && piece2 == 3;

    if (fromTwoToThree) {
      return t1 + (this.deadBandWidth / 2 - (uu1 - uu2)) / (this.deadBandWidth / 2) * (t2 - t1);
    }

    final boolean fromThreeToFour = piece1 == 3 && piece2 == 4;

    if (fromThreeToFour) {
      return t1 + (t2 - t1) / 2;
    }

    final boolean fromFourToOne = piece1 == 4 && piece2 == 1;

    if (fromFourToOne) {
      return t1 + (this.deadBandWidth / 2 - (uu2 - uu1)) / (this.deadBandWidth / 2) * (t2 - t1);
    }

    assert false : "never reached"; //$NON-NLS-1$

    return (t1 + t2) / 2;
  }

  /**
   * @see org.mklab.tool.control.system.SystemOperator#setInputSize(int)
   */
  @Override
  public void setInputSize(final int size) {
    super.setInputSize(size);
    super.setOutputSize(size);
  }

  /**
   * @see org.mklab.tool.control.system.SystemOperator#setOutputSize(int)
   */
  @Override
  public void setOutputSize(final int size) {
    super.setInputSize(size);
    super.setOutputSize(size);
  }

  /**
   * @see org.mklab.tool.control.system.parameter.StringExternalizable#getString(java.lang.String)
   */
  public String getString(String key) {
    return Messages.getString(key);
  }

  /**
   * @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;
    }
    DoubleBacklash castedObj = (DoubleBacklash)o;
    return ((this.deadBandWidth == castedObj.deadBandWidth) && (this.initialOutput == null ? castedObj.initialOutput == null : this.initialOutput.equals(castedObj.initialOutput))
        && (this.initialState == castedObj.initialState) && (this.previousOutputTime == castedObj.previousOutputTime)
        && (this.previousInput == null ? castedObj.previousInput == null : this.previousInput.equals(castedObj.previousInput))
        && (this.previousRate == null ? castedObj.previousRate == null : this.previousRate.equals(castedObj.previousRate))
        && (this.previousOutput == null ? castedObj.previousOutput == null : this.previousOutput.equals(castedObj.previousOutput))
        && (this.holdOutput == null ? castedObj.holdOutput == null : this.holdOutput.equals(castedObj.holdOutput)) && (this.holdRateSign == castedObj.holdRateSign));
  }

  /**
   * @see org.mklab.tool.control.system.SystemOperator#hashCode()
   */
  @Override
  public int hashCode() {
    int hashCode = super.hashCode();
    hashCode = 31 * hashCode + (int)(Double.doubleToLongBits(this.deadBandWidth) ^ (Double.doubleToLongBits(this.deadBandWidth) >>> 32));
    hashCode = 31 * hashCode + (this.initialOutput == null ? 0 : this.initialOutput.hashCode());
    hashCode = 31 * hashCode + (this.initialState ? 1231 : 1237);
    hashCode = 31 * hashCode + (int)(Double.doubleToLongBits(this.previousOutputTime) ^ (Double.doubleToLongBits(this.previousOutputTime) >>> 32));
    hashCode = 31 * hashCode + (this.previousInput == null ? 0 : this.previousInput.hashCode());
    hashCode = 31 * hashCode + (this.previousRate == null ? 0 : this.previousRate.hashCode());
    hashCode = 31 * hashCode + (this.previousOutput == null ? 0 : this.previousOutput.hashCode());
    hashCode = 31 * hashCode + (this.holdOutput == null ? 0 : this.holdOutput.hashCode());
    hashCode = 31 * hashCode + this.holdRateSign;
    return hashCode;
  }
}