Ejemplo n.º 1
0
  public FloatVar derivative(Store store, FloatVar f, java.util.Set<FloatVar> vars, FloatVar x) {
    if (f.equals(r)) {
      // f = p + c
      // f' = d(p)
      FloatVar v = Derivative.getDerivative(store, p, vars, x);
      return v;

    } else if (f.equals(p)) {
      // f = r - c
      // f' = d(r)
      FloatVar v = Derivative.getDerivative(store, r, vars, x);
      return v;
    }

    return null;
  }
  boolean contains(FloatVar[] fs, FloatVar r) {

    for (FloatVar f : fs) if (f.equals(r)) return true;

    return false;
  }
  double value(FloatVar f) {

    if (map.get(f) != null) return map.get(f);
    // else if (f.singleton())
    //     return f.value();

    Constraint c = constraint(f);
    if (c != null) eval.push(c);
    else if (f.singleton()) return f.value();

    // if (debug)
    //      System.out.println ("current constraint for variable " + f + " is " + c);

    double result = 0.0;

    if (c instanceof PmulQeqR) {
      if (f.equals(((PmulQeqR) c).r)) {
        result = value(((PmulQeqR) c).p) * value(((PmulQeqR) c).q);
      } else {
        System.out.println(
            "!!! Anable to compute middle value for "
                + f
                + "; + Constraint "
                + c
                + " does not define a function for variable\n");
        System.exit(0);
      }
    } else if (c instanceof PmulCeqR) {
      if (f.equals(((PmulCeqR) c).r)) result = value(((PmulCeqR) c).p) * ((PmulCeqR) c).c;
      else {
        System.out.println(
            "!!! Anable to compute middle value for "
                + f
                + "; + Constraint "
                + c
                + " does not define a function for variable\n");
        System.exit(0);
      }
    } else if (c instanceof PdivQeqR) {
      if (f.equals(((PdivQeqR) c).r)) {
        result = value(((PdivQeqR) c).p) / value(((PdivQeqR) c).q);
      } else {
        System.out.println(
            "!!! Anable to compute middle value for "
                + f
                + "; + Constraint "
                + c
                + " does not define a function for variable\n");
        System.exit(0);
      }
    } else if (c instanceof PplusQeqR) {
      if (f.equals(((PplusQeqR) c).r)) result = value(((PplusQeqR) c).p) + value(((PplusQeqR) c).q);
      else {
        System.out.println(
            "!!! Anable to compute middle value for "
                + f
                + "; + Constraint "
                + c
                + " does not define a function for variable\n");
        System.exit(0);
      }
    } else if (c instanceof PplusCeqR) {
      if (f.equals(((PplusCeqR) c).r)) result = value(((PplusCeqR) c).p) + ((PplusCeqR) c).c;
      else {
        System.out.println(
            "!!! Anable to compute middle value for "
                + f
                + "; + Constraint "
                + c
                + " does not define a function for variable\n");
        System.exit(0);
      }
    } else if (c instanceof PminusQeqR) {
      if (f.equals(((PminusQeqR) c).r))
        result = value(((PminusQeqR) c).p) - value(((PminusQeqR) c).q);
      else {
        System.out.println(
            "!!! Anable to compute middle value for "
                + f
                + "; + Constraint "
                + c
                + " does not define a function for variable\n");
        System.exit(0);
      }
    } else if (c instanceof LinearFloat) {

      FloatVar[] v = ((LinearFloat) c).list;
      double[] w = ((LinearFloat) c).weights;
      double sum = ((LinearFloat) c).sum;

      FloatVar vOut = null;
      double wOut = 1000.0;

      for (int i = 0; i < v.length; i++) {
        if (!v[i].equals(f)) sum -= value(v[i]) * w[i];
        else {
          vOut = v[i];
          wOut = w[i];
        }
      }

      if (vOut != null) result = sum / wOut;
      else {
        System.out.println(
            "!!! Anable to compute middle value for "
                + f
                + "; + Constraint "
                + c
                + " does not define a function for variable\n");
        System.exit(0);
      }
    } else {
      System.out.println("!!! Constraint " + c + " is not yet supported in Newtoen method\n");
      System.exit(0);
    }

    eval.pop();

    // if (debug)
    //     System.out.println ("returns " + result);

    return result;
  }