# STARTING WITH PYTHON3 – The very beginning – part 7

Journal: uffmm.org,
ISSN 2567-6458, July 23, 2019 – May 13, 2020
Email: info@uffmm.org
Author: Gerd Doeben-Henisch
Email:
gerd@doeben-henisch.de

###### CONTEXT

This is the next step in the python3 programming project. The overall context is still the python Co-Learning project.

###### SUBJECT

Meanwhile I am beginning to combine elements of the python language with some applied ideas (in the last section the idea of cognitive entropy illustrated with the equalization of strings). In this section I extend the idea of a simple 2-dimensional virtual world and the construction of objects and their properties.

Remark: for a general help-information you can find a lot of helpful text directly on the python.org site: https://www.python.org/doc/.

###### SZENARIO

Motivation

Because we want to introduce step-wise artificial actors which can learn, show some intelligence, and can work in teams, we need a minimal virtual world to start (this virtual world is a placeholder for the real world later if applied to the real world (RW) by sensors and actors). Although there is a big difference between the real world and a virtual world a virtual world is nevertheless very helpful for introducing basic concepts. And, indeed, finally if you are applying to real world data you will not be able to do this without mathematical models which represent virtual structures. You will need lots of mappings between real and virtual and vice versa. Thus from a theoretical point of view any kind of virtual model will do, the question is only how ‘easily’ and how ‘good’ it fits.

###### Virtual World (VW) – General Assumptions
1. A 2-dimensional world as a grid together with a world clock CLCK. The clock produces periodic events which can be used to organize a timely order.

2. There is an x and y axis with the root (0,0) in the upper left corner

3. A coordinate (x,y) represents a position.

4. A position can be occupied by (i) nothing or (ii) by an object. Objects can be obstacles, energy objects (= Food), a virtual executive actor, or even more.

5. Only one object per position is allowed.

6. It is assumed that there are four main directions (headings): ‘North (N)’ as -Y, ‘East (E)’ as +X, ‘South (S)’ as +Y, and ‘West (W)’ as -X. There are additional for compound directions North-East, East-South, South-West, and South-West.

7. Possible movements are always in one of the mentioned directions from one cell to the adjacent cell. Movements will be blocked by obstacle objects or actor objects. In case of compound directions these are blocked to if the adjacent main directions are blocked

8. A sequence of movements realizes in the grid a path from one position to the next.

9. The size of the assumed grids is always finite. In a strict finite world the border of the grid blocks a movement. In a finite-infinite world the borders of the grid are connected in a way that the positions in the south lead to the position in the north and the positions in the east lead to the positions in the west, and vice versa. Therefor can a path in an finite-infinite world become infinite.

10. A grid G with its objects O will be configured at the beginning of an experiment. Without the artificial actors all objects are in a static world assumed to be permanent. In a dynamic world there can be a world function f_w inducing changes depending from the world clock. Between permanent and dynamic exists some intermediate changes: Food can be there but with varying amount of energy, as well as with an actor; he can posses properties which can change during its existence.

11. During an experiment possible changes in the world can happen through a world function f_w, if such a function has been defined. The other possible source for changes are artificial actors, which can act and which can ‘die’.

