Package de.mpi.rgblab.optimizer

Source Code of de.mpi.rgblab.optimizer.Optimizer$Dif

package de.mpi.rgblab.optimizer;

import java.util.Iterator;
import java.util.LinkedList;
import java.util.Vector;

import de.mpi.rgblab.color.ColorModel;
import de.mpi.rgblab.color.ColorType;
import de.mpi.rgblab.data.Data;
import de.mpi.rgblab.utils.LAB;
import de.mpi.rgblab.utils.LogUtil;
import de.mpi.rgblab.utils.VisUtil;

public class Optimizer {

  private LogUtil logger; // = new LogUtil("log/opt.dat", true);
  // stopping criterea
  private LinkedList improvement = new LinkedList();
  // private Vector<Double> sumImprovements = new Vector<Double>();

  private double sumImprovements = 0;
  // number of the last steps of the algorithm taken into considaration
  private int numberImprovements;
  // threshold for the stopping criterium;
  private double threshold;

  // int number_all_elements =0;

  public Optimizer() {
    logger = new LogUtil(
        "log/opt_in_co_cmpOverallVis_weighting4_huePreservingFixed.dat",
        true);
    numberImprovements = 6;
    threshold = 40;
  }

  public Optimizer(String logFile) {
    logger = new LogUtil(logFile, true);
    numberImprovements = 6;
    threshold = 40;
  }
 
  public Optimizer(String logFile, double threshold, int numberImprovements) {
    logger = new LogUtil(logFile, true);
    this.numberImprovements = numberImprovements;
    this.threshold = threshold;
  }

  private class Dif {
    double dif_l;
    double dif_a;
    double dif_b;

    int num;

    // int num_a;
    // int num_b;

    public Dif() {
      dif_l = 0;
      dif_a = 0;
      dif_b = 0;

    }

    double meanL() {
      return dif_l / num;
    }

    double meanA() {
      return dif_a / num;
    }

    double meanB() {
      return dif_b / num;
    }

    public void update(double l, double a, double b) {
      updateL(l);
      updateA(a);
      updateB(b);
      num++;
    }

    private void updateL(double l) {
      dif_l += l;
    }

    private void updateA(double a) {
      dif_a += a;
    }

    private void updateB(double b) {
      dif_b += b;
    }

    public int getNum() {
      return num;
    }
  }

  // Vector<ColorModel> rgbColors;
  // Vector<ColorModel> labColors = new Vector<ColorModel>();

  // Vector<ColorModel> colors = new Vector<ColorModel>();

  // Vector<Dif> sum_dif = new Vector<Dif>();

  // private int biggestClass = 0;

  // Vector<Double> mean_surrounding_l = new Vector<Double>();
  // Vector<Double> mean_surrounding_a = new Vector<Double>();
  // Vector<Double> mean_surrounding_b = new Vector<Double>();
  /**
   * @param args
   */
  public static void main(String[] args) {
    // TODO Auto-generated method stub

  }

  private boolean changeDirection(double[] displacementVector,
      Vector<Double> currentSurroundingColor) {

    double t[] = new double[6];
    t[0] = (LAB.L_MAX - currentSurroundingColor.get(0))
        / displacementVector[0];
    t[1] = (LAB.L_MIN - currentSurroundingColor.get(0))
        / displacementVector[0];

    t[2] = (LAB.A_MAX - currentSurroundingColor.get(1))
        / displacementVector[1];
    t[3] = (LAB.A_MIN - currentSurroundingColor.get(1))
        / displacementVector[1];

    t[4] = (LAB.B_MAX - currentSurroundingColor.get(2))
        / displacementVector[2];
    t[5] = (LAB.B_MIN - currentSurroundingColor.get(2))
        / displacementVector[2];

    double min = getMinAbsValue(t);

    if (min > 0)
      return true;
    return false;

  }

  private double getMinAbsValue(double t[]) {
    double ret = t[0];

    double min = Math.abs(t[0]);
    for (int i = 1; i < t.length; i++) {
      if (min > Math.abs(t[i])) {
        min = Math.abs(t[i]);
        ret = t[i];
      }
    }
    return ret;
  }

  private Vector<Dif> adjustingColorLocally_convergeVisibilities(Data input,
      Vector<ColorModel> labColors) {

    // int ret_ = 0;
    // int step = 1;
    // double threshold = 0.05;

    // TreeMap[] data = input.getData();

    double weight;

    Vector<Float> visibility = input.getVisibility();
    // Vector<Double> saliency = input.getSaliency();
    Vector<Vector<Integer>> data = input.getData();

    Vector<Float> mean_class_visibility = getAverageClassVisibility(
        visibility, data, input.getClassNumbers(), input
            .getNumberElementsOfEachClass());

    double meanVisibility = getMean(visibility);
    logger.logln("mean visibility: " + meanVisibility);
    logger.logln("mean visibility class: "
        + mean_class_visibility.toString());
    double s = 0;
    for (int i = 0; i < input.getClassNumbers(); i++) {
      // change
      s += mean_class_visibility.get(i);
      // if (i!=5 && i!=1)
      // s += mean_class_visibility.get(i);
    }
    // change
    double meanClassVisibility = s / input.getClassNumbers();
    // double meanClassVisibility = s / (input.getClassNumbers() -2);
    logger.logln("average of the mean class visilibilities: "
        + meanClassVisibility);
    // double sal;
    double vis;
    // int x, y;
    int l;
    Vector<Vector<Integer>> surrounding = input.getSurrounding();

    ColorModel currentPointColor;
    Vector<Integer> current_surrounding; // = new
    // Vector<Integer>(input.getClassNumbers()+1);

    Vector<Double> currentSurroundingColor;
    // Vector<Double> sal_as_vector; // = new Vector<Double>(3);
    double displacementVector[] = new double[3];
    double displacementVectorTmp[] = new double[3];

    // double dim1, dim2, dim3;

    Vector<Dif> sum_dif = new Vector<Dif>();
    // sum_dif.removeAllElements();
    for (int i = 0; i < input.getClassNumbers(); i++)
      sum_dif.add(new Dif());

    for (int i = 0; i < data.size(); i++) {
      vis = visibility.get(i);
      // sal = saliency.get(i);
      // x = data.get(i).get(0);
      // y = data.get(i).get(1);
      l = data.get(i).get(2);

      weight = getWeight(i, input, labColors); // input.getMeanViewVisibility().get(i)
                      // / vis;

      current_surrounding = surrounding.get(i);
      currentPointColor = (ColorModel) labColors.elementAt(l);

      // change local color
      currentSurroundingColor = VisUtil.mean_surrounding_color_as_vector(
          currentPointColor, labColors, current_surrounding, input
              .getClassNumbers());

      // logger.logln("adj.Color.Locally - Center Point Color: " +
      // currentSurroundingColor.toString());
      // logger.logln("adj.Color.Locally - MEAN(Surrounding Color): " +
      // currentSurroundingColor.toString());

      for (int j = 0; j < currentSurroundingColor.size(); j++) {
        displacementVector[j] = currentPointColor.getDimension(j)
            - currentSurroundingColor.get(j);

      }
      double length = Math.sqrt(displacementVector[0]
          * displacementVector[0] + displacementVector[1]
          * displacementVector[1] + displacementVector[2]
          * displacementVector[2]);

      // moving away from the mean (from the currentSurroundingColor)
      if (meanVisibility > mean_class_visibility.get(l)) {
        for (int j = 0; j < currentSurroundingColor.size(); j++) {
          displacementVector[j] /= length;
        }
        // check whether to go inward or award.
        // going inward if there is more space to go away from the mean
        if (changeDirection(displacementVector, currentSurroundingColor)) {
          for (int j = 0; j < currentSurroundingColor.size(); j++) {
            displacementVector[j] = -displacementVector[j];
          }
        }
        // ret_++;

      }
      // moving to the mean (from the currentSurroundingColor)
      // always inward pointing
      else if (meanVisibility < mean_class_visibility.get(l)) {
        for (int j = 0; j < currentSurroundingColor.size(); j++) {
          displacementVector[j] /= -length;

        }
        // ret_++;
      }
      // do not move
      else {
        for (int j = 0; j < currentSurroundingColor.size(); j++) {
          displacementVector[j] = 0;
          ;
        }

      }

      // if (Math.abs(meanVisibility-vis) > 10) {
      //       
      // ret_ = false;
      //       
      // }

      // dim1 = currentPointColor.getDimension_1() + changeVector[0];
      // dim2 = currentPointColor.getDimension_2() + changeVector[1];
      // dim3 = currentPointColor.getDimension_3() + changeVector[2];

      Dif d = sum_dif.get(l);
      d.update(displacementVector[0] * weight, displacementVector[1]
          * weight, displacementVector[2] * weight);

      sum_dif.set(l, d);

      // check boundaries on global level is better

      // ColorModel tmp = new ColorModel();
      // tmp.setDimension_1(dim1);
      // tmp.setDimension_2(dim2);
      // tmp.setDimension_3(dim3);
      // tmp.setType(ColorType.LAB);

    }
    return sum_dif;

  }

  private Vector<Dif> adjustingColorLocally_increaseVisibilities(Data input,
      Vector<ColorModel> labColors) {

    double weight;
    // int ret_ = 0;
    // int step = 1;
    // double threshold = 0.05;

    // TreeMap[] data = input.getData();

    Vector<Float> visibility = input.getVisibility();
    // Vector<Double> saliency = input.getSaliency();
    Vector<Vector<Integer>> data = input.getData();

    Vector<Float> mean_class_visibility = getAverageClassVisibility(
        visibility, data, input.getClassNumbers(), input
            .getNumberElementsOfEachClass());

    double meanVisibility = getMean(visibility);
    logger.logln("mean visibility: " + meanVisibility);
    logger.logln("mean visibility class: "
        + mean_class_visibility.toString());
    double s = 0;
    for (int i = 0; i < input.getClassNumbers(); i++) {
      s += mean_class_visibility.get(i);
    }
    double meanClassVisibility = s / input.getClassNumbers();
    logger.logln("average of the mean class visilibilities: "
        + meanClassVisibility);
    // double sal;
    double vis;
    // int x, y;
    int l;
    Vector<Vector<Integer>> surrounding = input.getSurrounding();

    ColorModel currentPointColor;
    Vector<Integer> current_surrounding; // = new
    // Vector<Integer>(input.getClassNumbers()+1);

    Vector<Double> currentSurroundingColor;
    // Vector<Double> sal_as_vector; // = new Vector<Double>(3);
    double displacementVector[] = new double[3];
    double displacementVectorTmp[] = new double[3];

    // double dim1, dim2, dim3;

    Vector<Dif> sum_dif = new Vector<Dif>();
    // sum_dif.removeAllElements();
    for (int i = 0; i < input.getClassNumbers(); i++)
      sum_dif.add(new Dif());

    for (int i = 0; i < data.size(); i++) {
      vis = visibility.get(i);
      l = data.get(i).get(2);

      //weight = input.getMeanViewVisibility().get(i) / vis;
      weight = getWeight(i, input, labColors);
      current_surrounding = surrounding.get(i);
      currentPointColor = (ColorModel) labColors.elementAt(l);

      // change local color
      currentSurroundingColor = VisUtil.mean_surrounding_color_as_vector(
          currentPointColor, labColors, current_surrounding, input
              .getClassNumbers());

      for (int j = 0; j < currentSurroundingColor.size(); j++) {
        displacementVector[j] = currentPointColor.getDimension(j)
            - currentSurroundingColor.get(j);

      }
      double length = Math.sqrt(displacementVector[0]
          * displacementVector[0] + displacementVector[1]
          * displacementVector[1] + displacementVector[2]
          * displacementVector[2]);

      for (int j = 0; j < currentSurroundingColor.size(); j++) {
        displacementVector[j] /= length;
      }

      Dif d = sum_dif.get(l);
      d.update(displacementVector[0] * weight, displacementVector[1]
          * weight, displacementVector[2] * weight);

      sum_dif.set(l, d);

    }
    return sum_dif;

  }

