人类社交模拟(python实现)

基本理论 算法实现

Code: humans

# -*- coding: utf-8 -*-“””Created on Thu Sep 24 10:48:48 2015# Human@author: Zhang_Jun”””import randomimport math::self.ID = ID # 编号self.sex = random.randint(1,2) # 随机产生性别self.x_before = 0 ; self.y_before = 0 # 个人状态描述(前)self.x_after = 0 ; self.y_after = 0 # 个人状态描述(后)self.vector = [self.x_after – self.x_before , self.y_after – self.y_before] # 个人生活方向self.ability = 1 # 发展潜力,决定了个人发展方向的模长 self.age = 0 # 个人的年龄self.friend = [(ID,self)] # 个人的所有好友列表 (编号,包含好友所有信息的对象)self.friend_distance_sex = [(ID,0,0)] # 好友的亲密度与性别属性 (编号、亲密度、性别同性与否[0:同性,1:异性])self.mate = [ID,self,100000] # 个人的配偶 ,配偶编号 以及 亲密度self.stage = 1 # 个人的生存状况 1 为 生 0 为 死self.time = : # 社会刚形成期间,每个人随机发展,,不受他人影响for i in range(step): # step 为控制发展次数的参数()self.time = self.time + 1 # 每step一次,增加一个时间单位self.x_before = self.x_afterangle = random.randint(0,360)/180.0 * math.piself.x_after = self.x_after + self.ability * math.cos(angle)self.y_before = self.y_afterself.y_after = self.y_after + self.ability * math.sin(angle)self.vector = [self.x_after – self.x_before , self.y_after – self.y_before]:self.time = self.time = self.time + 1self.x_before = self.x_afterself.y_before = self.y_afterfriend_effect = [i * coef_friend for i in norm(sum_direction(self.friend))]angle = random.randint(0,360)/180.0 * math.pirandom_effect = [i*coef_random for i in norm([math.cos(angle) , math.sin(angle)])]direction =[i * self.ability for i in norm(sum_list(friend_effect,random_effect))][self.x_after,self.y_after] = sum_list([self.x_before,self.y_before] , direction)self.vector = [self.x_after – self.x_before , self.y_after – self.y_before]self.ability = sigmoid(inverse_sigmoid(self.ability) + coef_random * random.randint(-5,5)+ coef_friend * inverse_sigmoid(friend_ability(self.friend,self.ability))): /(math.exp(-0.1*x)+1):return 10*math.log(y / (2.0-y) ):return [x+y for x,y in zip(a,b)]:direction =[0,0]for friend in friend_List:direction = sum_list(direction,friend[1].vector)return direction:ss = i in L:ss = ss + i*isss = math.sqrt(ss)return [x/sss for x in L]:fri_ability = [fri[1].ability for fri in friend_List]:delta =1e-10P1 = [human1.x_before,human1.y_before]P2 = [human1.x_after,human1.y_after]P3 = [human2.x_before,human2.y_before]P4 = [human2.x_after,human2.y_after]if (P2[0]-P1[0])==0: # in case of denominator equals 0 (避免分母为0的情况)if (P3[0]-P4[0])==0::k2 = 1.0*(P4[1]-P3[1])/(P4[0]-P3[0])b2 = -k2*P3[0]+P3[1]P = [P1[0],k2*P1[0]+b2]if (P[0]-P1[0])*(P[0]-P2[0])<=0 and (P[0]-P3[0])*(P[0]-P4[0])<=0 \and (P[1]-P1[1])*(P[1]-P2[1])<=0 and (P[1]-P3[1])*(P[1]-P4[1])<=0 ::(P4[0]-P3[0])==0: # in case of denominator equals 0 (避免分母为0的情况)if (P2[0]-P1[0])==0::k1 = 1.0*(P2[1]-P1[1])/(P2[0]-P1[0])b1 = -k1*P1[0]+P1[1]P = [P3[0],k1*P3[0]+b1]if (P[0]-P1[0])*(P[0]-P2[0])<=0 and (P[0]-P3[0])*(P[0]-P4[0])<=0 \and (P[1]-P1[1])*(P[1]-P2[1])<=0 and (P[1]-P3[1])*(P[1]-P4[1])<=0 :::k1 = 1.0 * (P2[1] – P1[1]) / (P2[0] – P1[0])k2 = 1.0 * (P4[1] – P3[1]) / (P4[0] – P3[0])b1 = -k1 * P1[0] + P1[1]b2 = -k2 * P3[0] + P3[1]: # 求解两直线交点P[0] = (b1 – b2) / (k2 – k1)P[1] = (k2 * b1 – k1 * b2) / (k2-k1)(P[0]-P3[0])*(P[0]-P4[0])<=0 \and (P[1]-P1[1])*(P[1]-P2[1])<=0 and (P[1]-P3[1])*(P[1]-P4[1])<=0 ::return 0莫愁前路无知己,天下谁人不识君。

人类社交模拟(python实现)

相关文章:

你感兴趣的文章:

标签云: