ml-finance-python

python scripts for finance machine learning

git clone https://9o.is/git/ml-finance-python.git

genetic_algorithm.py

(1333B)


      1 
      2 from mlfromscratch.unsupervised_learning import GeneticAlgorithm
      3 
      4 def main():
      5     target_string = "Genetic Algorithm"
      6     population_size = 100
      7     mutation_rate = 0.05
      8     genetic_algorithm = GeneticAlgorithm(target_string,
      9                                         population_size,
     10                                         mutation_rate)
     11 
     12     print ("")
     13     print ("+--------+")
     14     print ("|   GA   |")
     15     print ("+--------+")
     16     print ("Description: Implementation of a Genetic Algorithm which aims to produce")
     17     print ("the user specified target string. This implementation calculates each")
     18     print ("candidate's fitness based on the alphabetical distance between the candidate")
     19     print ("and the target. A candidate is selected as a parent with probabilities proportional")
     20     print ("to the candidate's fitness. Reproduction is implemented as a single-point")
     21     print ("crossover between pairs of parents. Mutation is done by randomly assigning")
     22     print ("new characters with uniform probability.")
     23     print ("")
     24     print ("Parameters")
     25     print ("----------")
     26     print ("Target String: '%s'" % target_string)
     27     print ("Population Size: %d" % population_size)
     28     print ("Mutation Rate: %s" % mutation_rate)
     29     print ("")
     30 
     31     genetic_algorithm.run(iterations=1000)
     32 
     33 if __name__ == "__main__":
     34     main()