  private Vector<ColorModel> adjustingColorGlobally(Data input,
      Vector<ColorModel> labColors, Vector<Dif> sum_dif) {

    Vector<ColorModel> res = new Vector<ColorModel>();

    Vector<Integer> classesSize = input.getNumberElementsOfEachClass();
    int number_all_elements = input.getNumberOfAllElements();

    for (int i = 0; i < sum_dif.size(); i++) {
      Dif d = sum_dif.get(i);
      double weight = number_all_elements - classesSize.get(i);
      weight *= classesSize.size();
      weight /= number_all_elements;

      double l = d.meanL() * weight;
      double a = d.meanA() * weight;
      double b = d.meanB() * weight;
      logger.logln("disp. vec.: " + Double.toString(l) + "|"
          + Double.toString(a) + "|" + Double.toString(b));

      // double c = labColors.get(i).getDimension_3() /
      // labColors.get(i).getDimension_2();
      l += labColors.get(i).getDimension_1();
      a += labColors.get(i).getDimension_2();
      b += labColors.get(i).getDimension_3();
      // if (labColors.get(i).getDimension_3() < LAB.B_MAX
      // && labColors.get(i).getDimension_2() < LAB.A_MAX
      // && labColors.get(i).getDimension_3() > LAB.B_MIN
      // && labColors.get(i).getDimension_2() > LAB.A_MIN) {
      // a += labColors.get(i).getDimension_2();
      // b += labColors.get(i).getDimension_3();
      // } else {
      // a = labColors.get(i).getDimension_2();
      // b = labColors.get(i).getDimension_3();
      // }
      // b = a*c;

      ColorModel cm = new ColorModel();
      cm.setDimension_1(l);
      cm.setDimension_2(a);
      cm.setDimension_3(b);

      // cm.setDimension_1(LAB.getCorrectL(l));
      // cm.setDimension_2(LAB.getCorrectA(a));
      // cm.setDimension_3(LAB.getCorrectB(b));
      cm.setType(ColorType.LAB);
      if (LAB.isInRGBSpace(cm))
        res.add(cm);
      else
        res.add(labColors.get(i));
    }

    return res;
  }

  private Vector<ColorModel> adjustingColorGloballyHuePreserving(Data input,
      Vector<ColorModel> labColors, Vector<Dif> sum_dif) {
    Vector<ColorModel> res = new Vector<ColorModel>();

    Vector<Integer> classesSize = input.getNumberElementsOfEachClass();
    int number_all_elements = input.getNumberOfAllElements();

    for (int i = 0; i < sum_dif.size(); i++) {
      Dif d = sum_dif.get(i);
      double weight = 1;
      // double weight = number_all_elements - classesSize.get(i);
      // weight *= classesSize.size();
      // weight /= number_all_elements;

      double l_offset = d.meanL() * weight;
      double a_offset = d.meanA() * weight;
      double b_offset = d.meanB() * weight;
      logger.logln("disp. vec.: " + Double.toString(l_offset) + "|"
          + Double.toString(a_offset) + "|"
          + Double.toString(b_offset));

      double hue_constant = labColors.get(i).getDimension_3()
          / labColors.get(i).getDimension_2();

      double l, a, b;

      // the new color
      l = labColors.get(i).getDimension_1() + l_offset;
      a = labColors.get(i).getDimension_2() + a_offset;
      b = a * hue_constant;

      // is it in the lab space
      if (l > LAB.L_MAX || l < LAB.L_MIN) {
        l = labColors.get(i).getDimension_1();
      }
      if (a > LAB.A_MAX || a < LAB.A_MIN || b > LAB.B_MAX
          || b < LAB.B_MIN) {
        a = labColors.get(i).getDimension_2();
        b = labColors.get(i).getDimension_3();
      }

      ColorModel cm = new ColorModel();
      cm.setDimension_1(l);
      cm.setDimension_2(a);
      cm.setDimension_3(b);

      // cm.setDimension_1(LAB.getCorrectL(l));
      // cm.setDimension_2(LAB.getCorrectA(a));
      // cm.setDimension_3(LAB.getCorrectB(b));
      cm.setType(ColorType.LAB);
      // if (LAB.isInRGBSpace(cm))
      res.add(cm);
      // else
      // res.add(labColors.get(i));
    }

    return res;
  }

