class gamble: def __init__(self,name,consequence,probability): self.name = name self.consequence = consequence self.probability = probability def showName(self,NameAlt): NameAlt = NameAlt + [self.name] return NameAlt def showPayoff(self,Pay): Pay = Pay + [self.consequence] return Pay def showProb(self,Prob): Prob = Prob + [self.probability] return Prob Projecto1 = gamble('Projecto 1',(2,1.5,1.2),(0.1,0.4,0.5)) Projecto2 = gamble('Projecto 2',(2,1.5,1.2),(0.2,0.2,0.6)) Pay = [] Prob = [] NameAlt = [] ##NameAlt = Projecto1.showName(NameAlt) ##NameAlt = Projecto2.showName(NameAlt) ##print NameAlt ##Pay = Projecto1.showPayoff(Pay) ##Pay = Projecto2.showPayoff(Pay) ##print Pay ##Prob = Projecto1.showProb(Prob) ##Prob = Projecto2.showProb(Prob) ##print Prob Alternativas = [Projecto1, Projecto2] for i in range(0,len(Alternativas)): NameAlt = Alternativas[i].showName(NameAlt) Pay = Alternativas[i].showPayoff(Pay) Prob = Alternativas[i].showProb(Prob) print NameAlt print Pay print Prob class automaton: def __init__(self,alternatives,probabilities,expectations,chosen): self.alternatives = alternatives self.probabilities = probabilities self.expectations = expectations self.chosen = chosen def lookAt(self,Pay,Prob): for i in range(0,len(Pay)): self.alternatives = self.alternatives + [Pay[i]] for j in range(0,len(Prob)): self.probabilities = self.probabilities + [Prob[j]] def formExpect(self): for i in range(0,len(self.alternatives)): A = self.alternatives[i] Prob = self.probabilities[i] Weigh = [] for j in range(0,len(A)): Weigh = Weigh + [A[j] * Prob[j]] self.expectations = self.expectations + [sum(Weigh)] def choose(self,NameAlt): for i in range(0,len(NameAlt)): print "Alternative:", NameAlt[i], "| Expected Payoff:", self.expectations[i] M = max(self.expectations) for j in range(0,len(self.expectations)): if self.expectations[j] == M: self.choice = NameAlt[j] self.chosen = self.chosen + [NameAlt[j]] print("\nThe chosen alternatives are:") for k in range(0,len(self.chosen)): print self.chosen[k] print "\nWith associated expected payoff: "+str(M) Empresa = automaton([],[],[],[]) Empresa.lookAt(Pay,Prob) Empresa.formExpect() Empresa.choose(NameAlt)
Run
Reset
Share
Import
Link
Embed
Language▼
English
中文
Python Fiddle
Python Cloud IDE
Follow @python_fiddle
Browser Version Not Supported
Due to Python Fiddle's reliance on advanced JavaScript techniques, older browsers might have problems running it correctly. Please download the latest version of your favourite browser.
Chrome 10+
Firefox 4+
Safari 5+
IE 10+
Let me try anyway!
url:
Go
Python Snippet
Stackoverflow Question