ahmedfgad / GeneticAlgorithmPython

Source code of PyGAD, a Python 3 library for building the genetic algorithm and training machine learning algorithms (Keras & PyTorch).
https://pygad.readthedocs.io
BSD 3-Clause "New" or "Revised" License
1.88k stars 462 forks source link

How do I do it for multiple values of xi and yi? #96

Open oq-9 opened 2 years ago

oq-9 commented 2 years ago

How do I do it for multiple values of xi and yi? For example, I have:

X=
[[1.875, 1.875, 1.875, 2.5  ],
 [0.625, 3.125, 1.25 , 3.125],
 [3.125, 1.25 , 1.875, 0.625],
 [0.625, 1.25 , 0.625, 1.25 ],
 [0.625, 3.125, 0.625, 0.625]]
y=
[[3.],
[6.],
[9.],
[9.],
[6.]]

How do I calculate the weights for X that would fit y using the code above? Is there a way to calculate the error for each case and optimize accordingly from within the library?

ahmedfgad commented 2 years ago

This should be a multi-objective optimization problem. PyGAD only supports single-objective problems.

To do it in PyGAD, you should do the following:

  1. Set the number of genes in PyGAD to 20 (because X shape is 5x4=20).
  2. In the fitness function, reshape the solution into a 5x4 array.
  3. Multiply the solution by X, row by row, sum the results of each row to get only 5 outputs.
  4. Compare the 5 outputs to the y array and calculate the error for each y.
  5. Based on the 5 errors calculated, you should return a single value that represents the fitness.

I hope this helps. Please get back if further clarification is needed.

Fern-Parker commented 2 years ago

@oq-9 Hi, oq-9! Have you solved the problem? I've encountered the same problem with you. Could u share some experiences? Any help will be greatly appreciated!