  private Vector<ColorModel> adjustingColorGloballyHuePreserving_oldVersion(
      Data input, Vector<ColorModel> labColors, Vector<Dif> sum_dif) {
    Vector<ColorModel> res = new Vector<ColorModel>();

    Vector<Integer> classesSize = input.getNumberElementsOfEachClass();
    int number_all_elements = input.getNumberOfAllElements();

    for (int i = 0; i < sum_dif.size(); i++) {
      Dif d = sum_dif.get(i);
      double weight = 1;
      // double weight = number_all_elements - classesSize.get(i);
      // weight *= classesSize.size();
      // weight /= number_all_elements;

      double l = d.meanL() * weight;
      double a = d.meanA() * weight;
      double b = d.meanB() * weight;
      logger.logln("disp. vec.: " + Double.toString(l) + "|"
          + Double.toString(a) + "|" + Double.toString(b));

      double c = labColors.get(i).getDimension_3()
          / labColors.get(i).getDimension_2();

      l += labColors.get(i).getDimension_1();
      // a += labColors.get(i).getDimension_2();

      if (labColors.get(i).getDimension_3() < LAB.B_MAX
          && labColors.get(i).getDimension_2() < LAB.A_MAX
          && labColors.get(i).getDimension_3() > LAB.B_MIN
          && labColors.get(i).getDimension_2() > LAB.A_MIN) {
        a += labColors.get(i).getDimension_2();
        // b += labColors.get(i).getDimension_3();
      } else {
        a = labColors.get(i).getDimension_2();
        // b = labColors.get(i).getDimension_3();
      }
      b = a * c;

      ColorModel cm = new ColorModel();
      // cm.setDimension_1(l);
      // cm.setDimension_2(a);
      // cm.setDimension_3(b);

      cm.setDimension_1(LAB.getCorrectL(l));
      cm.setDimension_2(LAB.getCorrectA(a));
      cm.setDimension_3(LAB.getCorrectB(b));
      cm.setType(ColorType.LAB);
      // if (LAB.isInRGBSpace(cm))
      res.add(cm);
      // else
      // res.add(labColors.get(i));
    }

    return res;
  }

  public Vector<ColorModel> computeNewColor(Data data,
      Vector<ColorModel> labColors) {

    return convergeClassVisibility(data, labColors);

  }

  private Vector<ColorModel> increaseClassVisibility(Data data,
      Vector<ColorModel> labColors) {

    Vector<Dif> d = adjustingColorLocally_increaseVisibilities(data,
        labColors);
    return adjustingColorGloballyHuePreserving(data, labColors, d);
    // return adjustingColorGloballyHuePreserving(data, labColors);
  }

  private Vector<ColorModel> convergeClassVisibility(Data data,
      Vector<ColorModel> labColors) {

    Vector<Dif> d = adjustingColorLocally_convergeVisibilities(data,
        labColors);
    return adjustingColorGloballyHuePreserving(data, labColors, d);
    // return adjustingColorGloballyHuePreserving(data, labColors);
  }

  /*
   * public Vector<ColorModel> increaseVisibility(Data input,
   * Vector<ColorModel> labColors) { Vector<Double> visibility =
   * input.getVisibility(); // Vector<Double> saliency = input.getSaliency();
   * Vector<Vector<Integer>> data = input.getData(); Vector<Vector<Integer>>
   * surrounding = input.getSurrounding();
   *
   * double meanVisibility = getMean(visibility);
   * logger.logln("meanVisibility: " + meanVisibility);
   *
   * // double sal, vis; // int x, y; int l;
   *
   * ColorModel currentPointColor; Vector<Integer> current_surrounding;
   *
   * Vector<Double> currentSurroundingColor; Vector<Dif> displacement = new
   * Vector<Dif>(); Vector<Double> c1 = new Vector<Double>();
   *
   *
   * for (int i = 0; i < input.getClassNumbers(); i++) { displacement.add(new
   * Dif()); c1.add(((ColorModel) labColors.get(i)).getDimension_3() /
   * ((ColorModel) labColors.get(i)).getDimension_2()); }
   *
   * for (int i = 0; i < data.size(); i++) { // vis = visibility.get(i); //
   * sal = saliency.get(i); // x = data.get(i).get(0); // y =
   * data.get(i).get(1); l = data.get(i).get(2);
   *
   * current_surrounding = surrounding.get(i); currentPointColor =
   * (ColorModel) labColors.elementAt(l);
   *
   * // change local color currentSurroundingColor =
   * VisUtil.mean_surrounding_color_as_vector( currentPointColor, labColors,
   * current_surrounding); // logger.logln("surrounding color" + //
   * currentSurroundingColor.toString()); //
   * mean_surrounding_l.add(currentSurroundingColor.get(0)); //
   * mean_surrounding_a.add(currentSurroundingColor.get(1)); //
   * mean_surrounding_b.add(currentSurroundingColor.get(2));
   *
   * Dif d = displacement.get(l); // double length =
   * Math.sqrt(displacementVector[0] // displacementVector[0] +
   * displacementVector[1] // displacementVector[1] + displacementVector[2] //
   * displacementVector[2]); d.update(currentSurroundingColor.get(0),
   * currentSurroundingColor .get(1), currentSurroundingColor.get(2));
   * displacement.set(l, d);
   *
   * } // get the biggest class
   *
   * // int max_members = 0; // for (int i = 0; i < displacement.size(); i++)
   * { // if (max_members < displacement.get(i).getNum()) { // max_members =
   * displacement.get(i).getNum(); // biggestClass = i; // } // }
   *
   * Vector<ColorModel> res = new Vector<ColorModel>();
   *
   * // check mean color
   *
   * double threshold = 0.05; for (int i = 0; i < displacement.size(); i++) {
   * Dif d = displacement.get(i); double l_ = d.meanL(); double a_ =
   * d.meanA(); double b_ = d.meanB(); // System.out.println("mean: " + l_ +
   * " " + a_ + " " + b_); logger.logln("mean: " + l_ + " " + a_ + " " + b_);
   *
   * l_ = labColors.get(i).getDimension_1() - l_; a_ =
   * labColors.get(i).getDimension_2() - a_; b_ =
   * labColors.get(i).getDimension_3() - b_;
   *
   * // if (i == biggestClass && l_ < 0) // l_ = -l_;
   *
   * l_ = labColors.get(i).getDimension_1() + threshold l_; a_ =
   * labColors.get(i).getDimension_2() + threshold a_; // b_ =
   * labColors.get(i).getDimension_3() + threshold b_; b_ = c1.get(i) a_;
   *
   * // l_ *= threshold; // a_ *= threshold; // b_ *= threshold;
   *
   * ColorModel cm = new ColorModel(); cm.setDimension_1(LAB.getCorrectL(l_));
   * cm.setDimension_2(LAB.getCorrectA(a_));
   * cm.setDimension_3(LAB.getCorrectB(b_)); cm.setType(ColorType.LAB);
   * res.add(cm); }
   *
   * return res; }
   */

  // lab' - lab stopping criteria
  // public void optimize(Data data, Vector<ColorModel> rgbColors, int radius)
  // {
  //
  // // initNumberClassElements(data.getData(), data.getNumberOfClasses());
  // System.out.println("start opt");
  // // radius = 20;
  // data.computeSaliency(radius, rgbColors);
  // data.computeVisibility(radius);
  // data.export("input_measures.dat");
  //
  // Vector<ColorModel> labColors = new Vector<ColorModel>();
  // VisUtil.rgbToLabColors(rgbColors, labColors);
  //
  // logger.logln("INPUT: ");
  // for (int i = 0; i < labColors.size(); i++) {
  // logger.logln(labColors.get(i).toString());
  //
  // // print hue
  // double c = labColors.get(i).getDimension_3()
  // / labColors.get(i).getDimension_2();
  // logger.logln("HUE: " + c);
  // }
  //
  // // increase the mean visibility
  // // for (int j=0; j < 50; j++) {
  // // labColors = increaseClassVisibility(data, labColors);
  // // for (int i = 0; i < labColors.size(); i++)
  // // logger.logln(labColors.get(i).toString());
  // // data.computeSaliencySungKill(labColors);
  // // data.computeVisibility(radius);
  // // }
  //
  // Vector<ColorModel> labColorsOutput = new Vector<ColorModel>();
  // Vector<ColorModel> labColors_ = new Vector<ColorModel>();
  // Vector<ColorModel> cm = null;
  // boolean stop = false;
  //
  // double dif1;
  // double dif2;
  // double dif3;
  //
  // for (int j = 0; j < numberImprovements; j++) {
  //
  // // increase vis
  // labColors = increaseClassVisibility(data, labColors);
  // for (int i = 0; i < labColors.size(); i++)
  // logger.logln(labColors.get(i).toString());
  // data.computeSaliencySungKill(labColors);
  // data.computeVisibility(radius);
  //
  // labColorsOutput = computeNewColor(data, labColors);
  //
  // for (int i = 0; i < labColorsOutput.size(); i++)
  // logger.logln(labColorsOutput.get(i).toString());
  //
  // // lastImprovement.add(labColorsOutput);
  // Vector<Double> currentImprovement = new Vector<Double>();
  // for (int i = 0; i < labColorsOutput.size(); i++) {
  // dif1 = labColorsOutput.get(i).getDimension_1()
  // - labColors.get(i).getDimension_1();
  // dif2 = labColorsOutput.get(i).getDimension_2()
  // - labColors.get(i).getDimension_2();
  // dif3 = labColorsOutput.get(i).getDimension_3()
  // - labColors.get(i).getDimension_3();
  //
  // currentImprovement.add(dif1);
  // currentImprovement.add(dif2);
  // currentImprovement.add(dif3);
  //
  // }
  // improvement.add(currentImprovement);
  // if (j == 0) {
  // for (int i = 0; i < currentImprovement.size(); i++) {
  // sumImprovements.add(currentImprovement.get(i));
  // }
  // } else {
  // for (int i = 0; i < currentImprovement.size(); i++) {
  // sumImprovements.set(i, currentImprovement.get(i)
  // + sumImprovements.get(i));
  // }
  // }
  //
  // labColors = labColorsOutput;
  // data.computeSaliencySungKill(labColors);
  // data.computeVisibility(radius);
  //
  // }
  //
  // for (int j = 0; j < 750; j++) {
  // // System.out.println("step: " + j);
  //
  // // increse vis
  // labColors_ = increaseClassVisibility(data, labColors);
  // for (int i = 0; i < labColors_.size(); i++)
  // logger.logln(labColors_.get(i).toString());
  // data.computeSaliencySungKill(labColors_);
  // data.computeVisibility(radius);
  //
  // labColorsOutput = computeNewColor(data, labColors_);
  //
  // for (int i = 0; i < labColorsOutput.size(); i++)
  // logger.logln(labColorsOutput.get(i).toString());
  //
  // // update the list with the last results
  // // update the sum of the last 'numberLastResults' changes
  // Vector<Double> firstImprovement = (Vector<Double>) improvement
  // .removeFirst();
  // Vector<Double> currentImprovement = new Vector<Double>();
  //
  // for (int i = 0; i < labColorsOutput.size(); i++) {
  // dif1 = labColorsOutput.get(i).getDimension_1()
  // - labColors.get(i).getDimension_1();
  // dif2 = labColorsOutput.get(i).getDimension_2()
  // - labColors.get(i).getDimension_2();
  // dif3 = labColorsOutput.get(i).getDimension_3()
  // - labColors.get(i).getDimension_3();
  //
  // currentImprovement.add(dif1);
  // currentImprovement.add(dif2);
  // currentImprovement.add(dif3);
  // }
  // improvement.add(currentImprovement);
  //
  // for (int i = 0; i < currentImprovement.size(); i++) {
  // sumImprovements.set(i, sumImprovements.get(i)
  // + currentImprovement.get(i) - firstImprovement.get(i));
  // }
  //
  // Iterator it = sumImprovements.iterator();
  // stop = true;
  // logger.log("stopping criteria, improvement sum: ");
  // while (it.hasNext()) {
  // Double d = (Double) it.next();
  // logger.log(d.toString());
  // logger.log("  ");
  // if (Math.abs(d.doubleValue()) >= threshold) {
  // stop = false;
  // break;
  // }
  // }
  // logger.logln("");
  //
  // labColors = labColorsOutput;
  //
  // if (stop == true) {
  //
  // it = improvement.iterator();
  // while (it.hasNext())
  // logger.logln("improvement: " + it.next().toString());
  // System.out
  // .println("the optimization has found a local solution");
  //
  // System.out.println("steps: " + j);
  // break;
  // }
  //
  // data.computeSaliencySungKill(labColors);
  // data.computeVisibility(radius);
  //
  // }
  // // MemoryUtil.printMemoryToFile("end");
  // // MemoryUtil.freeMemory();
  // // MemoryUtil.printMemoryToFile("free memory");
  //
  // logger.logln("RESULT: ");
  // for (int i = 0; i < labColors.size(); i++) {
  //
  // logger.logln(labColors.get(i).toString());
  //
  // // print hue
  // double c = labColors.get(i).getDimension_3()
  // / labColors.get(i).getDimension_2();
  // logger.logln("HUE: " + c);
  // }
  //
  // double meanVisibility = getMean(data.getVisibility());
  // logger.logln("RESULT LAB mean visibility: " + meanVisibility);
  // VisUtil.labToRGBColors(rgbColors, labColors);
  //
  // VisUtil.rgbToLabColors(rgbColors, labColors);
  // data.computeSaliencySungKill(labColors);
  // data.computeVisibility(radius);
  //
  // meanVisibility = getMean(data.getVisibility());
  // logger.logln("RESULT RGB mean visibility: " + meanVisibility);
  //
  // for (int i = 0; i < rgbColors.size(); i++)
  // logger.log(rgbColors.get(i).toString());
  //
  // // MemoryUtil.closeFile();
  // data.export("optimized_measures.dat");
  // logger.close();
  //
  // System.out.println("end opt");
  // }
  public Vector<ColorModel> optimize(Data data, Vector<ColorModel> rgbColors,
      int radiusSurround, int radiusView) {
    System.out.println("start opt");
    // radius = 20;
    data.computeSaliency(radiusSurround, rgbColors);
    data.computeVisibility(radiusView);
    // data.export("input_measures.dat");
    Vector<ColorModel> labColors = new Vector<ColorModel>();
    VisUtil.rgbToLabColors(rgbColors, labColors);

    logger.logln("INPUT: ");
    for (int i = 0; i < labColors.size(); i++) {
      logger.logln(labColors.get(i).toString());

      // print hue
      double c = labColors.get(i).getDimension_3()
          / labColors.get(i).getDimension_2();
      logger.logln("HUE: " + c);
    }

    // for (int i=0; i < 5; i++) {
    labColors = optimize_increase(data, labColors, radiusView);
    labColors = optimize_converge(data, labColors, radiusView);
    // }

    VisUtil.labToRGBColors(rgbColors, labColors);
    data.computeSaliencySungKill(labColors);
    data.computeVisibility(radiusView);

    double meanVisibility = getMean(data.getVisibility());
    logger.logln("RESULT RGB mean visibility: " + meanVisibility);

    for (int i = 0; i < data.getClassNumbers(); i++)
      logger.log(rgbColors.get(i).toString());

    // MemoryUtil.closeFile();
    data.export("optimized_measures.dat");
    logger.close();

    System.out.println("end opt");
    return rgbColors;
  }

