Paradigmas de programación

Escuela Politécnica Nacional

¡Bienvenidos!

MSc. Adrián Égüez

Escuela Politécnica Nacional

Resultados de aprendizaje

MSc. Adrián Égüez

Discriminar los diferentes paradigmas de programación y sus campos de aplicación. 

Conocimiento

Escuela Politécnica Nacional

Resultados de aprendizaje

MSc. Adrián Égüez

Abstraer problemas de tal manera que le permitan crear soluciones basadas en uno o varios paradigmas de programación.

Destrezas

Escuela Politécnica Nacional

Resultados de aprendizaje

MSc. Adrián Égüez

Practicar la ética investigativa, responsabilidad social y rigurosidad científica en la generación de prototipos y asesoría sobre los paradigmas, metodologías y lenguajes de programación demostrando honestidad, responsabilidad, dedicación y colaboración.

Valores y Actitudes

Escuela Politécnica Nacional

Capítulo 1: Paradigmas de Programación

MSc. Adrián Égüez

¿Qué es paradigma? 

Conceptos - Paradigma

An example that serves as pattern or model.

A pattern that serves as a school of thoughts for programming of computers

Escuela Politécnica Nacional

Capítulo 1: Paradigmas de Programación

MSc. Adrián Égüez

¿Qué es una técnica de programación? 

Conceptos - Técnica de Programación

Related to an algorithmic idea for solving a particular class of problems

EX: Divide and conquer

Escuela Politécnica Nacional

Capítulo 1: Paradigmas de Programación

MSc. Adrián Égüez

Conceptos - Técnica de Programación

EX: Program development by stepwise refinement

Escuela Politécnica Nacional

Capítulo 1: Paradigmas de Programación

MSc. Adrián Égüez

Conceptos - Técnica de Programación

In each step, one or several instruction of the given program are decomposed into more detailed instructions. This successive decomposition or refinement of specifications terminates when all instructions are expressed in terms of an underlying computer or programming language, and must therefore be guided by the facilities available on that computer or language.

Escuela Politécnica Nacional

Capítulo 1: Paradigmas de Programación

MSc. Adrián Égüez

Conceptos - Estilo de programación

¿Qué es estilo de programación? 

The way we express ourselves in a computer program

Related to elegance or lack of elegance

Escuela Politécnica Nacional

Capítulo 1: Paradigmas de Programación

MSc. Adrián Égüez

Conceptos - Cultura de programación

¿Qué es cultura de programación? 

The totality of programming behavior, which often is tightly related to a family of programming languages

The sum of a main paradigm, programming styles, and certain programming techniques

Escuela Politécnica Nacional

Capitulo 1: Paradigmas de Programación

MSc. Adrián Égüez

Paradigma Imperativo

First do this and next do that

Adjective:
Expressing a command or plea

Noun: Command or an order.

Escuela Politécnica Nacional

Capítulo 1: Paradigmas de Programación

MSc. Adrián Égüez

Paradigma Imperativo

Meassurable effect program state

 Basic idea is the command: ->

Order is Important

Escuela Politécnica Nacional

Capítulo 1: Paradigmas de Programación

MSc. Adrián Égüez

Paradigma Imperativo

Incremental change of the program state as a function of time

Discipline and idea

Similar to descriptions of everyday routines

Assignment, IO, procedure calls

Escuela Politécnica Nacional

Capítulo 1: Paradigmas de Programación

MSc. Adrián Égüez

Paradigma Imperativo

Fortran, Algol, Pascal, Basic, C

Escuela Politécnica Nacional

Capítulo 1: Paradigmas de Programación

MSc. Adrián Égüez

Paradigma Imperativo

Abstracts one or more actions to a procedure, which can be called as a single command

Escuela Politécnica Nacional

Capítulo 1: Paradigmas de Programación

MSc. Adrián Égüez

Paradigma Imperativo

Memory locations and programmers can modify the contents of variables using assignments

Escuela Politécnica Nacional

Capítulo 1: Paradigmas de Programación

MSc. Adrián Égüez

Paradigma Declarativo

Describe a particular class of problems itself

Are equipped with tools that allow them to abstract from details of the implementation

Concentrate on details of the problem

Escuela Politécnica Nacional

Capítulo 1: Paradigmas de Programación

MSc. Adrián Égüez

Paradigma Declarativo

Hiding implementation details can be considered a handicap for programmers because access to low-level details provides a high degree of flexibility

Escuela Politécnica Nacional

Capítulo 1: Paradigmas de Programación

