Download as pdf or txt
Download as pdf or txt
You are on page 1of 62

演化式計算

Evolutionary Computation
EE-6950, Fall 2023
Lecture # 3 - EC Origins & Motivation
Outline
I. Review Lecture #2 + some additional Python tips

II. HW#01 Solution Review

III. EC Origins & Motivation

IV. Homework #02

2
I. Review Lecture #2
+ some additional Python tips
Review Lecture #2

• Let's quickly review some of the important points from Lecture #2 on


Python Programming…
Some additional Python tips

• Lists:
- Sorting
- Slicing
- Comprehensions
• Mutable type noob gotcha's
- Variable assignment
- Copy vs. Deepcopy
- Function parameter passing
• Conventional main program structure
List sorting

• In addition to the list method .sort():


- a_list.sort()

• There is also a standalone function for sorting: sorted(…)


- sorted(a_list)

• What’s the di erence?


- The .sort() method sorts the existing list in-place
- The sorted() method leaves the original list alone, returns a new sorted list

6
ff
Lists: Slicing

• Python lists natively support rich indexing,


known as "slicing"
>>> x = [0,1,2,3,4,5,6,7,8]
• Returns a sub-sequence from a list >>> x[2:5]
[2, 3, 4]
• Slicing indexing format is as follows: >>> x[1:6:2]
[1, 3, 5]
- [start:stop:step] >>>
- step is optional (defaults to 1)
- stop is nal index+1

7
fi
List, dict and set Comprehensions

>>> d={i:i**2 for i in range(5)} Dict comprehension {x:y}


>>> d
{0: 0, 1: 1, 2: 4, 3: 9, 4: 16}
>>>
>>> l=[i**2 for i in range(5)] List comprehension []
>>> l
[0, 1, 4, 9, 16]
>>>
>>> s={c for c in "abcdef"} Set comprehension {}
>>> s
{'e', 'f', 'b', 'd', 'a', 'c'}
>>>

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

• It includes two useful functions


- copy.copy(…)
- copy.deepcopy(…)

• 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…?

• Function parameters pass object references, >>> def func(x):


not independent object copies ... return id(x)
>>> a=456
• Be careful when passing mutable types! >>> id(a)
- Lists 4449449776
>>> func(a)
- Dicts 4449449776
- Class object instances
- Mutable containers >>> def func(x):
... x[0]=-999
- Etc. ...
>>> a=[0,1,2,3]
>>> func(a)
>>> a
[-999, 1, 2, 3]
>>>

11
Simple Main program example
# example_main.py
import optparse
import sys

# Main entry point


#
def main(argv=None):
if argv is None:
argv = sys.argv

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)

#do something here...


print("This program is boring!")

if not options.quietMode:
print('Main Completed!')

except Exception as info:


if 'options' in vars() and not options.quietMode:
raise #re-raise Exception, interpreter shows stack trace
else:
print(info)

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 is a convenient for implementing main programs. It allows the interpreter to


nd and run your main() entry point automatically

• 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

• sys.argv is a variable that is automatically set by the system (similar to C/C++)

• 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

• Solution has also been uploaded to iLearning


III. EC Origins & Motivation
Recap, what are Evolutionary Algorithms…

• Evolutionary algorithms are stochastic search algorithms inspired by the ideas


of genetic propagation and Darwinian evolution and selection.

• 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

• The best problem solvers known in nature are:


- The (human) brain
 Neuro-computing (arti cial neural networks)

- The evolution mechanism that created the human brain


 Evolutionary computing

20

fi
Other Nature-inspired algorithms.

• EC’s are not the only nature-inspired algorithms…


- Particle swarm optimization
- Ant colony algorithms
- Arti cial neural networks
- Arti cial immune system algorithms
- Simulated annealing
- Quantum computing

• If you’re interested in a broad, but not overly deep


overview of these areas:
- “Natural Computing Algorithms”, Brabazon, et al,
Springer, 2015

21
fi
fi
Evolutionary Computing: The Origins

• Historical perspective
• Biological inspiration:
- Darwinian evolution theory (simpli ed!)
- Genetics (simpli ed!)

22
fi
fi
Historical perspective

• 1948, Turing: Proposes “Genetical or Evolutionary search”


• 1964, Bremermann & Rechenberg: Evolution Strategies
• 1965, L. Fogel, Owens and Walsh: Evolutionary programming
• 1975, Holland: Genetic algorithms
• 1992, Koza: Genetic programming
• Late 90's, Deb & Goldberg: Multi-objective EA's
• 1990's – 2000's, De Jong & many others: Synergistic fusion and uni cation

23
Darwinian Evolution: Survival of the fittest

24
Darwinian Evolution: Survival of the fittest

• All environments have nite resources


- (i.e., can only support a limited number of individuals)

• Life forms have basic instinct/lifecycles geared towards reproduction


• Therefore some kind of selection is inevitable
• Those individuals that compete for the resources most e ectively
have increased chance of reproduction

• (Note: tness in natural evolution is a derived, secondary measure)

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

- Examples: hair color, eye color, height

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