  private Vector<ColorModel> optimize_increase(Data data,
      Vector<ColorModel> labColors, int radiusView) {

    // initNumberClassElements(data.getData(), data.getNumberOfClasses());
    // System.out.println("start opt");
    // // radius = 20;
    // data.computeSaliency(radius, rgbColors);
    // data.computeVisibility(radius);
    // data.export("input_measures.dat");
    //
    // Vector<ColorModel> labColors = new Vector<ColorModel>();
    // VisUtil.rgbToLabColors(rgbColors, labColors);

    // logger.logln("INPUT: ");
    // for (int i = 0; i < data.getClassNumbers(); i++) {
    // logger.logln(labColors.get(i).toString());
    //
    // // print hue
    // double c = labColors.get(i).getDimension_3()
    // / labColors.get(i).getDimension_2();
    // logger.logln("HUE: " + c);
    // }

    // increase the mean visibility
    // for (int j=0; j < 50; j++) {
    // labColors = increaseClassVisibility(data, labColors);
    // for (int i = 0; i < labColors.size(); i++)
    // logger.logln(labColors.get(i).toString());
    // data.computeSaliencySungKill(labColors);
    // data.computeVisibility(radius);
    // }

    Vector<ColorModel> labColorsOutput = new Vector<ColorModel>();
    // Vector<ColorModel> labColors_ = new Vector<ColorModel>();
    // Vector<ColorModel> cm = null;
    // boolean stop = true;

    // double dif1;
    // double dif2;
    // double dif3;

    double vis_ = 0;
    double currentImprovement = 0;
    for (int j = 0; j < numberImprovements; j++) {

      // increase vis

      vis_ = getMean(data.getVisibility());

      // we conducted the expirement - one step increase, one step
      // converge. The result is, the mean visibility did not change a lot

      // labColors_ = increaseClassVisibility(data, labColors);
      // for (int i = 0; i < data.getClassNumbers(); i++)
      // logger.logln(labColors_.get(i).toString());
      // data.computeSaliencySungKill(labColors_);
      // data.computeVisibility(radiusView);

      labColorsOutput = increaseClassVisibility(data, labColors);

      for (int i = 0; i < labColorsOutput.size(); i++)
        logger.logln(labColorsOutput.get(i).toString());

      labColors = labColorsOutput;
      data.computeSaliencySungKill(labColors);
      data.computeVisibility(radiusView);

      currentImprovement = getMean(data.getVisibility()) - vis_;
      improvement.add(currentImprovement);
      sumImprovements += currentImprovement;

    }

    for (int j = 0; j < 250; j++) {
      // System.out.println("step: " + j);

      // increse vis
      vis_ = getMean(data.getVisibility());
      // labColors_ = increaseClassVisibility(data, labColors);
      // for (int i = 0; i < labColors_.size(); i++)
      // logger.logln(labColors_.get(i).toString());
      // data.computeSaliencySungKill(labColors_);
      // data.computeVisibility(radiusView);

      labColorsOutput = increaseClassVisibility(data, labColors);

      for (int i = 0; i < data.getClassNumbers(); i++)
        logger.logln(labColorsOutput.get(i).toString());

      labColors = labColorsOutput;
      data.computeSaliencySungKill(labColors);
      data.computeVisibility(radiusView);

      // update the list with the last results
      // update the sum of the last 'numberLastResults' changes

      currentImprovement = getMean(data.getVisibility()) - vis_;
      improvement.add(currentImprovement);
      sumImprovements += currentImprovement;

      Double firstImprovement = (Double) improvement.removeFirst();
      sumImprovements -= firstImprovement;

      logger.logln("stopping criteria, improvement sum: "
          + sumImprovements);

      if (Math.abs(sumImprovements) < threshold) {
        Iterator it = improvement.iterator();
        while (it.hasNext())
          logger.logln("improvement: " + it.next().toString());
        System.out
            .println("Increase Visibility: local solution is found");

        System.out.println("steps: " + j);
        logger.logln("steps: " + j);
        break;
      }
    }

    return labColors;

    // logger.logln("RESULT: ");
    // for (int i = 0; i < labColors.size(); i++) {
    //
    // logger.logln(labColors.get(i).toString());
    //
    // // print hue
    // double c = labColors.get(i).getDimension_3()
    // / labColors.get(i).getDimension_2();
    // logger.logln("HUE: " + c);
    // }
    //
    // double meanVisibility = getMean(data.getVisibility());
    // logger.logln("RESULT LAB mean visibility: " + meanVisibility);
    // VisUtil.labToRGBColors(rgbColors, labColors);
    //
    // VisUtil.rgbToLabColors(rgbColors, labColors);
    // data.computeSaliencySungKill(labColors);
    // data.computeVisibility(radius);
    //
    // meanVisibility = getMean(data.getVisibility());
    // logger.logln("RESULT RGB mean visibility: " + meanVisibility);
    //
    // for (int i = 0; i < rgbColors.size(); i++)
    // logger.log(rgbColors.get(i).toString());
    //
    // // MemoryUtil.closeFile();
    // data.export("optimized_measures.dat");
    // logger.close();
    //
    // System.out.println("end opt");
  }

