I'm transcribing a Java genetic algorithm for JavaScript (Node.Js), but I'm having memory and optimization problem.
Problem Context: This is an algorithm in which the company where I work is trying to implement. The problem is that the algorithm I have is in Java, and they need it to be in PHP. But for reasons of adaptation and knowledge I'm trying to implement in JavaScript.
Algorithm goal: To try to arrive at the combination with the lowest purchase price (approximately, remember, it's a genetic algorithm) to buy a "deck" of cards in several stores, taking into consideration the price of the card and the freight charge charged by a store.
Algorithm Steps:
- Get card data (price and inventory in each store)
- Get store data (freight rate)
- Get order / pack information (card id and quantity to buy)
- Generate population (here's the problem)
- Mutate population
- Show the best chromosome generated in the mutation
Generate population: - Generate X chromosomes, where each gene of the chromosome is the combination Id_loja x Id_carta
Gene Generation:
gerarGenes(vetCards, vetLojas, pedido) {
let cont = 0;
for (let i = 0; i < pedido.getVetCodigo().length; i++) {
this.vetTempCards[i] = Cromossomo.clonar(vetCards[pedido.getPosVetCodigo(i)]);
let j = 0;
do {
const posicaoAleatoria = parseInt(Math.random() * vetLojas.length);
if (
this.vetTempCards[i].getPosVetQtd(posicaoAleatoria) > 0 &&
this.vetTempCards[i].getPosVetPreco(posicaoAleatoria) > 0
) {
this.matGene[0][cont] = posicaoAleatoria;
this.matGene[1][cont] = pedido.getPosVetCodigo(i);
this.vetTempCards[i].decVetQtd(posicaoAleatoria);
cont++;
j++;
}
} while (j < pedido.getPosVemNumComprar(i));
}
}
Clarification:
- "for" iterates each letter from the file
- Clone the letter
- "do-while" iterates how many units the letter [i] asks for
- get the ID of a random store
- check if this store has the letter [i] with stock and price > 0, if yes, save the Store ID and the letter ID on the Gene. Soon afterwards it decreases the stock of the letter in this store
Node Error:
<--- Last few GCs --->
[21267:0x372d870] 16966 ms: Scavenge 1396.5 (1422.7) -> 1395.9 (1423.2) MB, 2.1 / 0.0 ms (average mu = 0.186, current mu = 0.117) allocation failure
[21267:0x372d870] 16972 ms: Scavenge 1396.7 (1423.2) -> 1396.1 (1423.7) MB, 3.6 / 0.0 ms (average mu = 0.186, current mu = 0.117) allocation failure
[21267:0x372d870] 16979 ms: Scavenge 1396.9 (1423.7) -> 1396.3 (1424.7) MB, 2.6 / 0.0 ms (average mu = 0.186, current mu = 0.117) allocation failure
<--- JS stacktrace --->
==== JS stack trace =========================================
0: ExitFrame [pc: 0x1cc9fff5be1d]
Security context: 0x1a3769f9e6e1 <JSObject>
1: clonar [0x287063151361 [/home/.../Genetica/classes/Cromossomo.js:~94 [pc=0x1cc9ffff5650](this=0x09d1fad477b1 <JSFunction Cromossomo (sfi = 0x64dcbcdd719)>,obj=0x0b60ffd23c99 <JSArray[87]>)
2: clonar [0x287063151361] [/home/.../Genetica/classes/Cromossomo.js:~94] [pc=0x1cc9ffff5812](this=0x09d1fad477b1 ...
FATAL ERROR: Ineffective mark-compacts near heap limit Allocation failed - JavaScript heap out of memory
<Mais mensagens .....>
Abortado (imagem do núcleo gravada)
I'm sorry if I could not clarify my problem ... this is my first question in stackoverflow