Professional Documents
Culture Documents
Ec 03 2023
Ec 03 2023
Evolutionary Computation
EE-6950, Fall 2023
Lecture # 3 - EC Origins & Motivation
Outline
I. Review Lecture #2 + some additional Python tips
2
I. Review Lecture #2
+ some additional Python tips
Review Lecture #2
• Lists:
- Sorting
- Slicing
- Comprehensions
• Mutable type noob gotcha's
- Variable assignment
- Copy vs. Deepcopy
- Function parameter passing
• Conventional main program structure
List sorting
6
ff
Lists: Slicing
7
fi
List, dict and set Comprehensions
8
Variable Assignment
• Be careful!
Assignment does not create copies!
>>> l1 = [1,2,3]
>>> l2 = l1
>>> l2[1] = -99
>>> l1
[1, -99, 3]
>>> id(l1) == id(l2)
True
9
Copy & Deepcopy
• Sometimes you will need to copy arbitrary, complex objects
• There is a special module for this, the “copy” module:
- import copy
• Why deepcopy?
Trivial example, list containing a list:
>>> x=[[1,2,3],4,5]
>>> y=copy.copy(x)
>>> y[0][0]=9
>>> x
>>> [[9,2,3],4,5] # oops!
>>>
>>> x=[[1,2,3],4,5]
>>> y=copy.deepcopy(x)
>>> y[0][0]=9
>>> x
>>> [[1,2,3],4,5] # ok!
>>> y
>>> [[9,2,3],4,5]
10
Function Parameters: Pass by value or reference…?
11
Simple Main program example
# example_main.py
import optparse
import sys
try:
# get command-line options
#
parser = optparse.OptionParser()
parser.add_option("-q", "--quiet", action="store_true", dest="quietMode", help="quiet mode", default=False)
(options, args) = parser.parse_args(argv)
if not options.quietMode:
print('Main Completed!')
if __name__ == '__main__':
main() 12
__name__ == ‘__main__’, what?
• When running Python programs from the command line, the interpreter will
automatically set the “magic” variable __name__ to ‘__main__’
• This also prevents the main() routine from running automatically when importing
the main Module ( le). This is usually what you want, particularly when
implementing automated tests or automated run scripts (in this case, you
explicitly call the main function)
13
fi
fi
Command line option parsing: optparse (or argparse)
• You will notice in the previous main program that we are importing and
instantiating an “optparse” object
• Optparse (or also more recently “argparse”) is a built-in module that allows
easy management and parsing of command-line arguments
• You can easily create command line arguments that are boolean (on/o ),
expect a value (e.g., a number, string), or expect a choice from a set of values
14
sys.argv command line arguments
• It contains the list of command line arguments passed to your python program
(as a Python list)
• The rst item is the name of the Python le (or program) that you are running
• Example:
python3 MyProgram.py –inputFile DataFile.txt –q
sys.argv=> ['MyProgram.py', '–inputFile', 'DataFile.txt', '–q']
15
fi
fi
II. HW #01 Solution Review
Homework Solution Review
• HW #01
• They are extremely powerful computational algorithms that have had much
success in the areas of global optimization, ML, modeling and classi cation.
19
Motivation for evolutionary computing
• Nature has always served as a source of inspiration for engineers and scientists
20
fi
Other Nature-inspired algorithms.
21
fi
fi
Evolutionary Computing: The Origins
• Historical perspective
• Biological inspiration:
- Darwinian evolution theory (simpli ed!)
- Genetics (simpli ed!)
22
fi
fi
Historical perspective
23
Darwinian Evolution: Survival of the fittest
24
Darwinian Evolution: Survival of the fittest
25
fi
fi
Darwinian Evolution: Terminology
• phenotype
- the set of observable characteristics of an individual resulting from the
interaction of its genotype (genetic code, DNA) with the environment
26
Darwinian Evolution: Terminology
Phenotype??!
27
Darwinian Evolution: Diversity drives change
• Phenotypic traits:
- Behavior / physical di erences that a ect response to environment
- Partly determined by inheritance, partly by factors during development
- Unique to each individual, partly as a result of random changes
• If phenotypic traits:
- Lead to higher chances of reproduction
- Can be inherited
Then they will tend to increase in subsequent generations,
leading to new combinations of useful traits …
28
ff
Darwinian Evolution: Summary
29
Darwinian Evolution: Environment/Landscape
30
fi
Adaptive landscape metaphor (Wright, 1932)
31
ff
Adaptive landscape metaphor
32
Adaptive landscape metaphor
33
Genetics: Terminology
• genotype
- the genetic code (i.e., DNA) for individuals
34
Genetics: Terminology
Internal code/state
35
Genetics: Natural
• The information required to build a living organism is encoded in its DNA
• Changes in the genotype lead to changes in the organism (e.g., height, hair color)
36
Genetic Variation: Reproduction
37
Genetic Variation: Crossover/recombination (reproductive cell splitting, meiosis)
38
Genetic Variation: Mutation
• Occasionally some of the genetic material changes very slightly during this
process (replication error)
• This means that the child might have genetic material information not
inherited from either parent
• This can be
- Catastrophic: o spring not viable (most likely)
- Neutral: new feature don’t in uence tness
- Advantageous: strong new feature occurs
39
ff
fl
fi
Connection with Evolutionary Algorithms
• Population of individuals
/Crossover
41
Basic EA Pseudo-Code
42
Connection with EA: A simple 1-D example
f(x) = 50 − x 2
43
Connection with EA: A simple 1-D example
44

