Assignment 1 : Greenfield                                                                                  Chris Avery

Abstract: In this project our main aim was to use arrays, both single and multi-dimensional, in order to simulate an environment.  Inside this environment were entities that each had a spot in a 2D array and had an ID number assigned to it.  We needed to be able to make each entity check for other entities around it and also check their id number, and if there were more entities of other types than there were on itŐs own type that entity needed to try and move.  To do this we needed to create a class to handle the Entities, a class to handle the 2D arrays, and a class to create and run a simulation.  We were also using an already created class that gave us a random integer.  When the project is completed the only class you need to run is the simulation class, as it calls on the other classes to do its job.  If done correctly, you will be able to see that, eventually, the different types will get segregated into groups of just themselves, away from groups of other types.

 

Problem Description:  What we are doing here is basically just creating a huge grid.  Within this grid we are placing several entities, each with itŐs own way of identifying itself (i.e. some may be 1Ős while others are 2Ős).  We then want these entities to move themselves elsewhere in the grid if there are more entities of the other type around them than entities of their own type.  After enough rounds of movements, many of the entities will begin to clump together with their own type.

 

Theory and Algorithms: Many of the things necessary for us to do in the many classes of this assignment could be done using for loops, while loops, and if-else statements.  One such example would be in our simulation class, while we are trying to initialize the 2D array.  In pseudo-code:

For each spot in out array of entities

     We select a random row and column in our 2D array

     While the spot specified by this row and column is taken we must select a new

          Random row and column, and keep doing this until the spot specified isnŐt taken.

     We select a random ID number

     We create a new entity using our row, column, and ID number and place it in our

          array of entities

     We place this entity in the correct spot in our 2D array

We can later use a for loop to go through every entity we have in our 2D array and use multiple if statements on each one to check the surrounding area to see if it is surrounded by the same type or not.  Finally, we use if statements to see if it wants to move or not, and if it does we move it.

 

20x60 array with 500 entities

 

.........1....2.1.....22.1..22.111....1.....1....22....1.22.

1.1.1.111.111.21.1.1.21.1.2..1.2.1..1..22...11.1...2....1.21

.1.2...2.2.....1.22...1.12.1.2.2.1.2.2..2.....21.2...2.1..2.

.22..2.22.11.12...111...2212....2.21..2111..1.11....222....2

.1..2.12..2.1..1.1.22..122.2..1..22.2..1........1.1..22.....

2.11...22.221.1....1...1211......1....2......22........12...

21...1.....1.12.12121221..1.12..2..2212.1..22...11.11.1...11

1..12..2....2........1.....211.....122.212121.....22.1.11222

1.1.1.1.11.11..11......2.2..1.2.1111...21.1.2.1..2.1........

2.1.11....2....2..12222..1..2222..1..1.1.12....111.121.21.21

.1..22....1..2.1.212..2.1...1..1.22..1.122.1.11.2.121.......

2..2..1.121..2....1212122.......2.1....2.....1......1....112

..2....1..2.21..1........1...2.212.2.1.2.2......1.....1.....

2..2.2......11...21221..12....11.2..112...12...2..11.21..21.

...1...1.2.2.......1..2.22..2....2222..1.1..2.12.121..2...22

1.2211......2211.2....1.1...2.11..1.......2.2...22..........

12....22....11..1.1.221....22.21....2.1111...12111.....1.2..

.1..2...2....2.2.21.2...1.221.2.....1.2...2.22.2211....12...

...21........12.1.2.11...212.2.1.1212...21....1..22.1..121.1

..2..22.2.2.21..122121112..1..2.1122..1..1...1...........112

 

20x60 array with 500 entities after 50 iterations

..2.....21..22.21....2...1.2.2.1112...1...221.2.2..22221...2

1.1.1.111.111..1.1.1.21.1....12221..1.....2.11.12.2...221221

.1.....2.......1......1.12.1.2...1..222...2...21..222..1.2.2

