RateLimiter.java

/*
 * $Id: RateLimiter.java,v 1.17 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.ComplexNumericalMatrix;
import org.mklab.nfc.matrix.RealNumericalMatrix;
import org.mklab.nfc.ode.EquationSolver;
import org.mklab.nfc.ode.PiecewiseContinuousAlgebraicSystem;
import org.mklab.nfc.ode.PiecewiseUtil;
import org.mklab.nfc.scalar.ComplexNumericalScalar;
import org.mklab.nfc.scalar.RealNumericalScalar;
import org.mklab.tool.control.system.continuous.BaseContinuousStaticSystem;
import org.mklab.tool.control.system.parameter.Parameter;
import org.mklab.tool.control.system.parameter.StringExternalizable;


/**
 * 信号の変化率制限システムを表すクラスです。
 * 
 * @author koga
 * @version $Revision: 1.17 $
  * @param <RS> type of real scalar
 * @param <RM> type of real matrix
 * @param <CS> type of complex scalar
 * @param <CM> type of complex matrix
*/
public class RateLimiter<RS extends RealNumericalScalar<RS, RM, CS, CM>, RM extends RealNumericalMatrix<RS, RM, CS, CM>, CS extends ComplexNumericalScalar<RS, RM, CS, CM>, CM extends ComplexNumericalMatrix<RS, RM, CS, CM>> extends BaseContinuousStaticSystem<RS,RM,CS,CM> implements PiecewiseContinuousAlgebraicSystem<RS,RM,CS,CM>, StringExternalizable {

  /** 変化率の下限 */
  @Parameter(name = "rateLowerBound", description = "RateLimiter.1", internationalization = true)
  private RS rateLowerBound;

  /** 変化率の上限 */
  @Parameter(name = "rateUpperBound", description = "RateLimiter.3", internationalization = true)
  private RS rateUpperBound;

  /** 初期出力 */
  @Parameter(name = "initialOutput", description = "RateLimiter.5", internationalization = true)
  private RM initialOutput = this.sunit.createZeroGrid(1, 1);

  /** 1刻み前の時刻 */
  private RS previousTime;

  /** 1呼び出し前の出力 */
  private RM previousOutput;

  /** 入力の変化率 */
  private RM inputRate;

  /**
   * コンストラクター
   * @param sunit unit of scalar
   */
  public RateLimiter(RS sunit) {
    this(sunit.create(-1), sunit.create(1), sunit);
  }

  /**
   * 新しく生成された<code>Saturation</code>オブジェクトを初期化します。
   * 
   * @param rateLowerBound 出力の最小値
   * @param rateUpperBound 出力の最大値
   * @param sunit unit of scalar
   */
  public RateLimiter(final RS rateLowerBound, final RS rateUpperBound, RS sunit) {
    super(-1, -1, sunit);
    setAutoSize(true);
    setHasDirectFeedthrough(true);
    setRateLowerBound(rateLowerBound);
    setRateUpperBound(rateUpperBound);

    initialize();
  }

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

  /**
   * @see org.mklab.tool.control.system.continuous.BaseContinuousStaticSystem#initialize()
   */
  @Override
  public void initialize() {
    this.previousOutput = this.initialOutput.createClone();
    this.previousTime = this.sunit.create(0);
    this.inputRate = this.sunit.createZeroGrid(this.initialOutput.getRowSize(), 1);
  }

  /**
   * {@inheritDoc}
   */
  @Override
  public RM outputEquation(final RS t, final RM u) {
    if (t == this.previousTime) {
      return this.previousOutput.createClone();
    }

    final int size = u.getRowSize();
    final RM output = this.sunit.createZeroGrid(size, 1);

    this.inputRate = u.subtract(this.previousOutput).divide(t.subtract(this.previousTime));

    for (int i = 1; i <= size; i++) {
      final RS uc = u.getElement(i, 1);
      final RS up = this.previousOutput.getElement(i, 1);
      final RS rate = this.inputRate.getElement(i, 1);

      if (this.rateUpperBound.isLessThan(rate)) {
        output.setElement(i, 1, up.add(this.rateUpperBound.multiply(t.subtract(this.previousTime))));
      } else if (rate.isLessThan(this.rateLowerBound)) {
        output.setElement(i, 1, up.add(this.rateLowerBound.multiply(t.subtract(this.previousTime))));
      } else {
        output.setElement(i, 1, uc);
      }
    }

    if (EquationSolver.isTrial() == false) {
      this.previousOutput = output.createClone();
      this.previousTime = t;
    }

    return output;
  }

