Skip to content

Equation inputs #1

Closed
Closed

Description

@willstarplan

Hi Ahmed,

I'm using your code to solve a problem in my project. However, when I change the equation_inputs from [4,-2,3.5,5,-11,-4.7] to my datafile, that is a .dat with 2000 rows and 1 column, I get this error: "ValueError: operands could not be broadcast together with shapes (5,3) (3,2000)"

I'm using only three files, that's the reason of 3 in (3,2000).

I've partially solved my problem, if I use the max(datafile), but I don't find the best solution in all cases, because I'm improving only one point, instead of 2000. You can see the fitness evolution in this case, for example, in the attached figure. The fitness function is the Chi-square of one point.
graph1

Could you, please, give some insight about this particular problem?

Activity

  1. ahmedfgad commented on Dec 14, 2018

    @ahmedfgad
    Owner

    Hi,
    The problem your are looking to solve is different from that being solved by my code.

    My code has just a single solution represented as a single row to be optimized in order to maximize the fitness function. As far as I understood, your problem has more than one solution (point) to be optimized. If this is right, then you may fed the data to my code point by point. This is by looping through the 2,000 rows and feeding just a single row to the code. (Each row consists of a single point).
    Regarding the 3 files, if each file is independent from the other files, then the process might be repeated for each file. If the single row is returned from the 3 files, that is the single row consists of 3 points returned from the 3 files, then you can return such a row from the 3 files inside the loop and feed it to the code.

    It is important to change the GA parameters such as number of solutions per population and number of parents mating.

    Please let me know if my understanding is correct and whether the explanation works for you.
    Good luck.

  2. willstarplan commented on Dec 14, 2018

    @willstarplan
    Author

    Hi, thanks for replying me.

    Indeed, your original code solves a different problem, but the main idea is the same for my case.

    Although I've had the same idea (perform a loop through the 2000 rows), I didn't try yet. But I don't think this is gonna works, because in each iteration will find different weights.

    Let me explain my problem a bit better.

    Take a look in the Figure below.
    vlt_g_h2o_ga

    I have one observed data (black), and I want use a combination of three synthetic data to fit the black curve. In order to do that, I reduced the noise of black curve, which gave me the maximum y = 1.79 at x=3.05. My fitness function is the Chi-square, defined by: chi2 = ((data(x)-model(x))^2)/model(x), and the model is given by: model(x) = w1s1 + w2s2 + w3s3, where s# are the synthetic data. As my idea is reduce my chi-square, I've changed the parameters accordingly. After 1000 generations, the best result is shown in the Figure above. So the "local" chi-square at x=3.05 is 1E-4. If I use the weights for multiply the whole data, my total chi-square is 95, which is very good in this situation.

    However, as I'm optimising only the position x=3.05, sometimes the code find the best value for 'x', but not necessarily for the whole black curve, as you can see below. In this case, the "local" chi-square still 1E-4, but the total chi-square is 105, and therefore higher than my previous solution.

    figure_1

    So, in summary, I have to find w1, w2, w3 that after multiply their respective synthetic data, give me the best solution. That's the reason that I cannot do the loop for every point.

    I don't have to find 2000 solutions, but the best combination of w1, w2 and w3.

  3. ahmedfgad commented on Dec 14, 2018

    @ahmedfgad
    Owner

    Your problem is now clearer.
    You are trying to find 3 parameters (w1 to w3) that builds a model that makes the 3 synthetic data files fit the observed data. So, the suggestion of looping through each data point is not suitable.
    The solution to your problem is to change the way of calculating the fitness function. It is now calculated for a single row consisting of s1, s2, and s3. It needs to get modified to return the fitness value for the entire data. In other words, modify the cal_pop_fitness function inside the GA.py file of my code. The steps are as follows:

    1. Create a variable called total_fit, for example, and initialize it to 0.
    2. Create a loop inside the function that loops through each row of the data that consists of s1, s2, and s3. This defines the current model you suggested model(x) = w1s1 + w2s2 + w3s3.
    3. For each row, calculate the fitness value for it according to your equation chi2 = ((data(x)-model(x))^2)/model(x).
    4. Increment the total_fit variable by chi2. The value inside the total_fit valriable is the fitness value for the entire data based on the currently selected parameters s1:s3.

    Please let me know if you have any questions.

    By the way, I wrote about genetic algorithm in my book titled Practical Computer Vision Applications Using Deep Learning with CNNs available at Amazon here https://www.amazon.com/Practical-Computer-Vision-Applications-Learning/dp/1484241665.

    Regards,
    Ahmed

  4. willstarplan commented on Dec 14, 2018

    @willstarplan
    Author

    Thank you very much Ahmed.

    I'll try to do this, and let you know about the progress. Your code was my first contact with GA, so I'm sorry if I ask some naive question :-).

  5. ahmedfgad commented on Dec 14, 2018

    @ahmedfgad
    Owner
  6. self-assigned this
    on May 4, 2020
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

Labels

questionFurther information is requested

Projects

No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions