net.sourceforge.cilib.util.selection.weighting.ParticleBehaviorWeighting.java Source code

Java tutorial

Introduction

Here is the source code for net.sourceforge.cilib.util.selection.weighting.ParticleBehaviorWeighting.java

Source

/**
 * Computational Intelligence Library (CIlib)
 * Copyright (C) 2003 - 2010
 * Computational Intelligence Research Group (CIRG@UP)
 * Department of Computer Science
 * University of Pretoria
 * South Africa
 *
 * This library is free software; you can redistribute it and/or modify
 * it under the terms of the GNU Lesser General Public License as published by
 * the Free Software Foundation; either version 3 of the License, or
 * (at your option) any later version.
 *
 * This library is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU Lesser General Public License for more details.
 *
 * You should have received a copy of the GNU Lesser General Public License
 * along with this library; if not, see <http://www.gnu.org/licenses/>.
 */
package net.sourceforge.cilib.util.selection.weighting;

import com.google.common.base.Preconditions;
import com.google.common.collect.Iterables;
import com.google.common.collect.Lists;
import java.util.List;
import net.sourceforge.cilib.pso.particle.ParticleBehavior;
import net.sourceforge.cilib.util.selection.WeightedObject;

/**
 * Weighs ParticleBehaviors mainly for HPSO.
 */
public class ParticleBehaviorWeighting implements Weighting {

    private ParticleBehaviorRatio ratio;

    public ParticleBehaviorWeighting() {
        this.ratio = new SuccessRatio();
    }

    public ParticleBehaviorWeighting(ParticleBehaviorRatio ratio) {
        this.ratio = ratio;
    }

    @Override
    public <T> Iterable<WeightedObject> weigh(Iterable<T> iterable) {
        Preconditions.checkArgument(Iterables.get(iterable, 0) instanceof ParticleBehavior);
        List<WeightedObject> result = Lists.newArrayList();

        for (T t : iterable) {
            double weight = ratio.getRatio((ParticleBehavior) t);
            result.add(new WeightedObject(t, weight));
        }

        return result;
    }

    public void setRatio(ParticleBehaviorRatio ratio) {
        this.ratio = ratio;
    }
}