• Population consists of diverse set of individuals

• Combinations of traits that are better adapted tend to increase


representation in population
- Individuals are “units of selection”

• Variations occur through random changes yielding constant source of


diversity, coupled with selection means that:
- Population is the “unit of evolution”

• Note the absence of any “guiding force”

29
Darwinian Evolution: Environment/Landscape

• Notion of “ tness” is related to interaction with the environment

30
fi
Adaptive landscape metaphor (Wright, 1932)

• Can envisage population with n traits as existing in a n+1-dimensional space


(landscape/environment) with height corresponding to tness

• Each di erent individual (phenotype) represents a single point on the


landscape

• Population is therefore a “cloud” of points, moving on the landscape over


time as it evolves – adaptation

31
ff
Adaptive landscape metaphor

32
Adaptive landscape metaphor

• Selection “pushes” population up the landscape

• Genetic drift & Random variations in feature distribution


- Can allow population to descend hills, cross valleys, and leave local optima

33
Genetics: Terminology

• genotype
- the genetic code (i.e., DNA) for individuals

34
Genetics: Terminology

• genotype -> phenotype

External observable trait/state

Internal code/state

35
Genetics: Natural
• The information required to build a living organism is encoded in its DNA

• Genotype (DNA inside) determines phenotype

• We can think of this more abstractly as:


- Encoding (genotype) results in expressed Traits (phenotype)

• Genes  Phenotypic traits is a complex mapping

• Changes in the genotype lead to changes in the organism (e.g., height, hair color)

36
Genetic Variation: Reproduction

Cell from Father Cell from Mother

New Individual’s cell (child)

37
Genetic Variation: Crossover/recombination (reproductive cell splitting, meiosis)

• Chromosome (DNA) pairs align and duplicate


• Inner pairs link and swap parts of themselves

• Outcome is one copy of maternal/paternal chromosome plus


two entirely new combinations

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

• Individuals have a state representation


- Can be thought of as their genetic code (genotype/phenotype)

• Individuals have a tness (a measure of quality)


- Derived from individual’s state

• Variation operators for Individual’s state (inspired by natural genetics)


- Crossover (recombination, mating)
- Mutation
 Push towards novelty (exploration)

• Selection mechanisms towards higher tness  “survival of the ttest”


40
fi
fi
Basic Evolutionary Algorithm (EA)

/Crossover

41
Basic EA Pseudo-Code

42
Connection with EA: A simple 1-D example

• Let’s imagine a simple 1-D Optimization problem. Maximize:

f(x) = 50 − x 2

43
Connection with EA: A simple 1-D example

• The state/genotype/phenotype: The real number, x


- Genotype and phenotype are equivalent in this case

• Fitness landscape: f(x) = 50 − x 2


- Fitness function maps internal state to landscape
- (Note: also known as the “objective” function in optimization)

• Some possible ideas for variation operators on individuals:


• Crossover/recombination: x′ = (x1 + x2)/2 x
• Mutation: Random choice from Gaussian distribution centered around x

44

Connection with EA: A simple 1-D example

• The Population:

45
Example EA in Action - 1-D optimization

Initial Generation Intermediate Generation Final Generation

Many local minima One Global Minimum

Note: Multiple local minima can “trap” simple hill-climbing algorithms

46
Example EA in Action - 1-D optimization

Interactive demo…

47
A note on terminology moving forward…

• This overview, including various biological genetics terminology was to show


inspiration & motivation for the algorithmic approach

• Moving forward will generally avoid biological terminology (genotype,


phenotype, etc.) and use more straightforward language
• e.g. “state” instead of “genotype”

• However, it’s still good to be familiar with genetics terminology as it is


frequently used in EC journal papers and books.

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.

(can anyone guess what game motivated #3? ;-)

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!

Your program source code le should be named as follows:

hw2_your_student_id#.py

This will make your TA's life much easier ☺

52

fi
Supplemental Information for HW#02
Random Numbers in Algorithms

A few questions for consideration…

• Why are random numbers useful in search algorithms?

• How do we generate random numbers in Python programs?

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

• Typically use so-called "Pseudo-random Number Generators", also known as


"PRNG"s

• PRNGs are deterministic algorithms that generate sequences of numbers


having desired random number distributions (uniform, normal, etc.)

• 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

• Basic Matplotlib pyplot tutorial (online):


https://matplotlib.org/tutorials/introductory/pyplot.html

• Sample plotting examples with associated code:


https://matplotlib.org/tutorials/introductory/sample_plots.html

• Gallery of many useful data plotting examples with associated code:


https://matplotlib.org/gallery/index.html

60
Matplotlib, a simple example plot

#Simple Matplotlib example - 2-D line plot


#

import matplotlib.pyplot as plt


import math

#plot sqrt of integer sequence


x=[i for i in range(100)]
y=[math.sqrt(i) for i in x]

plt.plot(x,y)
plt.xlabel('x')
plt.ylabel('Square root of x')

plt.savefig('sqrt_plot.png') #write png


file
plt.show() #show on screen

61
Next week…
EC Components!

You might also like