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

xf.xflp.opt.construction.strategy.BaseStrategy Maven / Gradle / Ivy

There is a newer version: 0.6.0-RELEASE
Show newest version
package xf.xflp.opt.construction.strategy;

import xf.xflp.base.container.Container;
import xf.xflp.base.item.Item;
import xf.xflp.base.position.PositionCandidate;
import xf.xflp.exception.XFLPException;

import java.util.ArrayList;
import java.util.List;
import java.util.function.Function;

/** 
 * Copyright (c) 2012-2022 Holger Schneider
 * All rights reserved.
 *
 * This source code is licensed under the MIT License (MIT) found in the
 * LICENSE file in the root directory of this source tree.
 *
 * @author hschneid
 **/
public abstract class BaseStrategy {

	public abstract PositionCandidate choose(Item item, Container container, List posList) throws XFLPException;

	protected List getPositionWithMinValue(List candidates,
															  Function positionValue) {
		if(candidates == null) {
			return new ArrayList<>();
		}

		if(candidates.size() <= 1) {
			return candidates;
		}

		float[] distances = new float[candidates.size()];
		for (int i = distances.length - 1; i >= 0; i--) {
			distances[i] = positionValue.apply(candidates.get(i));
		}

		float minValue = Float.MAX_VALUE;
		for (int i = candidates.size() - 1; i >= 0; i--) {
			minValue = Math.min(minValue, distances[i]);
		}

		// Search all positions with max value
		List filteredPositions = new ArrayList<>();
		for (int i = distances.length - 1; i >= 0; i--) {
			if(distances[i] == minValue) {
				filteredPositions.add(candidates.get(i));
			}
		}
		return filteredPositions;
	}
}




© 2015 - 2024 Weber Informatics LLC | Privacy Policy