Aplicaciones NETLOGO

download Aplicaciones NETLOGO

of 17

Transcript of Aplicaciones NETLOGO

  • 7/27/2019 Aplicaciones NETLOGO

    1/17

    Inteligencia Artificial 2013-I

    Universidad Nacional de IngenieraEspecialidad: Ing. SISTEMAS Pgina 1

    INDICE

    1) Animacin de Agentes---------------------------------------------------------------------2

    a) Descripcin de la aplicacin---------------------------------------------------------2

    b) Cdigo de Fuente----------------------------------------------------------------------2

    c) Conclusiones----------------------------------------------------------------------------3

    2) Variedad Colores---------------------------------------------------------------------------4

    a) Descripcin de la aplicacin--------------------------------------------------------4

    b) Cdigo de Fuente----------------------------------------------------------------------4

    c) Conclusiones----------------------------------------------------------------------------5

    3) La comunicacin----------------------------------------------------------------------------5

    a) Descripcin de la aplicacin---------------------------------------------------------6b) Cdigo de Fuente----------------------------------------------------------------------6

    c) Conclusiones----------------------------------------------------------------------------7

    4) Funcionamiento de molculas en de un motor--------------------------------------8

    a) Descripcin de la aplicacin --------------------------------------------------------8

    b) Cdigo de Fuente----------------------------------------------------------------------8

    c) Conclusiones----------------------------------------------------------------------------9

    5) Variedad Colores---------------------------------------------------------------------------10

    a) Descripcin de la aplicacin--------------------------------------------------------10

    b) Cdigo de Fuente----------------------------------------------------------------------10

    c) Conclusiones----------------------------------------------------------------------------11

    6) Figura geomtrica--------------------------------------------------------------------------12

    a) Descripcin de la aplicacin--------------------------------------------------------12

    b) Cdigo de Fuente---------------------------------------------------------------------12

    c) Conclusiones---------------------------------------------------------------------------14

    7) Ruta de un punto a otro------------------------------------------------------------------15

    a) Descripcin de la aplicacin--------------------------------------------------------15

    b) Cdigo de Fuente---------------------------------------------------------------------15

    c) Conclusiones---------------------------------------------------------------------------17

  • 7/27/2019 Aplicaciones NETLOGO

    2/17

    Inteligencia Artificial 2013-I

    Universidad Nacional de IngenieraEspecialidad: Ing. SISTEMAS Pgina 2

    1. Animacin de Agentes1.1. Descripcin de la aplicacin

    En esta aplicacin simularemos agentes (carros, barcos, animales, personas, etc.) la

    participacin de estos agentes ser de modo aleatorio como podemos ver en la grafica

    siguiente.

    1.2. Codigo de Fuente

    breed [agents agent] ;; Repesenta los agentes

    globals

    [

    next-agent-tick ;; Lama al Siguiente Agente

    ]

    to setup

    ca ;; Limpiar todo

    askpatcheswith [ pycor>-9 ] [ setpcolor88 ] ; askpatcheswith [

    pycor=-9 ] [ setpcolor3 ]

    askpatcheswith [ pycor

  • 7/27/2019 Aplicaciones NETLOGO

    3/17

    Inteligencia Artificial 2013-I

    Universidad Nacional de IngenieraEspecialidad: Ing. SISTEMAS Pgina 3

    [ setshapeone-of

    [ "ambulance""bus""car""car 2""truck""van" ]

    ]

    if (type-of-agent ="any of the railway vehicles below")

    [ setshapeone-of

    [ "train""train freight engine""train passenger

    car""train passenger engine" ]]

    ]

    setheading90

    setsize size-of-agent

    setxcor (-max-pxcor)

    setycor y-position

    ]

    end

    to animate-agents

    ; Movemos la pantalla a la derecha o izquierda

    let colour black

    if (ticks= next-agent-tick)

    [

    create-new-agent

    set next-agent-tick ticks+random next-agent-random-tick-interval +

    minimum-gap ; Creamos un carro aparte

    ]

    ask agents

    [ if (xcor+1>max-pxcor)

    [ die ] ; Movemos el Agentes a la pantalla

    setxcorxcor+1; Movemos el agente a la derecha

    ]

    tickend

    1.3. Conclusiones

    Este modelo de simulacin con el programa de NetLogo nos ayuda a realizar interacciones con

    distintos agentes que nos rodean en la vida diaria.

  • 7/27/2019 Aplicaciones NETLOGO

    4/17

    Inteligencia Artificial 2013-I

    Universidad Nacional de IngenieraEspecialidad: Ing. SISTEMAS Pgina 4

    2. Variedad Colores2.1. Descripcin de la aplicacin

    En esta aplicacin simularemos la variedad de colores que nos pueden salircombinado colores bsicos como mostramos en la grafica siguiente.

    2.2. Cdigo de Fuente

    to setup

    ca;; Preparamos el color Blanco

    ask patches [ set pcolor white ]

    ;; Pintamos los 139 colores

    ask patches with [(pxcor >= 0) and (pxcor

  • 7/27/2019 Aplicaciones NETLOGO

    5/17

    Inteligencia Artificial 2013-I

    Universidad Nacional de IngenieraEspecialidad: Ing. SISTEMAS Pgina 5

    ask patches with [(pxcor = -2) and (pycor = 4

    [ set plabel-color black ]

    [ set plabel-color white ] ]

    end

    2.3. Conclusiones

    Como podemos observar la grafica anterior simulada nos permiteobtener los 139 colores que son visibles por el ser humano

  • 7/27/2019 Aplicaciones NETLOGO

    6/17

    Inteligencia Artificial 2013-I

    Universidad Nacional de IngenieraEspecialidad: Ing. SISTEMAS Pgina 6

    3. La comunicacin3.1. Descripcin de la aplicacin

    El grafico siguiente nos permite simular la presencia de puntosaleatorios azules en un primer momento que se tratan de comunicarluego con el paso del tiempo se van incrementando los puntosaleatorios de color rojo para que estos establezcan una comunicacin,de cada combinacin bajo la siguiente regla.

    Azul vs. Azul Resultado Azul

    Azul vs. Rojo Resultado Rojo

    Rojo vs. Rojo Resultado Rojo

    3.2. Cdigo de Fuente

    turtles-own [

    message? ;; Mensaje muestra la cantidad de interacciones de lacolor rojo

    to setup

    clear-all

    crt 500 [

    set message? false

    setxy random-xcor random-ycor

    ]

    ask one-of turtles [ set message? true ] ;; Aca nos da el mensaje

    1 de la cantidad de interacciones

    ask turtles [ recolor ]

    end

    to go

  • 7/27/2019 Aplicaciones NETLOGO

    7/17

    Inteligencia Artificial 2013-I

    Universidad Nacional de IngenieraEspecialidad: Ing. SISTEMAS Pgina 7

    ask turtles [ move ]

    ask turtles [ communicate ]

    ask turtles [ recolor ]

    tick

    end

    ;; Posision randomto move ;; Proceso del movimiento

    fd random 4

    ;; vuelta en random amount between -40 and 40 degrees,

    ;; combina en el medio turn at 0

    rt random 40

    lt random 40

    end

    ;; lo principal del procedimiento!

    to communicate ;; Procediminto

    if any? other turtles-here with [message?]

    [ set message? true ]

    end

    ; color con mensaje rojo and cambiamos con el mensaje blue

    to recolor ;; Proceso del color

    ifelse message?

    [ set color red ]

    [ set color blue ]

    end

    3.3. Conclusiones

    Como podemos notar en la grafica del simulador anterior aplicando laregla:

    Azul vs. Azul Resultado AzulAzul vs. Rojo Resultado RojoRojo vs. Rojo Resultado Rojo

    Al final del proceso de simulacin los puntos de color azul desapareceny se llena de puntos rojos el simulador

  • 7/27/2019 Aplicaciones NETLOGO

    8/17

    Inteligencia Artificial 2013-I

    Universidad Nacional de IngenieraEspecialidad: Ing. SISTEMAS Pgina 8

    4. Funcionamiento de molculas en de un motor4.1. Descripcin de la aplicacin

    Este simulador nos muestra el comportamiento de la estructurainterna de un motor.

    4.2. Cdigo de Fuente

    flashes-own [birthday]

    particles-own

    [

    speed mass energy ;; particle info

    momentum-difference ;; used to calculate pressure from wall

    hits

    last-collision ;; used to prevent particles from

    colliding multiple times

    to setup

    ca

    set-default-shape particles "circle"set-default-shape flashes "plane"

    set max-tick-delta 0.1073

    ;; box has constant size.

    set raw-width round (0.01 * box-width * max-pxcor)

    set raw-height round (0.01 * box-height * max-pycor)

    set piston-height raw-height

    make-box

    make-piston

    set piston-vel 0

    set gravity 0.125

    ;;; 19 patches of wall space on the inside of the box

    set length-horizontal-surface ( 2 * (raw-height - 1) + 1)

    set length-vertical-surface (piston-height)make-box

  • 7/27/2019 Aplicaciones NETLOGO

    9/17

    Inteligencia Artificial 2013-I

    Universidad Nacional de IngenieraEspecialidad: Ing. SISTEMAS Pgina 9

    make-particles

    set pressure-history [0 0 0] ;; plotted pressure will be averaged

    over the past 3 entries

    update-variables

    set init-avg-speed avg-speed

    set init-avg-energy avg-energy

    setup-plotsdo-plotting

    end

    to go

    if piston-height < 3

    [ user-message "The piston reached the bottom of the chamber. The

    simulation will stop."

    stop

    ]

    if piston-height >= 2 * raw-height - 1

    [ user-message "The piston reached the top of the chamber. The

    simulation will stop."

    stop]

    ask particles [ bounce ]

    ask particles [ move ]

    ask particles [ check-for-collision ]

    move-piston

    tick-advance tick-delta

    if floor ticks > floor (ticks - tick-delta)

    [

    calculate-pressure

    update-variables

    do-plotting

    ]

    calculate-tick-delta;; we check for pcolor = black to make sure flashes that are left

    behind by the piston die

    ask flashes with [ticks - birthday > 0.4 or pcolor = black]

    [ die ]

    display

    end

    to update-variables

    ;; particle variables

    set medium count particles with [color = green]

    set slow count particles with [color = blue]

    set fast count particles with [color = red]

    set avg-speed mean [speed] of particles

    set avg-energy mean [energy] of particles

    set tot-particle-energy sum [energy] of particles

    set piston-energy (piston-kinetic-energy + piston-potential-energy)

    end

    4.3. Conclusiones

    Este simulador es de gran importancia En la actualidad ya que permiteaumentar los niveles de eficiencia en los motores .

  • 7/27/2019 Aplicaciones NETLOGO

    10/17

    Inteligencia Artificial 2013-I

    Universidad Nacional de IngenieraEspecialidad: Ing. SISTEMAS Pgina 10

    5. Variedad Colores5.1. Descripcin de la aplicacin

    El siguiente simulador muestra las posibles salidas de unaexplosin bajo el efecto de de la gravedad.

    5.2. Cdigo de Fuente

    turtles-own

    [

    velocity-x ; particle velocity in the x axis

    velocity-y ; particle velocity in the y axis

    force-accumulator-x ; force exerted in the x axis

    force-accumulator-y ; force exerted in the y axis

    ]

    to setup

    clear-allask patch 0 (max-pycor / 2) [

    sprout num-particles [

    set velocity-x 10 - (random-float 20) ; initial x velocity

    set velocity-y 10 - (random-float 20) ; initial y velocity

    pen-down

    ]

    ]

    end

    to go

    if not any? turtles [ stop ]

    compute-forces ; calculate the forces and add them to the

    accumulator

  • 7/27/2019 Aplicaciones NETLOGO

    11/17

    Inteligencia Artificial 2013-I

    Universidad Nacional de IngenieraEspecialidad: Ing. SISTEMAS Pgina 11

    apply-forces ; calculate the new location and speed by multiplying

    the

    ; forces by the step-size

    tick-advance step-size

    display

    end

    ; calculate and sum all the forces exerted on the particles

    to compute-forces

    ask turtles

    [

    ; clear force accumulators

    set force-accumulator-x 0

    set force-accumulator-y 0

    ; calculate forces

    apply-gravity

    ]

    end

    ; updates the accumulator with the gravity forceto apply-gravity ;; turtle procedure

    set force-accumulator-y force-accumulator-y - gravity-constant

    end

    ; calculates the position of the particles at each step

    to apply-forces

    ask turtles

    [

    ; calculate the new velocity of the particle

    set velocity-x velocity-x + (force-accumulator-x * step-size)

    set velocity-y velocity-y + (force-accumulator-y * step-size)

    ; calculate the displacement of the particle

    let step-x velocity-x * step-sizelet step-y velocity-y * step-size

    ;; if the turtle tries to leave the world let it die

    if patch-at step-x step-y = nobody [ die ]

    ;; if the turtle does not go out of bounds

    ;; add the displacement to the current position

    let new-x xcor + step-x

    let new-y ycor + step-y

    facexy new-x new-y

    setxy new-x new-y

    ]

    end

    5.3. Conclusiones

    Este simulador nos muestra el posible comportamiento de lasbombardas de los juegos artificiales, asi tambin la explosin de uncuete ajo el efecto de la gravedad

  • 7/27/2019 Aplicaciones NETLOGO

    12/17

    Inteligencia Artificial 2013-I

    Universidad Nacional de IngenieraEspecialidad: Ing. SISTEMAS Pgina 12

    6. Figura geomtrica6.1. Descripcin de la aplicacin

    Mediante el siguiente simulador se muestra todas las posiblesgraficas que se puede construir con 5 puntos que tenemos comobase.

    6.2. Cdigo de Fuente

    globals [

    level ;; determines how many nodes you have to untangle;

    ;; the formula is below

    ]

    to setup

    clear-all

    set-default-shape turtles "circle"

    ask patches [ set pcolor white ] ;; plain white background

    set level starting-level

    setup-level

    end

    to setup-level

    reset-ticks ;; use tick counter as a move counter

    clear-turtles ;; when the turtles die, the links connecting them

    die too

    ;; create nodes and position them randomly

    create-turtles 4 + level [

    set color blue

    setxy random-xcor random-ycor

    ]

    ;; Now we need to make some links. We have to be careful that

  • 7/27/2019 Aplicaciones NETLOGO

    13/17

    Inteligencia Artificial 2013-I

    Universidad Nacional de IngenieraEspecialidad: Ing. SISTEMAS Pgina 13

    ;; the resulting graph has a solution. Probably there are lots

    ;; of ways this could be done, but this was the simplest way we

    ;; could think of.

    ;; First make a bunch of links at random.

    while [count links < count turtles] [

    ask one-of turtles [

    ask one-of other turtles [ attempt-link ]]

    ]

    ;; Then fill in all remaining allowable links.

    ask turtles [

    ask other turtles [ attempt-link ]

    ]

    ;; Now we have a graph which we know is solvable,

    ;; because the current layout is a solution.

    ;; Time to scramble the nodes around!

    while [solved?] [ scramble ]

    display

    end

    to attempt-link ;; link procedure

    ;; note that if the link already exists, nothing happens

    create-link-with myself [

    if any-intersections? [ die ]

    ]

    end

    to scramble

    ;; The turtles agentset is always in random order,

    ;; so this makes a random layout.

    layout-circle turtles (world-width / 2 - 1)

    end

    ;; This procedure lets us find the next turtle,

    ;; or the turtle two over, and so on.

    to-report turtle-plus [n] ;; turtle procedure

    report turtle ((who + n) mod count turtles)

    end

    to go

    if mouse-down? [

    ;; find the closest node

    let grabbed min-one-of turtles [distancexy mouse-xcor mouse-ycor]

    ;; loop until the mouse button is released

    while [mouse-down?] [

    ask grabbed [ setxy mouse-xcor mouse-ycor ]

    display

    ]

    ;; use tick counter as a move counter

    tick

    ;; check if the level is solved

    if solved? [

    user-message "You rock. Now try this..."

    set level level + 1

    setup-level

    ]

    ]

    end

    to-report solved?

  • 7/27/2019 Aplicaciones NETLOGO

    14/17

    Inteligencia Artificial 2013-I

    Universidad Nacional de IngenieraEspecialidad: Ing. SISTEMAS Pgina 14

    report all? links [not any-intersections?]

    end

    to-report any-intersections? ;; link procedure

    report any? other links with [crossed? self myself]

    end

    to-report crossed? [link-a link-b]

    ;; store nodes in variables for easy access

    let a1 [end1] of link-a

    let a2 [end2] of link-a

    let b1 [end1] of link-b

    let b2 [end2] of link-b

    let nodes (turtle-set a1 a2 b1 b2)

    ;; if the links share a node, they don't cross

    if 4 > count nodes [ report false ]

    ;; but if two nodes are on top of each other, we will say

    ;; the links do cross (so you can't cheat that way)

    if 4 > length remove-duplicates [list xcor ycor] of nodes

    [ report true ];; if the ends of link-a are on opposite sides of link-b,

    ;; and the ends of link-b are on opposite sides of link-a,

    ;; then the links cross

    report [subtract-headings towards a2 towards b1 < 0 xor

    subtract-headings towards a2 towards b2 < 0] of a1

    and [subtract-headings towards b2 towards a1 < 0 xor

    subtract-headings towards b2 towards a2 < 0] of b1

    end

    6.3. Conclusiones

    Este simulador es de gran ayuda para los estudiantes de geometraprincipalmente pero lamentablemente no se implanta en los colegios.

  • 7/27/2019 Aplicaciones NETLOGO

    15/17

    Inteligencia Artificial 2013-I

    Universidad Nacional de IngenieraEspecialidad: Ing. SISTEMAS Pgina 15

    7. Ruta de un punto a otro

    7.1. Descripcin de la aplicacin

    Mediante el grafico siguiente muestra como ir de un punto a otro por la ruta

    mas corta en un periodo de tiempo

    7.2. Cdigo de Fuente

    breed [ leaders leader ]

    breed [ followers follower ]

    globals [

    nest-x nest-y ;; location of center of nest

    food-x food-y ;; location of center of food

    leader-heading ;; heading of the leader ant]

    to setup

    ca

    set-default-shape turtles "bug"

    set nest-x 10 + min-pxcor ;; set up nest and

    food locations

    set nest-y 0

    set food-x max-pxcor - 10

    set food-y 0

    ;; draw the nest in brown by stamping a circular

    ;; brown turtle

    ask patch nest-x nest-y [sprout 1 [

  • 7/27/2019 Aplicaciones NETLOGO

    16/17

    Inteligencia Artificial 2013-I

    Universidad Nacional de IngenieraEspecialidad: Ing. SISTEMAS Pgina 16

    set color brown

    set shape "circle"

    set size 10

    stamp

    die

    ]

    ];; draw the nest in orange by stamping a circular

    ;; orange turtle

    ask patch food-x food-y [

    sprout 1 [

    set color orange

    set shape "circle"

    set size 10

    stamp

    die

    ]

    ]

    create-leaders 1

    [ set color red ;; leader ant isred

    set size 2

    wiggle 50 ] ;; ...and starts

    out with a random heading

    create-followers (num-ants - 1)

    [ set size 2

    set color yellow ] ;; middle ants are

    yellow

    ask turtles

    [ setxy nest-x nest-y ;; start the ants

    out at the nest

    set heading 90 ]

    ask turtle (num-ants - 1)[ set color blue ;; last ant is blue

    set pen-size 2

    pd ] ;; ...and leaves a

    trail

    ask leaders

    [ set pen-size 2

    pd ] ;; the leader also

    leaves a trail

    set leader-heading [heading] of one-of leaders

    end

    to go

    if all? turtles [xcor >= food-x]

    [ stop ]

    ask leaders ;; the leader ant

    wiggles and moves

    [ wiggle leader-wiggle-angle

    correct-path

    if (xcor > (food-x - 5 )) ;; leader heads

    straight for food, if it is close

    [ facexy food-x food-y ]

    if xcor < food-x ;; do nothing if

    you're at or past the food

    [ fd 0.5 ] ]

    ask followers

    [ face turtle (who - 1) ;; follower ants

    follow the ant ahead of them

  • 7/27/2019 Aplicaciones NETLOGO

    17/17

    Inteligencia Artificial 2013-I

    Universidad Nacional de IngenieraEspecialidad: Ing. SISTEMAS Pgina 17

    if time-to-start? and (xcor < food-x) ;; followers wait

    a bit before leaving nest

    [ fd 0.5 ] ]

    set leader-heading [heading] of one-of leaders

    tick

    end

    ;; turtle procedure; wiggle a random amount, averaging zero turn

    to wiggle [angle]

    rt random-float angle

    lt random-float angle

    end

    ;; turtle procedure

    to correct-path

    ifelse heading > 180

    [ rt 180 ]

    [ if patch-at 0 -5 = nobody

    [ rt 100 ]

    if patch-at 0 5 = nobody[ lt 100 ] ]

    end

    ;; turtle reporter; if true, then the ant is authorized to move out of

    the nest

    to-report time-to-start?

    report ([xcor] of (turtle (who - 1))) > (nest-x + start-delay +

    random start-delay )

    end

    7.3. Conclusiones

    Como podemos observar en el simulador anterior de la vida real para ir de un

    punto a otro sabemos como llegar al otro punto pero mas no cual es la ruta

    mas corta en la grafica anterior se nota que es una especie de parbola como

    sucede en la vida real pero matemticamente la ruta mas corta seria una recta

    pero no se cumple en la realidad.