Re: Neural networks
- Posted by Liquid-Nitrogen Software <nitrogen_069 at HOTMAIL.COM> Feb 18, 1999
- 433 views
Here is a simple natural selection program I made last week. It's a text based display. It shows the evolution of a group of organisms. The organisms each have a string of dna. when 2 organisms reproduce random pieces are selected from each parent to form the dna for the offspring. dna values of {0,0} will produce weak statistics, values of {1,0} or {0,1} will produce moderate statistics, and {1,1} will produce high statistics. At birth the dna is used to calculate the organisms beginning health and it's natural ability to collect food. Each day the organisms will collect food from the food source, which accumulates each day. The higher their ability to collect food, the more food they will collect. If an organism doesn't collect enough food to feed itself it will loose health. If there is not enough food in the food source for all organisms, then the organisms will limit how much food they will collect so that the colony has a better chance to survive. organisms can only reproduce with organisms of the same generation. There can only be 3 generations alive at one time. this is to prevent over population. Organisms highlighted purple have just mated. Organisms highlighted red are dead. Organisms highlighted green means that it's the last surviving of it's generation and will lose 1 health per day due to old age, to make way for new generations. reproducing requires that both parents be above a certain age and uses up some of the parents food. reproducing also causes the loss of some health. The age and food requirement becomes higher as the generations increase. You will notice that even though the offspring are produced by random combinations of their parents, higher generations will be born with superrior stats than the begining organisms. I hope I didn't miss anything out. If you have any comments or questions just e-mail me. --------------------------------------------------------- --Begin Code --Dna.e --Liquid-Nitrogen Software 1999 --------------------------------------------------------- include graphics.e include machine.e object junk tick_rate(100) integer ROWS if text_rows(50) != 50 then puts(1,"Couldn't set 50 text rows, must use 25.\nPress any key.") while get_key() != -1 do end while while get_key() = -1 do end while ROWS = 20 else ROWS = 45 end if cursor(NO_CURSOR) integer max_age, number_dead, food, food_growth, days, dpy, als, total_org_days, max_gen, min_gen sequence max_age_tag max_age = 0 max_age_tag = " " number_dead = 0 food = 2000 food_growth = 20 days = 0 --days passed since start. dpy = -1 --deaths per year als = 0 --average lifespan total_org_days = 0 --combined days lived total max_gen = 1 min_gen = -1 sequence orgs, reproduced, old orgs = {} reproduced = {} old = {} --dna = {health,health,health,health, fcol,fcol,fcol} -- (1) (2) (3) (4) (5) (6) (7) --org = {age, health, food, food_collect, dna, tag, generation} --current dna structure: -- 001..020 health -- 021..060 food collection constant vowels = "AEIOU" function create_org(sequence dna, integer g) sequence org g += 1 org = {0,0,90 + (g*10),0,dna, rand({26,26,26,26})+'@', g} if g > max_gen then max_gen = g min_gen = g-2 end if org[6][rand(4)] = vowels[rand(5)] for i = 1 to 20 do if compare(dna[i],{0,0})=0 then org[2] += 5 elsif compare(dna[i],{1,1})=0 then org[2] += 20 else org[2] += 10 end if end for for i = 21 to 60 do if compare(dna[i],{0,0})=0 then org[4] += 0 elsif compare(dna[i],{1,1})=0 then org[4] += 2 else org[4] += 1 end if end for if org[4] = 0 then org[4] = 1 end if return org end function function mix_dna(sequence g1, sequence g2) sequence g3 g3 = g1 for i = 1 to length(g1) do if rand(50) = 1 then g3[i][1] = rand(2)-1 else if rand(2) = 1 then g3[i][1] = g1[i][1] else g3[i][1] = g2[i][1] end if end if if rand(50) = 1 then g3[i][2] = rand(2)-1 else if rand(2) = 1 then g3[i][2] = g1[i][2] else g3[i][2] = g2[i][2] end if end if end for return g3 end function procedure do_day() sequence temp, new integer food_limit, food_get, eater, rok days += 1 food += rand((food_growth + max_gen)*15) if rand(20) = 1 then food += rand((food_growth + max_gen)*30) elsif rand(30) = 1 then food += rand((food_growth + max_gen) * 100) end if if rand(10) = 1 then food_growth += (rand(5)-3) if food_growth < 10 then food_growth = 10 elsif food_growth > 30 then food_growth = 30 end if end if temp = {} new = {} reproduced = {} old = {} for i = 1 to length(orgs) do orgs[i][1] += 1 if orgs[i][2] > 0 then temp = append(temp,orgs[i]) else if orgs[i][1] > max_age then max_age = orgs[i][1] max_age_tag = orgs[i][6] end if number_dead += 1 total_org_days += orgs[i][1] food += orgs[i][1] food += orgs[i][3] end if end for orgs = temp if length(orgs) > 0 then food_limit = floor(food/length(orgs))+1 end if for i = 1 to length(orgs) do --collect food food_get = rand(orgs[i][4]) if food_get > food_limit then food_get = food_limit end if if food >= food_get then orgs[i][3] += food_get food -= food_get else orgs[i][3] += food food = 0 end if --eat food eater = 10 + floor(max_gen/4) if eater > 60 then eater = 60 end if orgs[i][3] -= rand(eater) if orgs[i][3] < 0 then orgs[i][2] += orgs[i][3] if orgs[i][2] < 0 then orgs[i][2] = 0 end if if orgs[i][3] < 0 then orgs[i][3] = 0 end if end if --old_age rok = 0 for x = 1 to length(orgs) do if i != x then if orgs[x][7] <= orgs[i][7] then rok = 1 end if end if end for if rok = 0 then orgs[i][2] -= 1 old = append(old,orgs[i][6]) end if --reproduce if orgs[i][2] > 0 and orgs[i][1] > (orgs[i][7]*30)+20 and orgs[i][3] > 45 + (orgs[i][7]*5) then rok = 1 for x = 1 to length(orgs) do if x != i then if orgs[x][7] <= min_gen then rok = 0 end if end if end for if rok = 1 or orgs[i][7] <= min_gen then for x = 1 to length(orgs) do if x != i then if orgs[i][7] = orgs[x][7] then if orgs[x][2] > 0 and orgs[x][1] > (orgs[x][7]*30)+20 and orgs[x][3] > 45 + (orgs[x][7]*5) then orgs[i][3] -= 45 + (orgs[i][7]*5) orgs[x][3] -= 45 + (orgs[x][7]*5) orgs[i][2] -= (rand(10)+10) orgs[x][2] -= (rand(10)+10) reproduced = append(reproduced, orgs[i][6]) reproduced = append(reproduced, orgs[x][6]) new = append(new, create_org(mix_dna(orgs[i][5],orgs[x][5]), orgs[i][7])) exit end if end if end if end for end if if orgs[i][2] < 0 then orgs[i][2] = 0 end if if orgs[i][3] < 0 then orgs[i][3] = 0 end if end if end for for i = 1 to length(new) do orgs = append(orgs,new[i]) end for end procedure for i = 1 to 15 do junk = rand(repeat({2,2},60))-1 for x = 1 to length(junk) do junk[x][1] = junk[x][1] * (junk[x][2] = 0) end for for x = 1 to 80 do junk[rand(60)][rand(2)] = 0 end for --? junk orgs = append(orgs, create_org(junk, 0)) end for --integer max_age, number_dead, food, food_growth, days --sequence max_age_tag --org = {age, health, food, food_collect, dna, tag, generation} integer disp_wait, wait_max disp_wait = 1 wait_max = 1 integer key, max_disp atom tim key = -1 while 1 do tim = time() key = get_key() if key = 27 and length(orgs) = 0 then exit end if if key = 27 then orgs = {} end if do_day() disp_wait += 1 if disp_wait > wait_max then disp_wait = 1 position(1,1) mem_set(#B8000,0,160) text_color(7) printf(1,"Days:%6d, Food:%5d, Food-Growth:%3d, Years:%4d.\n",{days,food,food_growth*15,floor(days/356)}) printf(1,"Most-Days-Lived: %s:%6d, Number-Dead:%5d. Number-Of-Generations:%d.",{max_age_tag,max_age,number_dead,max_gen}) puts(1,"\n\n") --printf(1,"\n%d \n",min_gen) max_disp = length(orgs) if max_disp > ROWS then max_disp = ROWS end if --org = {age, health, food, food_value, food_collect, dna, tag, generation} for i = 1 to 47 do mem_set(#B8000 + (i+2)*160, 0, 160) if i <= max_disp then if find(orgs[i][6],reproduced) then text_color(5) elsif orgs[i][2] = 0 then text_color(4) elsif find(orgs[i][6],old) then text_color(2) else text_color(7) end if printf(1,"Gen:%3d Name:%s. Age:%5d. Health:%3d. Food:%5d, Food-Find:%2d.\n",{orgs[i][7],orgs[i][6],orgs[i][1],orgs[i][2],orgs[i][3],orgs[i][4]}) end if end for if length(orgs) = 0 then puts(1,"All Organisms Have Died.\n") if dpy = -1 then dpy = floor(number_dead / (days/365)) if number_dead = 0 then als = 0 else als = floor(total_org_days / number_dead) end if end if if dpy > -1 then printf(1,"Average-Deaths-Per-Year:%4d.\n",dpy) printf(1,"Average-Life-Span:%6d days.\n",als) end if end if end if --Comment these next 2 lines out so you can see what happens over a longer period of time. while time() - tim < .3 do --Waiting... end while end while ------------ --End Code-- ------------