2......22211.1.2.2111..2.21....2.221.2.1112.1.112.....22...2

.1.2..122...12.1.1.....12.....1........1......2.1.1.22...22.

..11...222..1.1....122.1211....2.1....2....22...22..2.21....

21...1.....1.1..1.1.1..1..121..2..22.1..1.2.....11.11.12..11

1.21........22..2...21......11..2..12.2.1.1212..2....1.11..2

1.1.121.11.11..112.2..2...2.1...111122.21212.21..2.1..22..2.

..1211.2222....2..1...2..12.......1..121.1....2111.1.12.1..1

21.....2..1.2221..12..221.2.1..12..22121...1.11...1.12.2..22

.2..221.1.1.......121212....22....1.2..2.....1....22122..11.

.2.....1.2.2212212.......1......1.22.1....2.2..21...2.12....

.......2....11....1..1..1.....11....11.2.21.2..2..11.21..212

...1...1.....22...212.2.....2...2222...1.1....1.2121........

1...112.......11...2..1.1.....112.1...2.2.22.2...2..2..2.2.2

1...........11..1.1...1.......21..2...1111...1.111.....1...2

.1..2.2...22...2..12.2..1...1...2...1.2..2.......11.22.12..2

2..212...2..212.1.2.11...21....1.1.1.....1.2.212..2.1.2121.1

.22......22..12.1..1.111...1....11....1..1...1......2....11.

 

15x40 array with 200 entities

..22..21.111....2..1...1.1...1..........

1....2......1.1.1....111...2.21.1.22..2.

...1..2....22..2...12.1.2.1..21.......1.

..1..2.....2..2..1.1.2.1.1..2..2.2....12

1..2....1..11.2...1.22...1122.....1.2.1.

.........2...2.21.2...2..21.........2.1.

....2...1...1..1..2111.22....1..12.1....

.1....21.2..11..1.....21...1.222....1..2

222...........12.1.........1.......1....

2..12.1...11.12..2.2..1.1221....1..2.11.

....2.2............1.......2121211.1.1..

1......2.2..121.1.122.12.1....1.2.1..2.1

....2..2........2...1...2....121...1..2.

.2.1..1.....212.2...22...112..2...1.....

.........2.2..11...2..1........221...1..

 

15x40 array with 200 entities after 500 iterations

......21.111..11...1....1...212..21.....

...22......21..............2.21.1.2.2122

122...22.1..2.......2.1.1.1...12......1.

....2....2....1.2..1...12...2..2.....21.

1...1...1..1....1.1......1122.2...2.1...

.1...1.........2.2.2.2......1.........1.

.21.1....21.1.......11.2.....1..12..1...

.1..2.21..21212...1..1.1...12......12.1.

2...1...1.1...1..1.........11.2....1..2.

2..12.1...1..22....2..121.2...1.1....11.

.1.2.....1...2....2..........2121..1..1.

1......2......121.122..2......12..1....1

.....2.21.....212..21...2..2.1.1...11.2.

.2....1.2....1.22.22.2..2.1222...11...2.

...22..2......11..2...121..22..2.1...1..

 

30x90 array with 1500 entities

1.221.12..12.121121211.11.212.1..2.1112.1........222.222.2.112121..1.111...2211....1.1.1.1

1222112..12.222...2..22.11.2.1.2.2....1.121........1.1.222121.1..222.2.212....12.1112.2..2

...1..2.212121.2.1211..1...222112...122.1..1.1.2.11.11.2.....1..12..1.2211..1.1..212...11.

2..111112....11..11121.2211..1121212111.2.2..22.2.2.12...221..2111.12...12.1.111.1.1....12

22..1.211.12.1...1.1..2211..1..2.22..11.22..121...2.21..1122.1.22.12.2.2212.2........2121.

22121221...22.2...2.121....11..1.1111..12.2.212.1.1......1.1..12..2.22...1211.11.2211...1.

