Advertisement
Guest User

Untitled

a guest
Nov 18th, 2019
149
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Python 6.15 KB | None | 0 0
  1. #!/usr/local/bin/python3
  2. import numpy as np
  3. import multiprocessing as mp
  4. import random
  5. import statistics
  6.  
  7.  
  8. #types of issue:
  9. #How complex an issue is - how long to go from a shallow understanding to a deep understanding
  10. #Each issue could have many solutions - we'll approximate that as a number (opinion), then quantise when we 'vote'
  11. #How many solutions there could be - e.g. 1 - 2 - 10
  12. #
  13. #How the issue is split accross individuals (who believes in what solution)
  14. #
  15. #Individuals:
  16. #How ‘deeply’ they understand the issue (how long it would take to understand the issue)
  17. #How ‘stuck’ individuals are of their view
  18. #How persuasive people are
  19. #
  20. #Who interacts, with whom, when, and how.
  21.  
  22.  
  23.  
  24.  
  25.  
  26. issues_spec= [{"name":"a", "complexity":60, "solutions":5}]*2+\
  27.              [{"name":"b", "complexity":30, "solutions":3}]*3
  28.  
  29. population_spec = \
  30.                   [{"name":"mp", \
  31.                     "opinion":{"a":30, "b":80}, \
  32.                     "understanding":{"a":20,"b":90}, \
  33.                     "eliquence":{"a":10,"b":60}, \
  34.                     "attain":90 \
  35.                   }]*650 \
  36.                   +\
  37.                   [{"name":"voters", \
  38.                     "opinion":{"a":60,"b": 20}, \
  39.                     "understanding":{"a":20,"b":20}, \
  40.                     "eliquence":{"a":70,"b":30}, \
  41.                     "attain":30 \
  42.                   }]*66000
  43.  
  44.  
  45. df=10
  46.  
  47. def randomdist(offset):
  48.     while 1:
  49.         r=np.random.noncentral_chisquare(df, offset)
  50.         if (r<100 and r>=0) : break
  51.     return r
  52.  
  53. class issue:
  54.     def __init__(self, spec, _i):
  55.         self.name = spec["name"]
  56.         self.i=_i
  57.         self.complexity=randomdist(spec["complexity"])
  58.         self.solutions=int(randomdist(spec["solutions"]+1))
  59.  
  60. issues=[]
  61. i=0
  62. for spec in issues_spec:
  63.     issues.append(issue(spec, i))
  64.     i+=1
  65.  
  66. class view:
  67.     def __init__(self, spec, issue):
  68.         self.issue=issue
  69.         self.opinion=randomdist(spec["opinion"][issue.name])
  70.         self.understanding=randomdist(spec["understanding"][issue.name])
  71.         self.eliquence=randomdist(spec["eliquence"][issue.name])
  72.        
  73. class agent:
  74.     def __init__(self, spec,_i):
  75.         self.name = spec["name"]
  76.         self.i=_i
  77.         self.views=[];
  78.         for issue in issues:
  79.             self.views.append(view(spec, issue))
  80.         self.attain=randomdist(spec["attain"])
  81.         self.trust={}; # agents that we end up trusting or distructing
  82.  
  83.     def interact(self, speaker, issue):
  84.         #        Agents opinion is changed by speaker, towards the speakers point of view if the elequence of the speaker is high, if the agent has trust in the speaker,  if the agents understanding is low, at the speed that the agent attains.
  85.         mv=self.views[issue.i]
  86.         sv=speaker.views[issue.i]
  87.         # If the speaker is ore eliquent than my current understanding, then I learn, otherwise, I ignore
  88.         changeby=0
  89.         if speaker in self.trust:
  90.             trust=self.trust[speaker]
  91.         else:
  92.             trust=0
  93.         if (sv.eliquence > mv.understanding):
  94.             changeby = randomdist((((sv.eliquence + trust - mv.understanding)*self.attain)/issue.complexity)/10)
  95.             mv.understanding = mv.understanding + abs(changeby)
  96.             if (changeby>0) :
  97.                 mv.opinion = ((mv.opinion) + (sv.opinion * changeby))/ (changeby+1)
  98.         if speaker in self.trust:
  99.             self.trust[speaker]+=changeby/10
  100.         else:
  101.             self.trust[speaker]=changeby/10
  102.  
  103. def addTrust(agents, trusted, value):
  104.     for trustagent in trusted:
  105.         for agent in agents:
  106.             if trustagent in agent.trust:
  107.                 agent.trust[trustagent]+=value
  108.             else:
  109.                 agent.trust[trustagent]=value
  110.  
  111. def print_votes(agents, issues):
  112.     for p in agents:
  113.         votes=[[]]
  114.         for issue in issues:
  115.             votes.insert(issue.i,[0]*issue.solutions)
  116.  
  117.         happyness=[]
  118.         htotal=0
  119.         for issue in issues:
  120.             opinions=[]
  121.             for a in agents[p]:
  122.                 opinions.append(a.views[issue.i].opinion)
  123.                 solution=(int(a.views[issue.i].opinion / (100/issue.solutions)))
  124.                 if (solution>=0 and solution<issue.solutions):
  125.                     votes[issue.i][(int(a.views[issue.i].opinion / (100/issue.solutions)))]+=1
  126.                 else:
  127.                     print ("Chosen solution outside range for issue :"+issue.name+str(issue.i)+" : "+str(a.views[issue.i].opinion))
  128.             happyness.insert(0,statistics.variance(opinions))
  129.  
  130.         av_happyness=statistics.mean(happyness)
  131.         for issue in issues:
  132.             print (p+" Votes for issue "+issue.name+str(issue.i)+" : ", end='')
  133.             win=0
  134.             i=0
  135.             for v in votes[issue.i]:
  136.                 if v>votes[issue.i][win] :
  137.                     win=i
  138.                 print (" "+str(v), end='')
  139.                 i+=1
  140.             print (" \t winner: "+str(win)+" \t  happyness: "+str(int(happyness.pop())))
  141.         print ("Average Happyness : "+str(av_happyness))
  142.  
  143.        
  144.        
  145. agents={}
  146. for spec in population_spec:
  147.     agents[spec["name"]]=[];
  148.  
  149. i=0    
  150. for spec in population_spec:
  151.     agents[spec["name"]].append(agent(spec,i))
  152.     i+=1;
  153.  
  154.  
  155.  
  156.  
  157.  
  158. print_votes(agents,issues)
  159.    
  160. #for i in range(10):
  161. #    print (".",end='', flush=True);
  162. #    for mp in random.sample(agents["mp"],20):
  163. #        for issue in issues:
  164. #            for voter in random.sample(agents["voters"],1000):
  165. #                voter.interact(mp, issue)
  166. #print("");
  167.  
  168.  
  169. print("sotition")
  170.  
  171. sortition=random.sample(agents["voters"],100)
  172.  
  173. addTrust(agents["voters"], sortition, 50)
  174.  
  175. for i in range(10):
  176.     print (".",end='', flush=True);
  177.     for voter in sortition:
  178.         for mp in random.sample(agents["mp"],10):
  179.             for issue in issues:
  180.                 voter.interact(mp, issue)
  181. print("");
  182.  
  183.  
  184. for i in range(5):
  185.     print (".",end='', flush=True);
  186.     for sortd in sortition:
  187.         for voter in random.sample(agents["voters"], 1000):
  188.             for issue in issues:
  189.                 voter.interact(sortd, issue)
  190. print("");
  191.  
  192.  
  193. print_votes(agents,issues)
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement