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

jeco.core.problem.Solution Maven / Gradle / Ivy

The newest version!
package jeco.core.problem;

import java.util.ArrayList;
import java.util.Comparator;
import java.util.HashMap;
import java.util.Map;

public class Solution> {

  protected ArrayList variables = new ArrayList();
  protected ArrayList objectives = new ArrayList();
  protected HashMap properties = new HashMap();

  public Solution(int numberOfObjectives) {
    for (int i = 0; i < numberOfObjectives; ++i) {
      objectives.add(0.0);
    }
  }

  public ArrayList getVariables() {
    return variables;
  }

  public V getVariable(int idx) {
    return variables.get(idx);
  }

  public ArrayList getObjectives() {
    return objectives;
  }

  public Double getObjective(int idx) {
    return objectives.get(idx);
  }

  public HashMap getProperties() {
    return properties;
  }

  @SuppressWarnings("unchecked")
  @Override
  public Solution clone() {
    Solution clone = new Solution(objectives.size());
    for (int i = 0; i < objectives.size(); ++i) {
      clone.objectives.set(i, objectives.get(i));
    }
    for (int i = 0; i < variables.size(); ++i) {
      clone.variables.add((V) variables.get(i).clone());
    }

    for (Map.Entry entry : properties.entrySet()) {
      clone.properties.put(entry.getKey(), entry.getValue());
    }

    return clone;
  }

  public int compareTo(Solution solution, Comparator> comparator) {
    return comparator.compare(this, solution);
  }

  @SuppressWarnings("unchecked")
	@Override
  public boolean equals(Object right) {
    Solution sol = (Solution) right;
    int nVar = Math.min(variables.size(), sol.variables.size());
    for (int i = 0; i < nVar; ++i) {
      if (!this.getVariable(i).equals(sol.getVariable(i))) {
        return false;
      }
    }
    int nObj = Math.min(objectives.size(), sol.objectives.size());

    for (int i = 0; i < nObj; ++i) {
      if (!this.getObjective(i).equals(sol.getObjective(i))) {
        return false;
      }
    }
    return true;
  }

  @Override
  public String toString() {
    StringBuilder buffer = new StringBuilder();
    for (int i = 0; i < this.objectives.size(); ++i) {
      buffer.append(objectives.get(i)).append(" ");
    }
    return buffer.toString();
  }
}




© 2015 - 2024 Weber Informatics LLC | Privacy Policy