All Downloads are FREE. Search and download functionalities are using the official Maven repository.

com.exigen.ie.constrainer.impl.IntExpAddArray Maven / Gradle / Ivy

package com.exigen.ie.constrainer.impl;
import java.util.Map;

import com.exigen.ie.constrainer.Constrainer;
import com.exigen.ie.constrainer.EventOfInterest;
import com.exigen.ie.constrainer.Failure;
import com.exigen.ie.constrainer.IntExp;
import com.exigen.ie.constrainer.IntExpArray;
import com.exigen.ie.constrainer.IntVar;
import com.exigen.ie.constrainer.NonLinearExpression;
import com.exigen.ie.constrainer.Observer;
import com.exigen.ie.constrainer.Subject;
/**
 * An implementation of the expression: sum(IntExpArray).
 */
public final class IntExpAddArray extends IntExpImpl
{
  private IntExpArray	       _vars;
  private Observer _observer;
  private IntVar _sum;

    static final private int[] event_map = {
        MIN,              MIN,
        MAX,              MAX,
        MIN| MAX | VALUE, VALUE,
                                         //         REMOVE, REMOVE
                                                 };


  class ExpAddVectorObserver extends Observer
  {

    ExpAddVectorObserver()
    {
//      super(event_map);
    }

    public int subscriberMask()
    {
      return MIN | MAX | VALUE;
    }

    public void update(Subject exp, EventOfInterest event)
      throws Failure
    {
      IntEvent e = (IntEvent) event;

  //    System.out.println("Event:" + e);

      _sum.setMin(_sum.min() + e.mindiff());
      _sum.setMax(_sum.max() + e.maxdiff());

    }


      public String toString()
      {
        return "ExpAddVectorObserver: "+_vars;
      }

      public Object master()
      {
        return IntExpAddArray.this;
      }


  } //~ ExpAddVectorObserver


  public IntExpAddArray(Constrainer constrainer, IntExpArray vars)
  {
    super(constrainer);
    int size = vars.size();
    _vars = vars;
    _observer = new ExpAddVectorObserver();

    IntExp[] data = _vars.data();

    for(int i=0; i < data.length; i++)
    {
      data[i].attachObserver(_observer);
    }

    String sum_name = "";

    if (constrainer().showInternalNames())
    {
      StringBuffer s = new StringBuffer();
      s.append("(");
      for(int i=0; i= max())
      return;

//    System.out.println("++++ Set max: " + max + " in " + this);

    int min_sum = min();


    IntExp[] vars = _vars.data();

    for(int i=0; i < vars.length; i++)
    {
      IntExp vari = (IntExp)vars[i];
      int maxi = max - (min_sum - vari.min());
      if (maxi < vari.max())
      {
        vari.setMax(maxi);
      }
    }
//    System.out.println("---- set max:" + max + " in " + this);
  }

  public void setMin(int min) throws Failure
  {

    if (min <= min())
      return;

//    System.out.println("++++ Set min: " + min + " in " + this);


    int max_sum = max();


    IntExp[] vars = _vars.data();

    for(int i=0; i < vars.length; i++)
    {
      IntExp vari = (IntExp)vars[i];
      int mini = min - (max_sum - vari.max());
      if (mini > vari.min())
      {
        vari.setMin(mini);
      }
    }
//    System.out.println("---- set min:" + min + " in " + this);
  }

  public void setValue(int value) throws Failure
  {
    int sum_min = min();
    int sum_max = max();

    if (value < sum_min || value > sum_max)
      _constrainer.fail("Add Array Set Value");


    if (value == sum_min )
    {
      setMax(value);
      return;
    }
    if (value == sum_max)
    {
      setMin(value);
      return;
    }

//    System.out.println("++++ Set value: " + value + " in " + this);

    IntExp[] vars = _vars.data();

    for(int i=0; i < vars.length; i++)
    {
      IntExp vari = (IntExp)vars[i];
      int mini = vari.min();
      int maxi = vari.max();

      int new_min = value - (sum_max - maxi);
      if (new_min > mini)
      {
        vari.setMin(new_min);
      }

      int new_max = value - (sum_min - mini);
      if (new_max < maxi)
      {
        vari.setMax(new_max);
      }
    }

//    System.out.println("---- set value: " + value + " in " + this);
  }

  public void removeValue(int value) throws Failure
  {
    int Max = max();
    if (value > Max)
      return;
    int Min = min();
    if (value < Min)
      return;
    if (Min == Max)
      constrainer().fail("remove for IntExpAddVector");
    if (value == Max)
      setMax(value-1);
    if (value == Min)
      setMin(value+1);
  }

  public int size()
  {
    return max() - min() + 1;
  }

  public boolean isLinear(){
    for (int i=0;i<_vars.size();i++){
      if (!_vars.get(i).isLinear())
        return false;
    }
    return true;
  }

  public double calcCoeffs(Map map, double factor) throws NonLinearExpression
  {
    if (!isLinear())
      throw new NonLinearExpression(this);
    double cumSum = 0;
    for (int i=0;i<_vars.size();i++){
      cumSum += _vars.get(i).calcCoeffs(map, factor);
    }
    return cumSum;
  }

} // ~IntExpAddArray




© 2015 - 2024 Weber Informatics LLC | Privacy Policy