.21..121.2.221..212......2122.2..21....212.2..1112..1.12.2..2.22..22.11...11..121..11.2..1

1..21.2...2....2211..22.21.221..211...211..122..221..221..212.11....1..1.12..211..12.2.221

221.1..1..1.111..2...1.2211211..22.1211....1...11122..1.11111222.12...2.1.22.2.222....22.2

...211..21..111112....1..22.11.2221....1........1...1..2..2.22.1...222222..2.1.1.22.112...

12..1.2.121..21.211..22.122211......1.1...11.12212.21.1.1..122.2.2...221...21211.1.11..212

2.222..1112121...22..212.211.111.1...22.212.2..12.2.....11.1.11...12..1.1.1.2.1.2.1.1..2..

1211.........2112.22.11..122221.1..2.2.22111..1.2.22....122.212211.212.1222.222..2.22.2.2.

.21.11.1.2121121..1.1.21.21.11221122..12122..211...11...2..2.....1.1111.2...21.1..1.1221..

12.11.2.2.2...1..2111.11.2....1..111.212.212..1......12.2.2..222..11..11..221...1.21..122.

..2...1..2..122..2..21.2.2...11211.2....1.22111.1..11.111.22....2.211.2.12.1112..22...2121

21...22.111.2.2.222.12222.2.1.2..1...2.22.1.2.1...21.21.1.2222221.2.12.1..12.1.1..21..1122

221..1221...1..12.2....1211..1..2.2....2....2.2211...2.1111...1.22.1121112..12.22...1..212

.1.1..222..21..11..1.1.1122.22.2.1..1.2.1.2.2222..12112...22..2.2.1....12..22.1.1..21.2..1

1..1...21222.22.11..2.....11.111....2..2212.21.21.122..1.11..12221..2..222112..12..11221..

.1....21.1.2..2.12.1.2....2..2221..112122122.1.1..1...2.....222112.11221...211..21.12..112

1.21...2.1.212.221.2.121..2..2.22211.112.....22.11112.1111...1.1..1.211.112..1.12...1222.1

22..11...2..2.22..11..12122122221211.1121122...2...1.1.2.1.21..21..221...2.2.122..12..2.11

.1..1.2..1112..1221.12.111.2.11..1..11.121....2......22...2.2.1.221.....1..2.1..12.....211

..1..2...1..22111.22.22...211...2..2.1.22.1.22..1221.1.22.1221..2.1..1...2..1...1.1..21..1

.2..2..2....22.2...11.2.2211..222.211.2.12...211121.221.1.1.11..221.122.12.2211...112.2.2.

...1.222111.11..11..222..111.........222.1211.22...2..12221111..2...1122.12.2.2...12122.22

.1.1..1222..21...211....221..2112..211.1.1112.21121.1.12.1.....212122.1112.112.1.....2221.

212.2.11111221.1.121.22..2.11221112121...111122121..2112..12.2..22111.2...22.11.1.11..21..

212...21..2122.2111.2.12..1..12.2..212.121...22222..1.2......122.12..211..2.2122..212.2111

 

30x90 array with 1500 entities after 5000 iterations

1..21212..12.1.11.1211.11.2122122..111.21.2...22.222.....2211.121.21.111212.21122.212121.1

1..2112..1.22..2.222...211.2.1..22...2121.12..2....1.1.2.21.1.1222..2...12.2.21..111.22.2.

..21.2.2.1.121.2.1211..12....211....1..212.1.122.11.11...2..21.212..122.11..1.1.2.122.2112

2..111112....11..11121...11..11.121.1112..22...2.22.12.22..1222111.1..2.1221.1112121..2.1.

22221.211.1..1.2.1.1....112.122.....211.2..21.1.22.221.21122.1..2.12.....1.2.2.222....1.12

2.1.1221..2........2121....11..121.11..1.....1.21.12...221.12.1..222..2.21211211...11...1.