  /**
   * 変化率の下限を設定します。
   * 
   * @param rateLowerBound 変化率の下限
   */
  public void setRateLowerBound(final RS rateLowerBound) {
    this.rateLowerBound = rateLowerBound;
  }

  /**
   * 変化率の上限を設定します。
   * 
   * @param rateUpperBound 変化率の上限
   */
  public void setRateUpperBound(final RS rateUpperBound) {
    this.rateUpperBound = rateUpperBound;
  }

  /**
   * 変化率の下限を返します。
   * 
   * @return 変化率の下限
   */
  public RS getRateLowerBound() {
    return this.rateLowerBound;
  }

  /**
   * 変化率の上限を返します。
   * 
   * @return 変化率の上限
   */
  public RS getRateUpperBound() {
    return this.rateUpperBound;
  }

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

    for (int i = 1; i <= size; i++) {
      final RS rate = this.inputRate.getElement(i, 1);

      if (rate.isLessThan(this.rateLowerBound)) {
        piece.add(0);
      } else if (rate.isLessThan( this.rateUpperBound)) {
        piece.add(1);
      } else {
        piece.add(2);
      }
    }

    return piece;
  }

  /**
   * {@inheritDoc}
   */
  @SuppressWarnings("boxing")
  public RS getDiscontinuousPoint(final RS t1, final RM u1, final RS t2, final RM u2) {
    final List<Integer> pieces1 = getPiece(t1, u1);
    final List<Integer> pieces2 = getPiece(t2, u2);
    if (pieces1.equals(pieces2)) {
      return this.sunit.getNaN();
    }

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

    final RS uu1 = u1.getElement(number, 1);
    final RS uu2 = u2.getElement(number, 1);
    final RS up = this.previousOutput.getElement(number, 1);

    final RS rate1 = uu1.subtract(up).divide(t1.subtract(this.previousTime));
    final RS rate2 = uu2.subtract(up).divide(t2.subtract(this.previousTime));

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

    if (fromOneToTwo || fromTwoToOne) {
      return t1.add(this.rateUpperBound.subtract(rate1).divide(rate2.subtract(rate1)).multiply(t2.subtract(t1)));
    }

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

    if (fromOneToZero || fromZeroToOne) {
      return t1.add(rate1.subtract(this.rateLowerBound).divide(rate1.subtract(rate2)).multiply( t2.subtract(t1)));
    }

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

    return t1.add(t2).divide(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;
    }
    RateLimiter<RS,RM,CS,CM> castedObj = (RateLimiter<RS,RM,CS,CM>)o;
    return ((this.rateLowerBound == castedObj.rateLowerBound) && (this.rateUpperBound == castedObj.rateUpperBound)
        && (this.initialOutput == null ? castedObj.initialOutput == null : this.initialOutput.equals(castedObj.initialOutput)) && (this.previousTime == castedObj.previousTime)
        && (this.previousOutput == null ? castedObj.previousOutput == null : this.previousOutput.equals(castedObj.previousOutput)) && (this.inputRate == null ? castedObj.inputRate == null
          : this.inputRate.equals(castedObj.inputRate)));
  }

  /**
   * @see org.mklab.tool.control.system.SystemOperator#hashCode()
   */
  @Override
  public int hashCode() {
    int hashCode = super.hashCode();
    hashCode = 31 * hashCode + (this.rateLowerBound.hashCode() ^ (this.rateLowerBound.hashCode() >>> 32));
    hashCode = 31 * hashCode + (this.rateUpperBound.hashCode() ^ (this.rateUpperBound.hashCode() >>> 32));
    hashCode = 31 * hashCode + (this.initialOutput == null ? 0 : this.initialOutput.hashCode());
    hashCode = 31 * hashCode + (this.previousTime.hashCode() ^ (this.previousTime.hashCode() >>> 32));
    hashCode = 31 * hashCode + (this.previousOutput == null ? 0 : this.previousOutput.hashCode());
    hashCode = 31 * hashCode + (this.inputRate == null ? 0 : this.inputRate.hashCode());
    return hashCode;
  }
}