  private Vector<ColorModel> optimize_converge(Data data,
      Vector<ColorModel> labColors, int radiusView) {

    Vector<ColorModel> rgbColors = new Vector<ColorModel>();
    // initNumberClassElements(data.getData(), data.getNumberOfClasses());
    // System.out.println("start opt");
    // // radius = 20;
    // data.computeSaliency(radius, rgbColors);
    // data.computeVisibility(radius);
    // data.export("input_measures.dat");
    //
    // Vector<ColorModel> labColors = new Vector<ColorModel>();
    // VisUtil.rgbToLabColors(rgbColors, labColors);

    logger.logln("INPUT: ");
    for (int i = 0; i < data.getClassNumbers(); i++) {
      logger.logln(labColors.get(i).toString());

      // print hue
      double c = labColors.get(i).getDimension_3()
          / labColors.get(i).getDimension_2();
      logger.logln("HUE: " + c);
    }

    // increase the mean visibility
    // for (int j=0; j < 50; j++) {
    // labColors = increaseClassVisibility(data, labColors);
    // for (int i = 0; i < labColors.size(); i++)
    // logger.logln(labColors.get(i).toString());
    // data.computeSaliencySungKill(labColors);
    // data.computeVisibility(radius);
    // }

    Vector<ColorModel> labColorsOutput = new Vector<ColorModel>();
    Vector<ColorModel> labColors_ = new Vector<ColorModel>();
    Vector<ColorModel> cm = null;
    boolean stop = true;

    double dif1;
    double dif2;
    double dif3;

    double vis_ = 0;
    double currentImprovement = 0;
    sumImprovements = 0;
    for (int j = 0; j < numberImprovements; j++) {

      vis_ = getMean(data.getVisibility());
      // labColors_ = increaseClassVisibility(data, labColors);
      // for (int i = 0; i < labColors_.size(); i++)
      // logger.logln(labColors_.get(i).toString());
      // data.computeSaliencySungKill(labColors_);
      // data.computeVisibility(radius);

      labColorsOutput = convergeClassVisibility(data, labColors);

      for (int i = 0; i < data.getClassNumbers(); i++)
        logger.logln(labColorsOutput.get(i).toString());

      labColors = labColorsOutput;
      data.computeSaliencySungKill(labColors);
      data.computeVisibility(radiusView);

      currentImprovement = getMean(data.getVisibility()) - vis_;
      improvement.add(currentImprovement);
      sumImprovements += currentImprovement;

    }

    for (int j = 0; j < 750; j++) {
      // System.out.println("step: " + j);

      // increse vis
      vis_ = getMean(data.getVisibility());

      // labColors_ = increaseClassVisibility(data, labColors);
      // for (int i = 0; i < labColors_.size(); i++)
      // logger.logln(labColors_.get(i).toString());
      // data.computeSaliencySungKill(labColors_);
      // data.computeVisibility(radius);

      labColorsOutput = convergeClassVisibility(data, labColors);

      for (int i = 0; i < data.getClassNumbers(); i++)
        logger.logln(labColorsOutput.get(i).toString());

      labColors = labColorsOutput;
      data.computeSaliencySungKill(labColors);
      data.computeVisibility(radiusView);

      // update the list with the last results
      // update the sum of the last 'numberLastResults' changes

      currentImprovement = getMean(data.getVisibility()) - vis_;
      improvement.add(currentImprovement);
      sumImprovements += currentImprovement;

      Double firstImprovement = (Double) improvement.removeFirst();
      sumImprovements -= firstImprovement;

      logger.logln("stopping criteria, improvement sum: "
          + sumImprovements);

      if (Math.abs(sumImprovements) < threshold) {
        Iterator it = improvement.iterator();
        while (it.hasNext())
          logger.logln("improvement: " + it.next().toString());
        System.out
            .println("the optimization has found a local solution");

        System.out.println("steps: " + j);
        logger.logln("steps: " + j);
        break;
      }
    }

    logger.logln("RESULT: ");
    for (int i = 0; i < data.getClassNumbers(); i++) {

      logger.logln(labColors.get(i).toString());

      // print hue
      double c = labColors.get(i).getDimension_3()
          / labColors.get(i).getDimension_2();
      logger.logln("HUE: " + c);
    }

    double meanVisibility = getMean(data.getVisibility());
    logger.logln("RESULT LAB mean visibility: " + meanVisibility);
    VisUtil.labToRGBColors(rgbColors, labColors);

    // VisUtil.rgbToLabColors(rgbColors, labColors);
    // data.computeSaliencySungKill(labColors);
    // data.computeVisibility(radius);
    //
    // meanVisibility = getMean(data.getVisibility());
    // logger.logln("RESULT RGB mean visibility: " + meanVisibility);
    //
    // for (int i = 0; i < data.getClassNumbers(); i++)
    // logger.log(rgbColors.get(i).toString());
    //
    // // MemoryUtil.closeFile();
    // data.export("optimized_measures.dat");
    // logger.close();
    //
    // System.out.println("end opt");

    return labColors;
  }

