eldemonio1

 avatar
unknown
java
a year ago
3.9 kB
14
Indexable
package pi4_BLR1171.util;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

import pi4_BLR1171.ejercicios.datos.DatosProductosDestinos;
import us.lsi.ag.ValuesInRangeData;
import us.lsi.ag.agchromosomes.ChromosomeFactory.ChromosomeType;

public class InRangeProductosDestinos implements ValuesInRangeData<Integer, SolucionProductosDestinos> {

	public InRangeProductosDestinos (String fichero) {
        // TODO Auto-generated constructor stub
		DatosProductosDestinos.iniDatos(fichero);
    }
	
	@Override
	public ChromosomeType type() {
		// TODO Auto-generated method stub
		return ChromosomeType.Range;
	}
	
	@Override
	public Integer size() {
		// TODO Auto-generated method stub
		return DatosProductosDestinos.getN()*DatosProductosDestinos.getM();
	}
	
	@Override
	public Integer min(Integer i) {
		// TODO Auto-generated method stub
		return 0;
	}
	
	@Override
	public Integer max(Integer i) {
		// TODO Auto-generated method stub
		return DatosProductosDestinos.getUdsProducto(i / DatosProductosDestinos.getM())+1;
	}

	private int objetivo(List<Integer> crom) {
		// TODO Auto-generated method stub
		int goal = 0;
		for (int k = 0; k < crom.size(); k++) {
			int i = k / DatosProductosDestinos.getM();
			int j = k % DatosProductosDestinos.getM();
			goal += crom.get(k) * DatosProductosDestinos.getCosteAlmacenamiento(i, j);
		}
		return goal;
	}
	/*
	private int error1(List<Integer> crom) {
		// Restriccion 1: La cantidad enviada a cada destino debe superar la demanda mínima
		int error = 0;
		Map<Integer, Integer> cantidadEnviada = new HashMap<>();
		for (int k = 0; k < crom.size(); k++) {
			int i = k / DatosProductosDestinos.getM();
			int j = k % DatosProductosDestinos.getM();
			int minimo = DatosProductosDestinos.getDestino(j).demandaMinima();
			if (crom.get(k) < minimo) {
				// Penalizamos relativo a cuanto se ha equivocado el cromosoma elevando al
				// cuadrado el error
				error += Math.pow(minimo - crom.get(k), 2);
			}
		}
		return error;
	}
	*/
	
	private int error1(List<Integer> crom) {
		// Restriccion 1: La cantidad enviada a cada destino debe superar la demanda mínima
		// Restriccion 2: La cantidad total enviada de cada tipo de producto no puede exceder la cantidad disponible
		int error = 0;
		Map<Integer, Integer> cantidadEnviadaProducto = new HashMap<>();
		Map<Integer, Integer> cantidadEnviadaDestino = new HashMap<>();
		for (int k = 0; k < crom.size(); k++) {
			int i = k / DatosProductosDestinos.getM();
			int j = k % DatosProductosDestinos.getM();
			
			if (cantidadEnviadaProducto.containsKey(i)) {
				cantidadEnviadaProducto.put(i, cantidadEnviadaProducto.get(i) + crom.get(k));
			} else {
				cantidadEnviadaProducto.put(i, crom.get(k));
			}
			
			if (cantidadEnviadaDestino.containsKey(j)) {
				cantidadEnviadaDestino.put(j, cantidadEnviadaDestino.get(j) + crom.get(k));
			} else {
				cantidadEnviadaDestino.put(j, crom.get(k));
			}
		}
		
		for (int i : cantidadEnviadaProducto.keySet()) {
			if (cantidadEnviadaProducto.get(i) > DatosProductosDestinos.getUdsProducto(i)) {
				error += Math.pow(cantidadEnviadaProducto.get(i) - DatosProductosDestinos.getUdsProducto(i), 2);
			}
		}
		
		for (int j : cantidadEnviadaDestino.keySet()) {
			if (cantidadEnviadaDestino.get(j) < DatosProductosDestinos.getDestino(j).demandaMinima()) {
				error += Math.pow(DatosProductosDestinos.getDestino(j).demandaMinima() - cantidadEnviadaDestino.get(j),2);
			}
		}
		return error;
	}

	@Override
	public Double fitnessFunction(List<Integer> crom) {
		// TODO Auto-generated method stub
		int goal = 0; double penal = 0.0;
		goal = objetivo(crom);
		penal = error1(crom);// + error2(crom);
		return -goal - 3*penal;
	}


	@Override
	public SolucionProductosDestinos solucion(List<Integer> crom) {
		// TODO Auto-generated method stub
		return SolucionProductosDestinos.of(crom);
	}



}
Editor is loading...
Leave a Comment