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

net.sourceforge.cilib.util.selection.WeightedSelection Maven / Gradle / Ivy

/**           __  __
 *    _____ _/ /_/ /_    Computational Intelligence Library (CIlib)
 *   / ___/ / / / __ \   (c) CIRG @ UP
 *  / /__/ / / / /_/ /   http://cilib.net
 *  \___/_/_/_/_.___/
 */
package net.sourceforge.cilib.util.selection;

import static com.google.common.base.Preconditions.checkArgument;
import static com.google.common.base.Preconditions.checkState;
import com.google.common.base.Predicate;
import com.google.common.collect.Iterables;
import com.google.common.collect.Lists;
import java.util.Arrays;
import java.util.List;
import net.sourceforge.cilib.util.selection.arrangement.Arrangement;

/**
 *
 */
public class WeightedSelection implements PartialSelection {

    private final WeightedObject[] elements;

    public static  WeightedSelection copyOf(Iterable iterable) {
        return new WeightedSelection(copyOfInternal(iterable));
    }

    // Check array size
    private static  WeightedObject[] copyOfInternal(Iterable iterable) {
        checkArgument(Iterables.size(iterable) >= 1, "Attempting to create a "
                + "selection on an empty collection is not valid.");
        List list = Lists.newArrayList(iterable);
        List result = Lists.newArrayListWithExpectedSize(list.size());
        for (T t : list) {
            result.add(new WeightedObject(t, 0.0));
        }
        return result.toArray(new WeightedObject[]{});
    }

    WeightedSelection(WeightedObject[] array) {
        this.elements = array;
    }

    @Override
    public PartialSelection exclude(T... items) {
        return exclude(Lists.newArrayList(items));
    }

    @Override
    public WeightedSelection orderBy(Arrangement arrangement) {
        List list = Lists.newArrayList(elements);
        List result = Lists.newArrayList(arrangement.arrange(list));
        return new WeightedSelection(result.toArray(new WeightedObject[]{}));
    }

    @Override
    public PartialSelection exclude(Iterable items) {
        List list = Lists.newArrayList(items);
        List result = Lists.newArrayList();
        for (WeightedObject o : elements) {
            if (!list.contains(o.getObject())) {
                result.add((T) o.getObject());
            }
        }
        return new WeightedSelection(copyOfInternal(result));
    }

    @Override
    public PartialSelection filter(Predicate predicate) {
        List result = Lists.newArrayList();
        for (WeightedObject o : elements) {
            if (!predicate.apply((T) o.getObject())) {
                result.add((T) o.getObject());
            }
        }
        return new WeightedSelection(copyOfInternal(result));
    }

    @Override
    public T select() {
        checkState(elements.length >= 1, "Selection is invalid, please verify selection state.");
        return (T) this.elements[0].getObject();
    }

    @Override
    public List select(Samples sampler) {
        List list = Lists.newArrayList();
        for (WeightedObject element : elements) {
            list.add((T) element.getObject());
        }
        return (List) sampler.sample(list);
    }

    public List weightedElements() {
        return Arrays.asList(elements);
    }
}




© 2015 - 2025 Weber Informatics LLC | Privacy Policy