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: [{},{}]
..........
}