Connection with EA: A simple 1-D example
• The Population:
45
Example EA in Action - 1-D optimization
46
Example EA in Action - 1-D optimization
Interactive demo…
47
A note on terminology moving forward…
48
IV. Homework #02
- Due by Sept 28 @ noon
- Submit to iLearning
Homework Assignment – Fun with PRNG's!
In this HW, use the built-in random number class in Python to generate random number
sequences, then view their distributions using matplotlib:
1. Using a uniform distribution: Generate a sequence of 10,000 random real numbers on the interval [0.0, 1.0], then
plot a histogram of the results using matplotlib.
2. Using a normal (Gaussian) distribution: Generate a sequence of 10,000 random real numbers with mean=5.0,
stddev=2.0, then plot a histogram of the results using matplotlib.
3. Make a dice-roll generator that can generate summed multiple dice rolls as follows 2d6 (2 dice rolls, 6-sided dice,
sum the two dice), 1d12 (1 dice roll, 12-sided dice), 2d10 and 1d20. Generate 10,000 trials for each case and
plot the resulting distributions using matplotlib.
50
Homework Assignment – Fun with PRNG's!
In this part, start with the provided Fun_w_prng.py le. We use YAML & Optparse to read in data
con gurations for two unequal dice, then view their distribution with matplotlib. (It’s your choice
how to combine the previous code with this le. If you have multiple les, please hand in your
code as a .zip le on iLearning)
4. Here the dice are no longer equal! We now consider two die with unequal probabilities. One has six sides with
numbers from 1 to 6, the other has ve sides with odd numbers from 1 to 9. Both die use the same probability
equation — the probability for each side is the number on that side divided by the sum of all sides. Roll the dice
and compute the total sum for each trial. Generate 10,000 trials and plot the resulting distribution using matplotlib.
51
fi
fi
fi
fi
Homework assignment
Very important!
hw2_your_student_id#.py
52
fi
Supplemental Information for HW#02
Random Numbers in Algorithms
54
Randomness in Search Algorithms
Consider this 1-D optimization problem again. What if we try…
- Gradient descent
- Random walk
- Some combination of these two approaches
f(x) Initial
state
Gradient Descent
55
Generating Random Numbers in Python
• Python has a very high quality built-in PRNG class based on the Mersenne-
Twister algorithm
• A few other common libraries have similar high-quality PRNG API's, such as
NumPy, TensorFlow, PyTorch
56
Generating Random Numbers in Python
• Can invoke a prng object using the built-in library as follows:
>>> from random import Random
>>> prng=Random()
>>> prng.seed(123)
>>>
>>> prng.random()
0.052363598850944326
>>>
• Many useful methods for generating various distributions (uniform, normal, etc.),
random integers, shu ing sequences, selecting random samples from
collections, etc.
• To nd out more about which functions are available & how to use them:
>>> from random import Random
>>> help(Random)
57
fi
ffl
Matplotlib Data Plotting Package
Matplotlib (https://matplotlib.org)
59
Getting started with Matplotlib
60
Matplotlib, a simple example plot
plt.plot(x,y)
plt.xlabel('x')
plt.ylabel('Square root of x')
61
Next week…
EC Components!