DoubleDeadZone.java

/*
 * $Id: DeadZone.java,v 1.18 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.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.18 $
 */
public class DoubleDeadZone extends DoubleBaseContinuousStaticSystem implements DoublePiecewiseContinuousAlgebraicSystem, StringExternalizable {

  /** 不感帯の下限 */
  @Parameter(name = "lowerLimit", description = "DeadZone.1", internationalization = true)
  private double lowerLimit;

  /** 不感帯の上限 */
  @Parameter(name = "upperLimit", description = "DeadZone.3", internationalization = true)
  private double upperLimit;

  /**
   * 新しく生成された<code>DeadZone</code>オブジェクトを初期化します。
   */
  public DoubleDeadZone() {
    this(-0.5, 0.5);
  }

  /**
   * 新しく生成された<code>Saturation</code>オブジェクトを初期化します。
   * 
   * @param lowerLimit 不感帯の下限
   * @param upperLimit 不感帯の上限
   */
  public DoubleDeadZone(final double lowerLimit, final double upperLimit) {
    super(-1, -1);
    setAutoSize(true);
    setHasDirectFeedthrough(true);
    setLowerLimit(lowerLimit);
    setUpperLimit(upperLimit);
  }

  /**
   * {@inheritDoc}
   */
  @Override
  public DoubleMatrix outputEquation( final double t, final DoubleMatrix u) {
    final int size = u.getRowSize();
    final DoubleMatrix y = new DoubleMatrix(size, 1);

    for (int i = 1; i <= size; i++) {
      final double uu = u.getDoubleElement(i, 1);
      if (uu < this.lowerLimit) {
        y.setElement(i, 1, uu - this.lowerLimit);
      } else if (this.upperLimit < uu) {
        y.setElement(i, 1, uu - this.upperLimit);
      } else {
        y.setElement(i, 1, 0);
      }
    }

    return y;
  }

  /**
   * 不感帯の下限を設定します。
   * 
   * @param lowerLimit 不感帯の下限
   */
  public void setLowerLimit(final double lowerLimit) {
    this.lowerLimit = lowerLimit;
  }

  /**
   * 不感帯の上限を設定します。
   * 
   * @param upperLimit 不感帯の上限
   */
  public void setUpperLimit(final double upperLimit) {
    this.upperLimit = upperLimit;
  }

  /**
   * 不感帯の下限を返します。
   * 
   * @return 不感帯の下限
   */
  public double getLowerLimit() {
    return this.lowerLimit;
  }

  /**
   * 不感帯の上限を返します。
   * 
   * @return 不感帯の上限
   */
  public double getUpperLimit() {
    return this.upperLimit;
  }

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

    for (int i = 1; i <= size; i++) {
      final double uu = u.getDoubleElement(i);
      if (uu < this.lowerLimit) {
        pieces.add(0);
      } else if (this.upperLimit < uu) {
        pieces.add(2);
      } else {
        pieces.add(1);
      }
    }

    return pieces;
  }

  /**
   * {@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 int piece1 = pieces1.get(number - 1);
    final int piece2 = pieces2.get(number - 1);

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

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

    if (fromZeroToOne || fromOneToZero) {
      return t1 + (this.lowerLimit - uu1) / (uu2 - uu1) * (t2 - t1);
    }

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

    if (fromOneToTwo || fromTwoToOne) {
      return t1 + (this.upperLimit - uu1) / (uu2 - uu1) * (t2 - t1);
    }

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

    return Double.NaN;
  }

  /**
   * @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;
    }
    DoubleDeadZone castedObj = (DoubleDeadZone)o;
    return ((this.lowerLimit == castedObj.lowerLimit) && (this.upperLimit == castedObj.upperLimit));
  }

  /**
   * @see org.mklab.tool.control.system.SystemOperator#hashCode()
   */
  @Override
  public int hashCode() {
    int hashCode = super.hashCode();
    hashCode = 31 * hashCode + (int)(Double.doubleToLongBits(this.lowerLimit) ^ (Double.doubleToLongBits(this.lowerLimit) >>> 32));
    hashCode = 31 * hashCode + (int)(Double.doubleToLongBits(this.upperLimit) ^ (Double.doubleToLongBits(this.upperLimit) >>> 32));
    return hashCode;
  }
}