Genetic Algorithm Optimization - Node.Js

Asked

Viewed 223 times

0

I’m transcribing a Java genetics algorithm for Javascript (Node.Js), but I’m having memory and optimization problems.

Context of the problem: it’s an algorithm that the company I work for 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.

Objective of the algorithm: Tend to arrive in combination with lower purchase price (approximately. Remember, it is a genetic algorithm) to buy a "deck" of cards in several stores, taking into account the price of the card and the freight price charged by a store.

Algorithm steps:

  • Pick up chart details (price and stock in each store)
  • Pick up shop details (freight price)
  • Pick up order/deck information (card id and quantity to buy)
  • Generate population (here is the problem)
  • Mutate in the population
  • Show the best chromosome generated in mutation

Generate population: - Generate X chromosomes, where each chromosome gene is the Id_loja x Id_carta combination

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 of the file
  • Clone the letter
  • "do-while" iterates how many units the letter[i] asks for
  • picks up the ID of a random store
  • check if this store has the letter[i] with stock and price > 0, if yes, it keeps the Store ID and the card ID in Gene. Soon after 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 couldn’t clear up my problem... this is my first question on stackoverflow

1 answer

0

I was able to solve the memory problem using

delete this.vetTempCards;

But if you have opinions on how to optimize this algorithm would be very welcome.

I’m trying to think of something using async/await

Browser other questions tagged

You are not signed in. Login or sign up in order to post.