Contact
CoCalc Logo Icon
StoreFeaturesDocsShareSupport News AboutSign UpSign In
| Download
Views: 1419
Kernel: Anaconda (Python 3)
import numpy as np import matplotlib.pyplot as plt %matplotlib notebook plt.style.use('ggplot') import random from random import randint import math

System Parameters

food_amount = 100 pacman_amount = 1 board_size = 100 default_health = 5 pacmen = [] food = [] crot = 1 cloc =.2 cbite = 1 turn_loss = 1 phi_prec = (80/255.) alpha_prec = 10/255. delta_prec = 10/255. pacman_radius = 1 food_health = 5
class pacman: def __init__(self): #change dynamically self.x = randint(0,board_size) self.y = randint(0,board_size) self.theta = randint(0,359) self.health = default_health #dna self.phi = randint(0,255) self.alpha = randint(0,255) self.delta = randint(0,255) self.phi_val = self.phi*phi_prec self.alpha_val = self.alpha*alpha_prec self.delta_val = self.delta*delta_prec #self.f = closest food #self.dist = closest food distance def __bool__(self): return True def __repr__(self): return "{pacman, x:"+str(self.x)+", y:"+str(self.y)+", phi:"+str(self.phi)+"}" def find_dist(self,f): return np.sqrt((self.x-f.x)**2 + (self.y-f.y)**2) def find_angle(self,f): dx = f.x - self.x dy = f.y - self.y theta1 = 360. - self.theta - math.degrees(math.atan((dx/dy))) theta2 = 360. - self.theta - theta1 print(theta1) if(theta1 < theta2): return theta1 return theta2 def check_angle(self,f):#check if the food is in the range left_bound = self.theta - (self.phi_val/2) right_bound = self.theta + (self.phi_val/2) f_angle = self.find_angle(f) if(left_bound > f_angle and right_bound < f_angle): return True if(left_bound > f_angle + 360. and right_bound < f_angle + 360.): return True return False def find_food(self): global food closest = False closest_dist = False for i in food: dist = self.find_dist(i) if((dist < closest_dist or not closest_dist) and (self.check_angle(i)) or True): closest = i closest_dist = dist self.f = closest self.dist = closest_dist if(closest != False): return True return False def rotate_to_food(self): #cost of rotate = crot*theta*health #no cost to rotation new_theta = self.find_angle(self.f) error = random.uniform(self.alpha_val/-2., self.alpha_val/2.) self.theta = new_theta + error def get_food(self): self.rotate_to_food() error = random.uniform(self.delta_val/-2., self.delta_val/2.) new_dist = self.dist + error angle = self.find_angle(self.f) self.x += new_dist*np.cos(angle) self.x = 400 self.y += new_dist*np.sin(angle) cost = cloc*new_dist self.health -= cost #cost of rotate def bite(self): to_food = self.find_dist(self.f) self.health -= cbite if(to_food < pacman_radius):#means the bite was sucessful self.health += food_health food.remove(self.f) def random_rotate(self): self.theta = randint(0,359) #self.health += randint(-2,1) def step(self): self.health -= turn_loss if(self.find_food()): self.get_food() self.bite() self.random_rotate() def get_genes(self): return[self.phi,self.alpha,self.delta] def set_genes(self,genes): self.phi = genes[0] self.alpha = genes[1] self.delta = genes[2] def to_bits(self): genes = self.get_genes() split_genes = [] for i in genes: bin_str = format(i, '#010b') bin_str = bin_str[2:] split_genes.append([bin_str[:4],bin_str[4:]]) return split_genes def reproduce(self,other): son1 = pacman() son2 = pacman() son1_genes = [] son2_genes = [] mybits = self.to_bits() otherbits = other.to_bits() for i,v in enumerate(mybits): son1_gene = v[0]+otherbits[i][1] son2_gene = otherbits[i][0]+v[1] son1_gene = int('0b'+son1_gene,base=2) son2_gene = int('0b'+son2_gene,base=2) son1_genes.append(son1_gene) son2_genes.append(son2_gene) son1.set_genes(son1_genes) son2.set_genes(son2_genes) return [son1,son2] p = pacman() p2 = pacman() print(p.reproduce(p2))
[{pacman, x:86, y:89, phi:181}, {pacman, x:67, y:66, phi:55}]
class dot: def __init__(self): self.x = random.uniform(0, board_size) self.y = random.uniform(0, board_size) def __repr__(self): return "{dot, x:"+str(self.x)+", y:"+str(self.y)+"}"
def gen1(): global pacmen pacmen = [] for i in range(0,pacman_amount): p = pacman() pacmen.append(p) pacmen = np.array(pacmen) print(pacmen[0]) return pacmen def food1(): global food food = [] for i in range(0,food_amount): f = dot() food.append(f) food = np.array(food) return food
def driver(gen,life_span,generations): food = food1() dt = 1 t = [0] tracker = [] global pacmen for g in range(0,generations): #iterates through each generation tracker.append([]) for i in range(0,len(pacmen)): tracker[g].append([]) t.append(t[-1]+dt) for q in range(0,life_span): #runs through time with current generation for i,v in enumerate(pacmen): tracker[g][i].append([v.x,v.y,v.theta]) v.step() #makes the next generation sons = [] healthy_pacmen = [] for i,v in enumerate(pacmen): #end of a generation mean reproduce if(v.health > 0): healthy_pacmen.append(v) for i,v in enumerate(healthy_pacmen): if( i % 2 == 1): temp = v.reproduce(healthy_pacmen[i-1]) sons.extend(temp) #print('generation over') pacmen = sons return t,tracker
t,tracker = driver(gen1(),10,1) #print(tracker[0][0])
{pacman, x:67, y:77, phi:52} 263.5206654046971 280.90858284436945 289.3060238383546 312.58911836807846 318.2527553136118 357.59514264881625 231.35988590048692 190.56070476453618 239.54516887866865 262.2616401859034 216.8928508215002 256.6469532539659 193.3660130014478 365.5313742190705 194.6486339429472 217.30004675424482 211.63534195247934 361.9547070477688 214.01428330165984 216.15385559839225 344.95091764643075 366.4132067446574 345.116386932689 316.7710592544355 264.3461994582524 223.3033829633468 226.58295610986514 319.4434441299796 213.68655639652883 187.89796729366674 276.0546321730351 249.9774657754155 227.7628736564204 363.87603056380107 191.02775957234925 252.38192422629947 345.7424466526589 189.5200494315498 254.31958705513284 206.42781962516605 317.1173237398926 244.5725232222342 204.9130362276265 311.281630855251 220.22523980843175 235.00767008839676 338.4154956705046 296.3382940675976 397.9422421075771 7.68378126140378 6.909672635589189 7.696185140659253 8.495919048943009 178.42929009179153 180.78457747421777 178.45924287412151 176.7789727666705 175.38950453552425 181.99720861194533 176.1179583862816 8.796580600487943 4.846636151983176 4.187456474995031 4.287462459406257 176.9639526792213 4.413456105625045 181.00471160802482 178.02413712507058 182.7179634927157 176.4092879503591 9.871581508218085 175.66422091012714 6.969100945877301 179.1251639841193 5.595356082438684 7.279672336191084 4.999741206850572 180.67734360941512 3.6962523093835813 8.384478323559875 179.98510213204318 177.96003105941242 9.694257409054558 182.23849966248434 182.02980697748197 5.341566418411759 177.40886846405238 182.86244181992907 5.712562862984157 180.72253183118124 11.588623627879485 176.71040279835748 175.65729353400002 181.20691785788893 5.120199801520073 9.551807563238285 173.5553700237802 179.25256265913174 8.677637062097702 8.677637062097702 267.3096941146285 2.112030004962257 2.5181164298851826 1.7588162046214748 4.349850223504902 -7.905714594942339 -2.7361080846840906 -1.7435609983201061 -2.5806844353612917 -5.810952933843154 -7.521256308073291 -6.780146844523088 -6.68506684907328 5.167544948174587 -5.701995940949715 -0.25659233825683714 0.37030733789966064 2.0974864314248123 -3.9567576171094885 -4.208786655158093 -1.917604498258882 -4.691952533730031 -3.3942656613548507 -4.753223161922975 -3.902365689031484 -8.705975764767928 -10.43172608392041 4.266480360771297 -2.1558011354894404 1.8712840318181918 -2.3024965271885094 -2.074566436571544 -1.828357407486891 1.0832421974219386 3.4025585218710646 -4.248245081022915 -4.4980958021725 6.118583263254756 -1.7405068878557302 -2.3614680395361063 -6.8119287850364 1.9958969929302341 -3.86252879258231 -1.725790893217578 2.5038697517415685 -5.209932855299471 7.927808704622336 -6.024968672822823 -6.316307693166692 -5.425070208699012 2.706855254950618 5.013522879197282 -7.719384943309521 -3.0244977456343065 5.006743622338966 293.83983515525904 244.46459611543062 244.06534831990956 244.3531989253231 245.80172507788978 236.2963900460981 238.31232507753268 236.36887900628682 234.11441907165516 235.42383357838258 412.7972363037621 233.35904585570438 246.29828449358712 233.70823366040094 241.68292836468095 241.429156172189 242.0888560876434 234.758493967755 240.83049685895085 237.51533608782916 235.78952406300758 238.98871832264894 412.9481744394041 411.88259954699663 244.71690765929753 236.9878418241995 244.10151686639725 241.19311517098697 238.20777866581977 241.32509310363957 245.3939937281374 236.92630817564162 235.3876577294929 247.25499019891026 239.3485325982939 238.99674802069245 242.79201364373608 235.1927663204391 239.7960905065447 243.2263735461818 237.14930665353398 249.20438258965277 233.99721702198104 233.13796029702291 237.42821018897726 242.88319131609848 246.762864669794 411.1584387301356 236.7950337960478 246.1592254574685 206.29089676862975 108.00851040867664 109.02418790145623 107.52022034853019 110.46216217956012 102.37692179356333 107.48939315064226 107.26044481997084 107.66809973339834 104.55350268798841 100.12764780766007 104.2363425495902 103.83390855517808 105.12970371787726 99.11956007449945 102.35286397196447 107.87518003932418 110.02908520753412 106.32249257604869 106.55356176144144 103.04533484715704 106.90678452892476 103.78340289816867 102.92329265318153 100.70616862741058 99.55266931310156 98.69951275423912 111.21142403912455 102.24180812338076 107.78613531298137 107.93715124336109 103.40915669015526 103.93049249952996 107.21866657662599 109.09892544581902 109.29996804199146 105.99579319330445 104.41998759120776 105.49746505597166 98.64501980005815 103.03825531374227 105.60840529577817 102.7853068903132 106.70637707926085 105.91704252406703 109.71512168579345 102.50064016630664 113.25119417598842 101.56007958663318 104.31042146997302 104.671485231496 101.81036162630646 110.31178249790098 110.71085241480849 104.30526104842662 106.58056270217205 111.19099983602533 283.3962491414901 169.3559039248456 170.23832491948775 171.00267973377893 167.66607069692702 170.06512457999202 170.73231225982886 168.96088374722802 165.1661756795081 169.01661572253303 168.60148028234434 169.46261484546875 164.90905402547887 166.94664566994007 171.36863509759087 169.9589763621837 170.25808000955024 166.7460114054073 167.0047751816502 170.30656019372137 167.79348920171304 166.52208897851597 166.3241170383219 164.87169618041094 165.00954437876115 167.56270680056588 169.3781179934983 166.31393433309353 166.78665509639984 170.05539774632902 166.4059111198157 170.18257134863484 168.54522429927147 168.23135468361085 169.37657688983342 164.8477259454212 167.54556219606098 168.676888300497 167.62940612285288 170.28100344242867 168.73724105457987 166.6572857088492 167.21447301254074 169.25529148078306 166.01354767393693 171.4315970005624 171.00954955794202 166.3655808628637 169.4135446393984 169.87391987400156 171.48990740749144 317.70666893650616 129.58424218552406 130.10298894088677 128.44322463348158 130.46056027405302 129.21193829332483 130.24888860720685 130.681083401133 127.67920872583802 129.83560897889882 127.6972312791123 129.92204907031967 129.67879578844213 130.11755486377356 127.67816204511142 128.6789384975064 128.86118705792703 130.80881538213507 130.3082111346116 130.4929235216701 128.38701163644376 128.60671938862885 130.47839901400093 129.0859947688678 128.252570638689 128.48027939894988 127.5724473902107 127.82679030895356 129.15658494060543 127.64174763257228 129.61410678164208 127.99694086519933 128.27774141861042 130.2479128208617 129.2965000905727 128.40746078195338 130.0132858902402 129.29521699055104 129.31432918775224 130.00156346358224 127.7138785483245 129.01926667879763 129.46075044738328 129.12193054010905 130.48812134502134 129.45646277902756 128.43253525129865 129.00507741084655 130.03264051896718 128.0891854265348 130.19711975571514 130.20825605668708 130.72043207309085 338.1655639983099 250.82389327106227 251.8479987590134 250.33586866173619 253.26138939906858 245.38065410405224 250.43374701841216 250.17307389154288 250.61101644840534 247.5354763334592 243.06839574581528 247.24130110741555 246.82863808898531 248.11878117575867 242.09479924062435 245.30315377705566 250.73863907676446 252.88253284132742 249.28183854240106 249.5238937258965 245.96218392780793 249.86000763151165 246.71817978297724 245.8120574037125 243.69983423274684 242.50273628240612 241.70395147532506 254.02611257043174 245.24507637718006 250.71921118165616 250.7581628955928 246.2577422492473 246.78425012780554 250.13296849245899 251.9643960993466 252.103831222186 248.8636474711213 247.35240131517315 248.4579767473503 241.64068996055687 245.99570566893718 248.5078386422111 245.76326836714995 249.66946516891034 248.80366396490186 252.55205683088457 245.42171622754466 256.00285810257645 244.57445266786425 247.29427103155422 247.67049471279796 244.72229144103474 253.15343688597247 253.4962209700238 247.33906211585884 249.5177491104402 253.9853065942064 282.9082307295594 225.9122452658114 226.94538930635903 228.05607043217472 222.48371502220604 226.6511507441401 226.144334074016 226.8029318298734 224.28930981851698 219.7727999968002 224.21207314204494 223.74471295648084 224.88762382534614 219.17908735225205 221.94387913518733 228.16287146418253 225.7121079494645 226.02694834919046 222.26083493458515 226.19499759477057 223.12432906142558 221.8677071019119 220.83816073246555 219.32946592999332 219.0807241808801 222.3543031937305 226.82134663185846 225.90014501589397 221.91966103118833 222.4621603155753 226.1212692045977 227.01179350851265 224.53810627620697 223.69377707528037 224.96038393922055 218.94228223572247 222.65114170119023 224.48244629117113 222.6144748375979 226.10111201717203 224.64662427693878 227.7156481546669 221.79386012013848 221.83044573202562 224.08235036520603 224.557646774415 221.05720047152286 228.31592514186477 228.15665466718337 224.5431071996659 225.74423441786342 228.69128159536726 302.0216587872447 12.787506597341903 191.80408177195892 12.872414911576328 13.278546266638969 183.04797539365165 185.55535024538872 183.05394540415077 181.71467173842026 180.30564350304212 187.5424047927781 181.11428560540799 13.458151662185955 189.9544951711535 189.0708270591797 188.84699146260243 181.64606666237165 13.702420650005521 189.76764738454466 186.34413793780334 182.7054200757453 188.17413610236758 181.7848004557141 15.151008670034813 181.2225093212794 191.51503325903866 183.73495239945768 191.12402863798096 12.364565870936389 190.47609522624433 185.44822680658012 188.3646392258593 12.39507919289845 13.344108211290134 185.0955405070403 182.82828310915068 14.305987353327325 187.22421467577428 187.09937749624294 190.08730910826552 182.0897395072857 14.852982815695228 187.9400850881105 190.415755963882 186.1121962039411 16.135345833573908 181.67362491783928 180.53007857319835 186.70686253096162 189.68811693902109 14.374942664072748 178.39346456154578 184.03819504617883 13.35307488632563 13.35307488632563 257.2097806737936