eldemonio2
unknown
java
a year ago
1.8 kB
4
Indexable
package pi4_BLR1171.util; import java.util.ArrayList; import java.util.HashMap; import java.util.List; import java.util.Map; import pi4_BLR1171.ejercicios.datos.DatosProductosDestinos; public class SolucionProductosDestinos { private Map<Integer, List<Integer>> solucion = new HashMap<>(); private List<List<Integer>> cromosoma = new ArrayList<>(); private Integer costeTotal= 0; public static SolucionProductosDestinos of(List<Integer> crom) { return new SolucionProductosDestinos(crom); } public SolucionProductosDestinos(List<Integer> crom) { /* Tenemos una lista de n*m elementos de manera que [x_01, x_02, ... x_0m, x_11,x_12, ... x_1m, ... x_n1, x_n2, ... x_nm], es decir x_ij es el número de unidades del producto i que se envían al destino j Podemos obtener la i con la operación entera i = k/m y la j con la operación módulo j = k%m siendo k el índice del cromosoma */ int m = DatosProductosDestinos.getM(); for (int k = 0; k < crom.size(); k++) { int i = k / m; int j = k % m; costeTotal += crom.get(k) * DatosProductosDestinos.getCosteAlmacenamiento(i, j); } //Cortamos la lista de cromosomas en sublistas de tamaño m cromosoma = crom.stream().collect(ArrayList::new, (list, item) -> { List<Integer> last = list.isEmpty() ? null : list.get(list.size() - 1); if (last == null || last.size() == DatosProductosDestinos.getM()) list.add(last = new ArrayList<>()); last.add(item); }, ArrayList::addAll); for(int i = 0; i<cromosoma.size();i++) { solucion.put(i, cromosoma.get(i)); } } public String toString() { System.out.println("RAW: "); cromosoma.forEach(System.out::println); return String.format("Productos y cantidades: %s, Coste total: %d", solucion, costeTotal); } }
Editor is loading...
Leave a Comment