MSc. Adrián Égüez

Paradigma Declarativo

 However, a lot of flexibility implies a lot of potential for errors, and, more importantly, less potential for abstraction

Escuela Politécnica Nacional

Capítulo 1: Paradigmas de Programación

MSc. Adrián Égüez

Paradigma Declarativo

 EX:

Web App C

Web App Assembly

Escuela Politécnica Nacional

Capítulo 2: Paradigma Funcional

MSc. Adrián Égüez

Conceptos

An imperative program means to execute commands

 Functional program means to evaluate expressions

Escuela Politécnica Nacional

Capítulo 2: Paradigma Funcional

MSc. Adrián Égüez

Conceptos

Functions in a functional program are functions in a mathematical sense

Escuela Politécnica Nacional

Capítulo 2: Paradigma Funcional

MSc. Adrián Égüez

Conceptos

The result of a function call depends only on the values of the arguments

Escuela Politécnica Nacional

Capítulo 2: Paradigma Funcional

MSc. Adrián Égüez

Conceptos

Imperative Programming

Functions

Output

Input

Other variables

Escuela Politécnica Nacional

Capítulo 2: Paradigma Funcional

MSc. Adrián Égüez

Conceptos

Imperative Programming

Functions

Output

Input

Other variables

SIDE EFFECTS

Changed values of other variables

Escuela Politécnica Nacional

Capítulo 2: Paradigma Funcional

MSc. Adrián Égüez

Conceptos

Functional Programming

Output

Input

NO
SIDE EFFECTS

NO
OTHER VARIABLES

Escuela Politécnica Nacional

Capítulo 2: Paradigma Funcional

MSc. Adrián Égüez

Conceptos

The result of evaluating an expression is determined only by the parts of the expression

-

Not by evaluation order

Escuela Politécnica Nacional

Capítulo 2: Paradigma Funcional

MSc. Adrián Égüez

Type polimorfism

length::[a] -> Int

The type a used as argument to the list type constructor [ ] represents an arbitrary type

Escuela Politécnica Nacional

Capítulo 2: Paradigma Funcional

MSc. Adrián Égüez

Type polimorfism

Type polymorphism allows us to use type variables that represent arbitrary types

Helps to make defined functions more generally applicable

Escuela Politécnica Nacional

Capítulo 2: Paradigma Funcional

MSc. Adrián Égüez

High order functions

Functions in a functional program can not only map data to data

Helps to make defined functions more generally applicable

f o g -> f ( g (x) )

Escuela Politécnica Nacional

Capítulo 2: Paradigma Funcional

MSc. Adrián Égüez

Lazy evaluation

Delays the evaluation of an expression until its value is needed

Can be faster than eager evaluation because unnecessary computations are skipped

Escuela Politécnica Nacional

Capítulo 2: Paradigma Funcional

MSc. Adrián Égüez

Eager Evaluation

 Most programming languages use eager evaluation 

All expressions are evaluated as soon as they are bound to a variable

Each argument is evaluated exactly once

Escuela Politécnica Nacional

Capítulo 2: Paradigma Funcional

MSc. Adrián Égüez

Characteristics

Don't support if / else / switch

Abstraction, Encapsulation, Inheritance, and Polymorphism

Use function calls

Escuela Politécnica Nacional

Capítulo 2: Paradigma Funcional

MSc. Adrián Égüez

Characteristics

Easy to test

No mutable / Parallel Programming

No state / No errors

Escuela Politécnica Nacional

Capítulo 3: Paradigma Orientado a Objetos

MSc. Adrián Égüez

Conceptos

Uses Mutable data

Follows Imperative Programming Model

Escuela Politécnica Nacional

Capítulo 3: Paradigma Orientado a Objetos

MSc. Adrián Égüez

Conceptos

Focus is on POO “How you are doing” / FP "What are you doing"

Its methods can produce serious side effects

Escuela Politécnica Nacional

Capítulo 3: Paradigma Orientado a Objetos

MSc. Adrián Égüez

Conceptos

Flow control is done using loops and conditional statements.

Its methods can produce serious side effects

Escuela Politécnica Nacional

Capítulo 3: Paradigma Orientado a Objetos

MSc. Adrián Égüez

Conceptos

 POO Loop vs FP Recursion for iterate Collection Data

Execution order of statement is very important

Escuela Politécnica Nacional

Capítulo 4: Programación Genética

MSc. Adrián Égüez

Conceptos

Automatic programming

Method to evolve computer programs

Escuela Politécnica Nacional

Capítulo 4: Programación Genética

MSc. Adrián Égüez

Conceptos

The most potential as a way to automatically write computer programs

Give the computer a problem and ask the computer to build a program to solve it

Escuela Politécnica Nacional

Capítulo 4: Programación Genética

MSc. Adrián Égüez

Conceptos

Survival of the fittest

Follows Darwin’s theory of evolution

Escuela Politécnica Nacional

Capítulo 4: Programación Genética

MSc. Adrián Égüez

Conceptos

Reproduce with each other

There is a population of computer programs

(Individuals)

Escuela Politécnica Nacional

Capítulo 4: Programación Genética

MSc. Adrián Égüez

Conceptos

 Eventually evolve to do well in the given environment

The best individuals will survive

Escuela Politécnica Nacional

Capítulo 4: Programación Genética

MSc. Adrián Égüez

Ejemplo - a variation of robotic soccer

On a field about the size of half a table-tennis table

Robotic soccer is played with three to five robots per team

Escuela Politécnica Nacional

Capítulo 4: Programación Genética

MSc. Adrián Égüez

Ejemplo - a variation of robotic soccer

 Bunting it with the front of their chasis

They chase around an orange golf ball

Escuela Politécnica Nacional

Capítulo 4: Programación Genética

MSc. Adrián Égüez

Ejemplo - a variation of robotic soccer

It would be quite possible to use genetic programming to “design” a strategy for an entire team to play soccer

Escuela Politécnica Nacional

Capítulo 4: Programación Genética

MSc. Adrián Égüez

Ejemplo - a variation of robotic soccer

The robots should “know” how to follow the ball

Before a soccer-playing team could evolve

Escuela Politécnica Nacional

Capítulo 4: Programación Genética

MSc. Adrián Égüez

Ejemplo - a variation of robotic soccer

How can genetic programming be used to evolve code so that a robot will follow the ball

THE PROBLEM?

Escuela Politécnica Nacional

Capítulo 4: Programación Genética

MSc. Adrián Égüez

Ejemplo - a variation of robotic soccer

Good ball-following somehow needs to be measured

We need the ability to rate an individual’s performance

Escuela Politécnica Nacional

Capítulo 4: Programación Genética

MSc. Adrián Égüez

Ejemplo - a variation of robotic soccer

Produces a value called raw fitness

This measure is called a fitness test

Escuela Politécnica Nacional

Capítulo 4: Programación Genética

MSc. Adrián Égüez

Ejemplo - a variation of robotic soccer

Then we’re in a position to start the evolutionary process

If we have the ability to create a random program that has some amount of control over the robot

Escuela Politécnica Nacional

Capítulo 4: Programación Genética

MSc. Adrián Égüez

Ejemplo - a variation of robotic soccer

Evaluate Individual and give it a rank

Population 5000 (Individual) random programs.

Escuela Politécnica Nacional

Capítulo 4: Programación Genética

MSc. Adrián Égüez

Ejemplo - a variation of robotic soccer

These individuals would reproduce with one another to produce a new population

 Good individuals, that follow the ball closely, would rank highly

Escuela Politécnica Nacional

Capítulo 4: Programación Genética

MSc. Adrián Égüez

Ejemplo - a variation of robotic soccer

The average performance of the new population will be better than the average of the previous population

 If reproduction is biased so that highly ranked individuals reproduce more frequently

Escuela Politécnica Nacional

Capítulo 4: Programación Genética

MSc. Adrián Égüez

Ejemplo - a variation of robotic soccer

It could also be expected that the best individual in the new population would be better than the best individual of the previous population

Escuela Politécnica Nacional

Capítulo 4: Programación Genética

MSc. Adrián Égüez

Ejemplo - a variation of robotic soccer

After sufficient generations, the best individual in the last population would be an excellent solution to the problem

Escuela Politécnica Nacional

Capítulo 4: Programación Genética

MSc. Adrián Égüez

Ejemplo - a variation of robotic soccer

It could be used to evolve computer program solutions for any problem domain so long as individual solutions can be compared and ranked

Escuela Politécnica Nacional

Capítulo 4: Programación Genética

MSc. Adrián Égüez

Ejemplo - a variation of robotic soccer

The massive disadvantage of GP is the phenomenal computing resources required before any real-world problem can be tackled

Escuela Politécnica Nacional

Capítulo 4: Programación Genética

MSc. Adrián Égüez

Ejemplo - creating an individual

Which come together to form code

