title: Python Genetic Algorithm: Setting Functions for Usage
body: <p>I am experimenting with python about genetic algorithm. In other words, I want to make a program to simulate random process and control come parts (like experiment) to play around with variables and outcomes.</p>
  <p>For my program, I am simulating rat reproduction and controlling its population to create superior (in my case, heavier) rats at the end. In specific, I want to have starting population of rats, randomly pair two for reproduction (in which offspring will have average weight of two parent rats) and control offspring population and so on. In simple words, I will have x population of rats (initial and constant), create offsprings, list them by weight, kill from the bottom (lightest rats) to lower the offspring population == initial population and repeat the process to eventually create heavier rats.</p>
  <p>I know this experiment does not reflect real-life example but I want to initialize with this program and maybe make modifications or applications to other usages.</p>
  <p>Sorry for the long introduction. Now, before writing the actual program, I want to create functions (using def command) to make actual codes easier to write. So far, I've come up with this:</p>
  <pre><code>import random
  
  #Variables (Stop at (1) period of time or (2) above standard)
  #number_rats = #initial population (standard)
  min_offspring = 2
  max_offspring = 10
  #weight_rat = #Avg. of parents' weight (consider as single gender)
  #mutation = #Randomly reduce the weight of small number of offspring (how many and how 
  much)
  
  def breeding(female, male):
      #random # of offspring and random weight for each
      offspring = []
      number_offspring = random.randint(min_offspring, max_offspring)
      for i in range(0, number_offspring, 1):
           offspring.append(random.triangular(female, male))
      return offspring
  
  def #random pair
  
  def # cut offspring population to number_rats (list from highest weight to lowest and 
  cut light rats)
  
  def # negative mutation (maybe inside breeding function) 10% chance of mutation that 
  decreases weight by 10%.
  </code></pre>
  <p>I have tried to complete the function for breeding to take 2 rats (I labeled male and female but I decided that any 2 rats are fine to lessen the complication) and they would produce random numbers of offsprings.</p>
  <p>However, I am stuck on creating function for (1) taking random pairs from number_rats, (2) cutting huge number of offsprings to == number_rat, and (3) creating negative mutation when reproducing. It would help me a lot to help me write these functions in concise ways.</p>
  <p>I know this is long post with incomplete work but helping me with this initial stage would greatly help me. I am sorry if this is not advanced work.</p>
  <p>Anyways, thanks in advance.</p>