| … | … | @@ -405,63 +405,8 @@ |
| 405 | 405 | (format #t "Found a network of size ~D\n" (cog-arity (gar just-one))) |
| 406 | 406 | |
| 407 | 407 | ; --------------------------------------------------------------------- |
| 408 | | ; Assign initial susceptibility and infirmity values to individuals |
| 409 | | ; in the network. These will be randomly generated numbers. |
| 410 | | ; |
| 411 | | ; This will be done twice, illustrating two different styles of coding. |
| 412 | | ; The first style is a traditional, scheme list (srfi-1) style of |
| 413 | | ; coding, something that a naive coder might attempt. (This is not |
| 414 | | ; scheme-specific; python coders might code the same way, if this |
| 415 | | ; demo was written in python.) Despite every attempt to "keep it |
| 416 | | ; simple", this code quickly becomes opaque and hard to understand. |
| 417 | | ; Thus, the first thing shown is NOT the recommended style. |
| 418 | | ; |
| 419 | | ; The second time, it will be done in Atomese. The code is recognizably |
| 420 | | ; identical to the first attempt, but with far fewer calls to srfi-1 |
| 421 | | ; routines to manage and transform lists. This is because Atomese, |
| 422 | | ; and specifically, the Atomese query language, is far more compact in |
| 423 | | ; managing state. You don't have to juggle things around with maps |
| 424 | | ; and lists and functions -- the query language manages all this, |
| 425 | | ; automatically. |
| 426 | | ; |
| 427 | | ; ---------- |
| 428 | | ; So: version 1: traditional software proramming style. |
| 429 | | |
| 430 | | ; Each individual is linked back to the anchor node. Each individual |
| 431 | | ; is also represented with a ConceptNode (we have some other junk |
| 432 | | ; also attached to the anchor, so filter that out.) |
| 433 | | (define individual-list |
| 434 | | (filter (lambda (ind) (equal? (cog-type ind) 'ConceptNode)) |
| 435 | | (map gar (cog-incoming-by-type anchor 'MemberLink)))) |
| 408 | ; A handy utility. We need this for the rest of the demo. |
| 436 | 409 | |
| 437 | | (format #t "The social network consists of ~D individuals\n" |
| 438 | | (length individual-list)) |
| 439 | | |
| 440 | | ; Assign to each individual the state of "initially healthy". |
| 441 | | ; Choose some random numbers for the overall population health: |
| 442 | | ; some random susceptibility (the probability of becoming ill after |
| 443 | | ; being exposed), the infirmity (probability of illness leading to |
| 444 | | ; death) and a probability of recovering from illness. |
| 445 | | (for-each |
| 446 | | (lambda (individual) |
| 447 | | (cog-set-value! individual seir-state susceptible) |
| 448 | | (cog-set-value! individual susceptibility |
| 449 | | (cog-execute! (RandomNumber (Number 0.2) (Number 0.8)))) |
| 450 | | (cog-set-value! individual infirmity |
| 451 | | (cog-execute! (RandomNumber (Number 0.01) (Number 0.55)))) |
| 452 | | (cog-set-value! individual recovery |
| 453 | | (cog-execute! (RandomNumber (Number 0.6) (Number 0.95))))) |
| 454 | | individual-list) |
| 455 | | |
| 456 | | ; ---------- |
| 457 | | ; So: version 2: Atomese query language proramming style. |
| 458 | | |
| 459 | | ; The above uses a very traditional scheme `for-each` loop to perform |
| 460 | | ; the initialization. The Atomese query language automatically iterates |
| 461 | | ; over the entire contents of the AtomSpace, so this loop is not |
| 462 | | ; explicitly needed. |
| 463 | | |
| 464 | | ; ------ But first, a handy utility: |
| 465 | 410 | ; Execute some Atomese. Assume that it returns a SetLink. |
| 466 | 411 | ; Unwrap the SetLink, discard it, return the contents. |
| 467 | 412 | (define (exec-unwrap ATOMESE) |
| … | … | @@ -470,13 +415,23 @@ |
| 470 | 415 | (cog-delete set-link) |
| 471 | 416 | contents) |
| 472 | 417 | |
| 473 | | ; --------- |
| 474 | | |
| 475 | | ; Here's the Atomese version of the above. This performs a search of the |
| 476 | | ; AtomSpace, locating all individuals. They are easy to identify: each |
| 477 | | ; individual was previously anchored via a MemberLink. The DeleteLink |
| 478 | | ; runs the contents inside of it, but then deletes the return value. |
| 479 | | ; This is handy, to avoid garbage from accumulating in the AtomSpace. |
| 418 | ; --------------------------------------------------------------------- |
| 419 | ; Initialize the network. |
| 420 | ; |
| 421 | ; Assign to each individual the state of "initially healthy". |
| 422 | ; Choose some random numbers for the overall population health: |
| 423 | ; some random susceptibility (the probability of becoming ill after |
| 424 | ; being exposed), the infirmity (probability of illness leading to |
| 425 | ; death) and a probability of recovering from illness. |
| 426 | ; |
| 427 | ; The initialization is done with a graph-query and graph-rewrite |
| 428 | ; run over the AtomSpace. The query is simple: find all individuals |
| 429 | ; attached with a `MemberLink` to an anchor node. The rewrite is |
| 430 | ; trickier: its a `DeleteLink` whose contents are executed first. |
| 431 | ; The contents perform the intialization of each individual that is |
| 432 | ; found. The DeleteLink then just discards the wrapper ListLink. |
| 433 | ; We didn't actually need to discard it, but it seems nice to not |
| 434 | ; leave ponitless garbage lying round in the AtomSpace. |
| 480 | 435 | |
| 481 | 436 | (define (initialize-state) |
| 482 | 437 | (exec-unwrap |
| … | … | @@ -498,6 +453,16 @@ |
| 498 | 453 | (initialize-state) |
| 499 | 454 | |
| 500 | 455 | ; --------- |
| 456 | ; Each individual is linked back to the anchor node. Each individual |
| 457 | ; is also represented with a ConceptNode (we have some other junk |
| 458 | ; also attached to the anchor, so filter that out.) |
| 459 | (define individual-list |
| 460 | (filter (lambda (ind) (equal? (cog-type ind) 'ConceptNode)) |
| 461 | (map gar (cog-incoming-by-type anchor 'MemberLink)))) |
| 462 | |
| 463 | (format #t "The social network consists of ~D individuals\n" |
| 464 | (length individual-list)) |
| 465 | |
| 501 | 466 | ; Pick one person, and make them infected. |
| 502 | 467 | (cog-set-value! (car individual-list) seir-state infected) |
| 503 | 468 | |