The fundamental elements of an individual are its genes

Escuela Politécnica Nacional

Capítulo 4: Programación Genética

MSc. Adrián Égüez

Ejemplo - creating an individual

Terminals

Functions

Escuela Politécnica Nacional

Capítulo 4: Programación Genética

MSc. Adrián Égüez

Ejemplo - creating an individual

Functions: nodes with children

Terminals: Are leaves without branches

Escuela Politécnica Nacional

Capítulo 4: Programación Genética

MSc. Adrián Égüez

Ejemplo - creating an individual

The function’s children provide the arguments for the function

Escuela Politécnica Nacional

Capítulo 4: Programación Genética

MSc. Adrián Égüez

Ejemplo - creating an individual

There are 5 terminals

Escuela Politécnica Nacional

Capítulo 4: Programación Genética

MSc. Adrián Égüez

Ejemplo - creating an individual

This is what returns

Escuela Politécnica Nacional

Capítulo 4: Programación Genética

MSc. Adrián Égüez

Ejemplo - creating an individual

The genes that will be available to the GP system must be selected or created by the user

Escuela Politécnica Nacional

Capítulo 4: Programación Genética

MSc. Adrián Égüez

Ejemplo - creating an individual

Poor selection may well render the system incapable of evolving a solution

Escuela Politécnica Nacional

Capítulo 4: Programación Genética

MSc. Adrián Égüez

Ejemplo - creating a random population

Koza specifies three techniques: namely grow, full and ramped-halfand-half

Escuela Politécnica Nacional

Capítulo 4: Programación Genética

MSc. Adrián Égüez

Ejemplo - creating a random population

The initial population will not have duplicate individuals

Escuela Politécnica Nacional

Capítulo 4: Programación Genética

MSc. Adrián Égüez

Ejemplo - Grow

An individual created with this method may be a tree of any depth up to a specified maximum, m

Creates one individual at a time

Escuela Politécnica Nacional

Capítulo 4: Programación Genética

MSc. Adrián Égüez

Ejemplo - Grow

Starting from the root of the tree every node is randomly chosen as either a function or terminal

random:
function or terminal

Escuela Politécnica Nacional

Capítulo 4: Programación Genética

MSc. Adrián Égüez

Ejemplo - Grow

If the node is a terminal, a random terminal is chosen

Choosen node "Y" :
Y / X / 7 / 11 / 2.2

Escuela Politécnica Nacional

Capítulo 4: Programación Genética

MSc. Adrián Égüez

Ejemplo - Grow

If the node is a function, a random function is chosen

Choosen node "-" :
Y / X / 7 / 11 / 2.2

Escuela Politécnica Nacional

Capítulo 4: Programación Genética

MSc. Adrián Égüez

Ejemplo - fitness test

Individuals need to be assessed for their fitness

This is a problem specific issue:
how good (or bad) is this individual?

Escuela Politécnica Nacional

Capítulo 4: Programación Genética

MSc. Adrián Égüez

Ejemplo - fitness test

The question could be replaced with an algorithm like

let the individual play soccer for five minutes; the fitness value is the same as the number of goals scored

Escuela Politécnica Nacional

Capítulo 4: Programación Genética

MSc. Adrián Égüez

Ejemplo - fitness test

This could be an acceptable test as it can distinguish between different levels of ability

A good player may score six goals, while an excellent player may score ten

Escuela Politécnica Nacional

Capítulo 4: Programación Genética

MSc. Adrián Égüez

Ejemplo - fitness test

“yes, at least one goal was scored” or “no, zero goals were scored”

The fitness test gives as detailed answer as possible

Escuela Politécnica Nacional

Capítulo 4: Programación Genética

MSc. Adrián Égüez

Ejemplo - fitness test

In this case, the greater the value the better the individual

Escuela Politécnica Nacional

Capítulo 4: Programación Genética

MSc. Adrián Égüez

Ejemplo - fitness test

The sum of the distances between the robot and the ball for a number of discrete times

For a ball-follower a good fitness test

Escuela Politécnica Nacional

Capítulo 4: Programación Genética

MSc. Adrián Égüez

Genetic Operations

Apply the fitness test to all the individuals in the initial random population

The evolutionary process starts

Escuela Politécnica Nacional

Capítulo 4: Programación Genética

MSc. Adrián Égüez

Genetic Operations

Individuals in the new population are formed by two main methods: reproduction and crossover.

Escuela Politécnica Nacional

Capítulo 4: Programación Genética

MSc. Adrián Égüez