Remark: The basic idea of this kind of a virtual world I have got from a paper from S.W.Wilson from 1994 entitled ZCS: a zeroth level classifier system published in the Journal Evolutionary Computation vol. 2 number 1 pages 1-18. I have used this concept the first time in a lecture in 2012 (URL: https://www.doeben-henisch.de/fh/gbblt/node95.html). Although this concept looks at a first glance very simple, perhaps too simple, it is very powerful and allows very far reaching experiments (perhaps I can show some aspects from this in upcoming posts :-)).

###### ACTOR STORY

1. There is a human executive actor as a user who uses a program as an assisting actor by an interface with inputs and outputs.
2. The program tries to construct a 2D-virtual world in the format of a grid. The program needs the size of the grid as (m,n) = (number of columns, number of rows), which is here assumed by default as equal m=n. After the input the program will show the grid on screen.
3. Then the program will ask for the percentage of obstacles ‘O’ and energy objects (= food) ‘F’ in the world. Randomly the grid will be filled according to these values.
4. Then a finite number of virtual actors will be randomly inserted too, at least one.
5. After the completion of the virtual world grid a the list of all food objects as well as actors will be shown together with their individual Ids as well as additional properties.

###### IMPLEMENTATION

vw2b.py (main module)

gridHelper.py (import module)

###### EXERCISES

import copy as cp

The module ‘copy’ is important for cases where one wants to make a copy from an object with name ‘N’ and vaulue ‘V’ where the new name ‘N*’ should not be coupled to the old value ‘V’ but to some new value ‘V*’. Otherwise one has eventually many different new names ‘N1, N2, …, Nk’ but all are coupled to the same value ‘V’. Changing ‘V’ in connection with ohne Ni from all the new names wille be valid for all names. To stop such a synchrony between new names you have to use ‘copy’ like N*=copy.copy(N), or with the abbreviation ‘cp’: N*=cp.copy(N). In the source code there is one passage where this situation has become vivid.

def makeobjL(c,objL,mx,n):
ol=[]

In the following line list comprehension is used to collect all objects of type f==c in the matrix together with their (y,x) coordinates:
ol=[[y,x,f] for y in range(n) for x in range(n) for f in mx[y][x] if f==c]

Then for every such collected object the standard values of of the dictionary objL are appended to these objects by copying (!!! see above) these values. This is important because these values will all be changed afterwards independently from each other.
[ol[i].append(cp.copy(objL[c])) for i in range(len(ol))]

Now for every selected object with the  standard values an individual ID is computed and inserted at the first position of the property list.
for i in range(len(ol)):
ol[i]=i

return ol

olA, olA

With these operations we have now two independetn lists, one for food objects (olF), one for actor objects (olA) with individual values for each object. These can now individually  be managed during the upcoming simulation.

###### DEMO

PS C:\Users\gerd_2\code> python vw2b.py
Number of columns (= equal to rows!) of 2D-grid ?5

[‘_’, ‘_’, ‘_’, ‘_’, ‘_’]
[‘_’, ‘_’, ‘_’, ‘_’, ‘_’]
[‘_’, ‘_’, ‘_’, ‘_’, ‘_’]
[‘_’, ‘_’, ‘_’, ‘_’, ‘_’]
[‘_’, ‘_’, ‘_’, ‘_’, ‘_’]

Percentage (as integer) of obstacles in the 2D-grid?50
Number of objects :
12
Position :
4 0
Position :
0 2
Position :
3 2
Position :
2 0
Position :
1 0
Position :
3 2
Position :
1 4
Position :
2 4
Position :
2 4
Position :
2 0
Position :
1 0
Position :
3 0
New Matrix :

[‘_’, ‘_’, ‘O’, ‘_’, ‘_’]
[‘O’, ‘_’, ‘_’, ‘_’, ‘O’]
[‘O’, ‘_’, ‘_’, ‘_’, ‘O’]
[‘O’, ‘_’, ‘O’, ‘_’, ‘_’]
[‘O’, ‘_’, ‘_’, ‘_’, ‘_’]

Percentage (as integer) of Energy Objects (= Food) in the 2D-grid ?20
Number of objects :
5
Position :
0 4
Position :
3 4
Position :
1 0
Position :
3 4
Position :
0 1
New Matrix :

[‘_’, ‘F’, ‘O’, ‘_’, ‘F’]
[‘F’, ‘_’, ‘_’, ‘_’, ‘O’]
[‘O’, ‘_’, ‘_’, ‘_’, ‘O’]
[‘O’, ‘_’, ‘O’, ‘_’, ‘F’]
[‘O’, ‘_’, ‘_’, ‘_’, ‘_’]

Percentage (as integer) of Actor Objects in the 2D-grid ?10
Number of objects :
2
Position :
4 3
Position :
0 2
New Matrix :

[‘_’, ‘F’, ‘A’, ‘_’, ‘F’]
[‘F’, ‘_’, ‘_’, ‘_’, ‘O’]
[‘O’, ‘_’, ‘_’, ‘_’, ‘O’]
[‘O’, ‘_’, ‘O’, ‘_’, ‘F’]
[‘O’, ‘_’, ‘_’, ‘A’, ‘_’]

Objects as food

[0, 1, ‘F’, [0, 1000, 20]]
[0, 4, ‘F’, [1, 1000, 20]]
[1, 0, ‘F’, [2, 1000, 20]]
[3, 4, ‘F’, [3, 1000, 20]]

Objects as actor

[0, 2, ‘A’, [0, 1000, 5, 100]]
[4, 3, ‘A’, [1, 1000, 5, 100]]

STOP = ‘N’, CONTINUE != ‘N’
N