How to fill an array with respect to the values of another javascript array?

0

I am doing the genetic algorithm of a flock of sheep. It turns out that from this exercise I generated a population that would be an array of objects.

Population example, of course for this problem you must generate more objects, but this uses a minimum amount just as an example:

poblacion: [
  {nombre: "Oveja 1", peso: 147, precio: 4026}
  {nombre: "Oveja 2", peso: 168, precio: 3975}
  {nombre: "Oveja 3", peso: 106, precio: 1642}
  {nombre: "Oveja 4", peso: 145, precio: 3639}
  {nombre: "Oveja 5", peso: 117, precio: 1838}
  {nombre: "Oveja 6", peso: 151, precio: 2355}
]

From this population I must create other arrangements fulfilling the condition that the sum of the weight values does not exceed 300, following this example the following arrangements should be as follows:

camion1: [
    {nombre: "Oveja 1", peso: 147, precio: 4026}
  ]

camion2:[
    {nombre: "Oveja 2", peso: 168, precio: 3975}
    {nombre: "Oveja 3", peso: 106, precio: 1642}
  ]

camion3:[
    {nombre: "Oveja 4", peso: 145, precio: 3639}
    {nombre: "Oveja 5", peso: 117, precio: 1838}
  ]

camion4:[
    {nombre:  "Oveja 6", peso: 151, precio: 2355}
  ]

As you can see the arrays truck1, truck2, truck3 and truck4 have the objects of the population array with the difference that when adding the property weight does not exceed 300. How could I create those subarrays ?, for the moment I have this:

var poblacion = [];
var camion = [];
var objCromosomas = {};

var maxpesoAleatorio = 200;
var minpesoAleatorio = 50;

var maxprecioAleatorio = 5000;
var minprecioAleatorio = 1500;

var pesoCamion = 2000;
var sumaPeso = 0;
var sumapesoIdeal = 0;

for (var i = 1; i<190; i++) {
    var objOveja = new Object();
    var pesoAleatorio = Math.round(Math.random() * (maxpesoAleatorio - minpesoAleatorio) + minpesoAleatorio);
    var precioAleatorio = Math.round(Math.random() * (maxprecioAleatorio - minprecioAleatorio) + minprecioAleatorio);

    objOveja.nombre = "Oveja "+i;
    objOveja.peso   = pesoAleatorio;    
    objOveja.precio = precioAleatorio;
    poblacion.push(objOveja);
}
angular.forEach(poblacion, function(value, key){
    sumaPeso = value.peso+sumaPeso;
    if (sumaPeso < pesoCamion) {
        camion.push(value);
    }
});

 objCromosomas["camion"] = camion;
 console.log(objCromosomas);

In the end my result should look like this:

{
 nombre: "Cromosomas",
 camion1: [{},{}],
 camion2: [{},{}]
 ..........
}
    
asked by JG_GJ 10.05.2018 в 00:34
source

2 answers

2

I think this should help you:

var camionN = 1; /*Para llevar una cuenta de los camiones*/

angular.forEach(poblacion, function(value, key){
    sumaPeso = value.peso+sumaPeso;
    sumaPrecio = value.precio+sumaPrecio
    if (sumaPeso < pesoCamion) {
        camion.push(value);

        /*En caso de que ya se haya recorrido todo el array población*/
        if(value == poblacion[poblacion.length-1]){ 
          objCromosomas["camion"+camionN] = camion;
          objCromosomas["camion"+camionN].total = sumaPrecio;
        }
    }else{ /*Cuando el camion se llene*/

        /*se agrega el array camion a objCromosomas*/ 
        objCromosomas["camion"+camionN] = camion;
        objCromosomas["camion"+camionN].total = sumaPrecio;
        camionN++;  

        camion = []; //vacío el camión para comenzar a llenarlo de nuevo
        //meto a la oveja que no cabía en el camión anterior
        camion.push(value);
        sumaPeso = value.peso;
        sumaPrecio = value.precio;
    }
});

And the complete code would be like that

var poblacion = [];
var camion = [];
var objCromosomas = {};

var maxpesoAleatorio = 200;
var minpesoAleatorio = 50;

var maxprecioAleatorio = 5000;
var minprecioAleatorio = 1500;

var pesoCamion = 2000;
var sumaPeso = 0;
var sumaPrecio = 0;
var sumapesoIdeal = 0;

for (var i = 1; i<190; i++) {
    var objOveja = new Object();
    var pesoAleatorio = Math.round(Math.random() * (maxpesoAleatorio - 
    minpesoAleatorio) + minpesoAleatorio);
    var precioAleatorio = Math.round(Math.random() * (maxprecioAleatorio - 
    minprecioAleatorio) + minprecioAleatorio);

    objOveja.nombre = "Oveja "+i;
    objOveja.peso   = pesoAleatorio;    
    objOveja.precio = precioAleatorio;
    poblacion.push(objOveja);
}

var camionN = 1; /*Para llevar una cuenta de los camiones*/

angular.forEach(poblacion, function(value, key){
    sumaPeso = value.peso+sumaPeso;
    sumaPrecio = value.precio+sumaPrecio
    if (sumaPeso < pesoCamion) {
        camion.push(value);

        /*En caso de que ya se haya recorrido todo el array población*/
        if(value == poblacion[poblacion.length-1]){ 
          objCromosomas["camion"+camionN] = camion;
          objCromosomas["camion"+camionN].total = sumaPrecio;
        }
    }else{ /*Cuando el camion se llene*/

        /*se agrega el array camion a objCromosomas*/ 
        objCromosomas["camion"+camionN] = camion;
        objCromosomas["camion"+camionN].total = sumaPrecio;
        camionN++;  

        camion = []; //vacío el camión para comenzar a llenarlo de nuevo
        //meto a la oveja que no cabia en el camión anterior
        camion.push(value);
        sumaPeso = value.peso;  
        sumaPrecio = value.precio;
    }
});

console.log(objCromosomas);
    
answered by 10.05.2018 / 06:59
source
1

I think you could create an array of arrays like this:

var camiones = [];
angular.forEach(poblacion, function(value, key){
    sumaPeso = value.peso+sumaPeso;

    if (sumaPeso < pesoCamion) {
        camion.push(value);
    } else {
        camiones.push(camion);
        camion = [];
        camion.push(value);
        sumaPeso = value.peso;
    }
});
    
answered by 10.05.2018 в 01:28