Genetic Operations - Reproduction

 Reproduction is where a selected individual copies itself into the new population

Is effectively the same as one individual surviving into the next generation

Escuela Politécnica Nacional

Capítulo 4: Programación Genética

MSc. Adrián Égüez

Genetic Operations - Reproduction

Koza allowed 10% of the population to reproduce

A reproduced individual does not need to be tested, as the result is already known

Escuela Politécnica Nacional

Capítulo 4: Programación Genética

MSc. Adrián Égüez

Genetic Operations - Reproduction

If the fitness test does not change, reproduction can have a significant effect on the total time required for GP because a reproduced individual will have an identical fitness score to that of its parent

Escuela Politécnica Nacional

Capítulo 4: Programación Genética

MSc. Adrián Égüez

Genetic Operations - Reproduction

A fitness test that has a random component, which is effectively a test that does not initialise to exactly the same starting scenario, would not apply for this increase in efficiency

Escuela Politécnica Nacional

Capítulo 4: Programación Genética

MSc. Adrián Égüez

Genetic Operations - Reproduction

 The selection of an individual to undergo reproduction is the responsibility of the selection function

Escuela Politécnica Nacional

Capítulo 4: Programación Genética

MSc. Adrián Égüez

Genetic Operations - Crossover

Crossover requires two individuals and produces two different individuals for the new population

Escuela Politécnica Nacional

Capítulo 4: Programación Genética

MSc. Adrián Égüez

Genetic Operations - Crossover

Genetic material from two individuals is mixed to form offspring

Koza uses crossover on 90% of the population

Escuela Politécnica Nacional

Capítulo 4: Programación Genética

MSc. Adrián Égüez

Genetic Operations - Crossover

It provides the source of new (and eventually better) individuals

It is the more important of the two methods

Escuela Politécnica Nacional

Capítulo 4: Programación Genética

MSc. Adrián Égüez

Genetic Operations - Crossover

There are a few other evolutionary operations: editing, mutation, permutation, encapsulation and decimation

Escuela Politécnica Nacional

Capítulo 4: Programación Genética

MSc. Adrián Égüez

Decisiones del usuario

The user must make a number of decisions before the GP system may begin

Escuela Politécnica Nacional

Capítulo 4: Programación Genética

MSc. Adrián Égüez

Decisiones del usuario

Firstly, the available genes need to be selected and created

Escuela Politécnica Nacional

Capítulo 4: Programación Genética

MSc. Adrián Égüez

Decisiones del usuario

Secondly, the user must specify a number of control parameters

Escuela Politécnica Nacional

Capítulo 4: Programación Genética

MSc. Adrián Égüez

Decisiones del usuario

The decisions are critically important as they have a limiting effect on the search space of possible programs

Escuela Politécnica Nacional

Capítulo 4: Programación Genética

MSc. Adrián Égüez

Decisiones del usuario

Having too many limits may remove all chance of evolving an acceptable individual

Escuela Politécnica Nacional

Capítulo 4: Programación Genética

MSc. Adrián Égüez

Decisiones del usuario

Not restricting the search-space sufficiently has its own issues

Escuela Politécnica Nacional

Capítulo 4: Programación Genética

MSc. Adrián Égüez

Decisiones del usuario

If it is not possible to solve the problem given the available genes

What hope has the system got to evolve a solution?

Escuela Politécnica Nacional

Capítulo 4: Programación Genética

MSc. Adrián Égüez

Decisiones del usuario

It is important to include every necessary function and terminal

Escuela Politécnica Nacional

Capítulo 4: Programación Genética

MSc. Adrián Égüez

Decisiones del usuario

If it is not possible to solve the problem given the available genes

What hope has the system got to evolve a solution?

Escuela Politécnica Nacional

Referencias

MSc. Adrián Égüez

  • http://geneticprogramming.com/
  • Genetic Programming On the Programming of Computers by Means of Natural Selection John R. Koza
  • A Field Guide to Genetic Programming Riccardo Poli, William B. Langdon, Nicholas F. McPhee, et al.
  • Introduction to Genetic Programming - Matthew Walker
  • Genetic Programming: An Introduction and Tutorial, with a Survey of Techniques and Applications - William B. Langdon, Riccardo Poli, Nicholas F. McPhee, and John R. Koza
  • Genetic Programming – basic ideas - Fernando Lobo

Paradigmas de Programación

By Adrian Eguez

Paradigmas de Programación

Slides sobre los paradigmas de la programación

  • 1,014