  public static double getMean(Vector v) {
    Iterator it = v.iterator();
    double s = 0;
    while (it.hasNext())
      s += (Float) it.next();

    return s / v.size();
  }

  public Vector<Float> getAverageClassVisibility(Vector<Float> v,
      Vector<Vector<Integer>> p, int classes, Vector<Integer> classesSize) {
    Vector<Float> ret = new Vector<Float>();
    for (int i = 0; i < classes; i++) {
      ret.add(0.0f);
      // number_class_elements.add(0);
    }

    int l;
    float v_score;
    for (int i = 0; i < p.size(); i++) {
      l = p.get(i).get(2);
      v_score = v.get(i);
      ret.set(l, ret.get(l) + v_score);
      // number_class_elements.set(l, number_class_elements.get(l) + 1);
    }
    for (int i = 0; i < classes; i++) {
      ret.set(i, ret.get(i) / classesSize.get(i));
    }
    return ret;
  }

  // private void initNumberClassElements(Vector<Vector<Integer>> p, int
  // classes) {
  // if (number_class_elements.isEmpty() == false)
  // return;
  //   
  // for (int i = 0; i < classes; i++) {
  // number_class_elements.add(0);
  // }
  // int l;
  // number_all_elements = p.size();
  // for (int i = 0; i < number_all_elements; i++) {
  // l = p.get(i).get(2);
  // number_class_elements.set(l, number_class_elements.get(l) + 1);
  // }
  // }

  double getWeight(int currentPoint, Data input,
      Vector<ColorModel> v_cm) {
    double w = 0;
    // version 3 - meanVisibility/ the visibility of the current point
    // w = input.getMeanViewVisibility().get(i) /
    // input.getVisibility().get(i);

    // version4
    int l = input.getData().get(currentPoint).get(2);
    double saliency = input.getSaliency().get(currentPoint);
    Vector<Vector<Integer>> number_surrounding_points = input
        .getSurrounding();

    Vector<Integer> number_points = number_surrounding_points
        .get(currentPoint);
    double n = number_points.get(l);
    double z = n * saliency;

    double u;
    double ch = 0;

    ColorModel cm = v_cm.get(l);
    for (int j = 0; j < input.getNumberOfClasses(); j++) {
      n = number_points.get(j);
      u = VisUtil.eucl_distance_center_surrounding(cm, v_cm,
          number_points, input.getNumberOfClasses());
      ch += n * u;
    }

    w = ch / z;
    return w;
  }

}
TOP

Related Classes of de.mpi.rgblab.optimizer.Optimizer$Dif

TOP
Copyright © 2018 www.massapi.com. All rights reserved.
All source code are property of their respective owners. Java is a trademark of Sun Microsystems, Inc and owned by ORACLE Inc. Contact coftware#gmail.com.