Java Array Join joinParetos(double[][] pareto1, double[][] pareto2)

Here you can find the source of joinParetos(double[][] pareto1, double[][] pareto2)

Description

join Paretos

License

LGPL

Declaration

public static double[][] joinParetos(double[][] pareto1,
            double[][] pareto2) throws Exception 

Method Source Code

//package com.java2s;
//License from project: LGPL 

import java.util.ArrayList;
import java.util.List;

public class Main {
    public static double[][] joinParetos(double[][] pareto1,
            double[][] pareto2) throws Exception {
        if (pareto1 == null) {
            if (pareto2 != null)
                return pareto2;
        } else if (pareto2 == null) {
            if (pareto1 != null)
                return pareto1;
        }//from   w  w  w.java 2s  . c om

        if (pareto1[0].length != pareto2[0].length) {
            throw new Exception(
                    "Cannot merge pareto fronts with different number of objetives");
        } else {

            int numObjs = pareto2[0].length;
            int par1size = pareto1.length;
            int par2size = pareto2.length;
            ArrayList<double[]> newPareto = new ArrayList<double[]>(
                    par1size + par2size);
            for (int i = 0; i < pareto1.length; i++) {
                if (!solutionIsInFront(pareto1[i], newPareto))
                    newPareto.add(pareto1[i]);
            }
            for (int i = 0; i < pareto2.length; i++) {
                if (!solutionIsInFront(pareto2[i], newPareto))
                    newPareto.add(pareto2[i]);
            }

            double[][] newParetoArray = new double[newPareto.size()][numObjs];
            newParetoArray = newPareto.toArray(newParetoArray);

            return newParetoArray;
        }
    }

    public static boolean solutionIsInFront(double[] sol,
            List<double[]> front) {
        boolean isIn = false;

        for (double[] compSol : front) {
            if (solutionIsEqual(sol, compSol)) {
                isIn = true;
                break;
            }
        }

        return isIn;
    }

    public static boolean solutionIsEqual(double[] sol1, double[] sol2) {

        boolean allEqual = true;

        for (int i = 0; i < sol1.length; i++) {
            if (sol1[i] != sol2[i]) {
                allEqual = false;
                break;
            }
        }

        return allEqual;
    }
}

Related

  1. joinAttributes(final String[] atts1, final String[] atts2)
  2. joinByComma(String[] values)
  3. joinByteArrays(final byte[] array1, byte[] array2)
  4. joinBytesArrays(byte[]... args)
  5. joiner(int[] ints, String split)
  6. joinString(String delimiter, String[] strings)
  7. joinString(String[] array)
  8. joinString(String[] str, String delimiter)
  9. joinStringArray(String[] array1, String[] array2)