2.1.21.1..2..1...1.2..22.21...2.2.1222..1.2...1112.2121.2.2.2..2..2..11222112.1.12.11.2.21

12..1.2.2.22.....112...2.1.2.1.2211.2..11..1222...1.2..1222122112...1..1.12.2.11..1..22221

..1212.12.1.1112.22.21..211.11...2.1211...21...11122..1211111....1.222.212...2..2222...2..

....112..1..11111...221.2...112..21....1..2..2..122.1....222.2.1.22...2..22.2121....112222

1...1...1.1...12.11..2..1.2211..2.221.122.11.1221..21.1212.1222........12...1.11.12112..1.

22.22.211121212....22.1.2.11.111.12.....212.2.21222..2..1121.11...12.21.1.12221.2.1.12.2.2

1.11..2...22.2112.2..1122122221.1........1112212.2.2..2.12.2.12.11..1.21.2.222..........2.

..1.11.1..1211.1..121.21..1.112.1122.21.1.2..21122.11.......2...21.111122222.1212.1212.1..

1..11.........1...111.1122..2.122111.212..12.21..22..1..2........2112.11....1.221.21..12.2

2.....1.2...122.2.22.122.2..211211......1...11121.211.111.2..2....211...122111..22....21.1

.1......111.2.2..2..1.222..21..2.1..22....1...12...1221.12..2.2.1.2.1221.21221.1...1..11..

..1.21..1.2.1..122..2..1.11..1..2.....22..2.222.11..2..11112.21.2..11.111.2.1.22.2.21...1.

21.1..2.2.2.1.2112.1.1211.2..22..122122.1.2.22222.2.1122...2.....21....1.2222.1.12..12.221

1.21.2..122.2...11..2....2112111...22.2221.2.12.1.12.2.1211.212.212.2...22112..1.2.11.2122

.12....121......12.12.2...2..2.21..11.12.1..2121..12..2.2...22.11..11..1.22.11.2.1.1.2.11.

1.21...2.1..1.2.212.21.1...222222.11.1122.......1111.211112..1.1..12211211..21.1.2..1..2.1

.22.11.....2..2...11..1.1221....1.11211211.22..2.221.1...12212.21.2..1...2.2.1.2.21...2211

.1.21.22.11122.12.1.12.1112.211..1..1121.122....2..2.2..2..2..1...1....212..212.1......211

..1..222.1....11122.2....2211.122.2.212...12222.1..1.1.2..1..12.2.1..12...2.12.2121..21221

.2...2...2..222222211.2...11....2.211...1.22.2111.1..212121.11.2..121...12.2211..211...222

2.212.22111.11..112..222.1112..22....2...1.1122222...212..1111.222..11...12...2..21.1..2..

21.1..12.2..21....11......1..21122..11.121112..11.1.1212.1..22.21.12..1112.112.1.2...2..1.

21.22211111..1.1.121.22.2..11.2111.1212..111122121..2112.21222....111.22.2..211.1.11...122

.1..2..1..2122..1112221.2.1..1.2.22212.1.1.......2..12.2..22.1..21..22112..2.1.2...1.2.111

 

Results:  As you can see from the before and after, the arrays start out completely jumbled and after running a few iterations they begin to get more organized by ID number.  Depending on the size of the array, the number of entities, how far they are allowed to move, and many other factors, it vary on how long it will take, but after enough iterations the array should reach an almost constant state of equilibrium.

 

Discussion and Summary: These results are interesting because they can be used to show things such as why many neighborhoods or areas in a country can end up segregated.  Even if  people have very small dislikes and likes they are looking for, they could still end up in an area completely away from any diversity.  I ran into trouble a couple of times along the way in this project, mostly on minor code problems that I somehow missed, but once I was able to get by them I feel like I met the goals decently well.  Is my code completely perfect?  Probably not, there are probably a couple spots that could make my program better/faster, but I believe what I have gets the job done just fine.