Parallel Python Community Forums

Python Forums => Parallel Python Forum => Topic started by: tpike on September 27, 2017, 09:24:53 AM



Title: Parallel Python 1.6.4.4, Python 3.6 --- won't call function
Post by: tpike on September 27, 2017, 09:24:53 AM
I am doing a zero intelligence trader model and have tried several variations (with classes, without classes, calling pp.Server in different locations) and the primary trade function will not call. I keep getting a key error, where it seems not to recognize the function, and a name error "BPF".


The code is only 48 lines long. So below is the code in its entirety plus error, any help would be appreciated!

import random, time
import pp

class Agent:
    #create an object of each agent with one attribute price
    def __init__(self, min_price, max_price):
        self.price = random.randint(min_price, max_price)
       
class Market:
    #create market envirnment which is a list of all the agents and empyt list to track trades.
    def __init__(self, numbuyers, numsellers, min_price, max_price, num_server):
        self.buyers = [Agent(min_price, max_price) for i in range(numbuyers)]
        self.sellers =[Agent(min_price, max_price) for i in range(numsellers)]
        self.trades = []
       
       
    def trade(self, sellers, buyers):
        #function iterates through list of buyers and sellers and sees if a trade can occur
        print ("Traders are trading")
        for seller in sellers:
            idx = sellers.index(seller)
            buyer = buyers[idx].price
            if seller.price <= buyer:
                self.trades.append(random.uniform(buyer, seller.price))
        result = sum(self.trades)/len(self.trades)
        return result
   
    #execute trades for all agents
    def execution(self, num_procs):
        #sets up servers shuffles, buyer, agents list
        server = pp.Server(num_procs)
        random.shuffle(self.buyers)
        random.shuffle(self.sellers)
        #execute trades using parrallel processing
        job = server.submit(self.trade,(self.buyers, self.sellers), (), ("random",))
        print ("Average Trade Price", job())
        server.print_stats()
               
       

if __name__ == '__main__':
    #job_server = pp.Server(4)
    market = Market(100000,100000, 1, 20, 4)
    start_init = time.time()
    start_trade = time.time()
    result = market.execution(4)
    end_time = time.time()
    print ("Sequential Time:", end_time - start_init)
    print ("Total Time:", end_time - start_trade)


An error has occured during the function import
Traceback (most recent call last):
  File "C:\Users\ymamo\Anaconda3\lib\site-packages\pp-1.6.4.4-py3.6.egg\ppworker.py", line 96, in run
    six.exec_(__fobj)
  File "<string>", line 71, in <module>
  File "<string>", line 222, in Random
NameError: name 'BPF' is not defined
A fatal error has occured during the function execution
Traceback (most recent call last):
  File "C:\Users\ymamo\Anaconda3\lib\site-packages\pp-1.6.4.4-py3.6.egg\ppworker.py", line 105, in run
    __f = locals()[ppc.str_(__fname)]
KeyError: 'trade'


Title: Re: Parallel Python 1.6.4.4, Python 3.6 --- won't call function
Post by: tpike on September 28, 2017, 02:33:04 AM
I got it narrowed down to an issue with the import. It actually gives the right answer and works but still throws an error. The trick seemed to be importing the Agent class (ZI agent),

Any insights would be appreciated, thank you

Updated Code:

ZIAgent.py

import random

class Agent:
    #create an object of each agent with one attribute price
    def __init__(self, min_price, max_price):
        self.price = random.randint(min_price, max_price)

ZI_Agent in Parallel

import ZIAgent
import random
import time
import pp


class Market:
    #create market environment which is a list of all the agents and empty list to track trades.
    def __init__(self, numbuyers, numsellers, min_price, max_price):
        self.buyers = [ZIAgent.Agent(min_price, max_price) for i in range(numbuyers)]
        self.sellers =[ZIAgent.Agent(min_price, max_price) for i in range(numsellers)]
       
       
    def deal(self, sellprice, buyprice):
        return random.uniform(sellprice, buyprice)
   
   
    def traders(self, sellers, buyers):
        #function iterates through list of buyers and sellers and sees if a trade can occur
        print ("Traders are trading")
        trades = []
        for seller in sellers:
            idx = sellers.index(seller)
            buyprice = buyers[idx].price
            sellprice = seller.price
            if sellprice <= buyprice:
                trades.append(self.deal(sellprice, buyprice))
        result1 = sum(trades)/len(trades)
        result2 = len(trades)
        return result1, result2
   
    #execute trades for all agents
    def execution(self, num_procs):
       
        job_server = pp.Server(num_procs)
        #sets up servers shuffles, buyer, agents list
        random.shuffle(self.buyers)
        random.shuffle(self.sellers)
        #execute trades using parrallel processing
        job = job_server.submit(self.traders,args = (self.sellers, self.buyers), depfuncs = (self.deal,), modules = ("ZIAgent", "random",))
        print ("Average Trade Price", job()[0])
        print ("Number of Trades", job()[1])
        job_server.print_stats()
               
       

if __name__ == '__main__':
    #job_server = pp.Server(4)
    market = Market(1000,1000, 1, 20)
    start_init = time.time()
    start_trade = time.time()
    result = market.execution(4)
    end_time = time.time()
    print ("Sequential Time:", end_time - start_init)
    print ("Total Time:", end_time - start_trade)

Output

An error has occured during the function import
Traceback (most recent call last):
  File "C:\Users\ymamo\Anaconda3\lib\site-packages\pp-1.6.4.4-py3.6.egg\ppworker.py", line 96, in run
    six.exec_(__fobj)
  File "<string>", line 71, in <module>
  File "<string>", line 222, in Random
NameError: name 'BPF' is not defined
Traders are trading
 Average Trade Price 10.17098977834595
Number of Trades 520
Job execution statistics:
 job count | % of all jobs | job time sum | time per job | job server
         1 |        100.00 |       0.0572 |     0.057154 | local
Time elapsed since server creation 0.1389172077178955
0 active tasks, 4 cores

Sequential Time: 5.481494665145874
Total Time: 5.481494665145874


Title: Parallel Python 1.6.4.4, Python 3.6
Post by: xi on October 16, 2017, 09:12:01 PM
Hi
I am using python 3 and there is no problem to install PP. However, when i run here comes the error:
please let me know how to fix it.
thanks


An error has occured during the function import
Traceback (most recent call last):
  File "C:\ProgramData\Anaconda3\lib\site-packages\pp-1.6.4.4-py3.6.egg\ppworker
.py", line 96, in run
    six.exec_(__fobj)
  File "<string>", line 1, in <module>
  File "<string>", line 10, in SelectionMixin
NameError: name 'builtins' is not defined
An error has occured during the function import
Traceback (most recent call last):
  File "C:\ProgramData\Anaconda3\lib\site-packages\pp-1.6.4.4-py3.6.egg\ppworker
.py", line 96, in run
    six.exec_(__fobj)
  File "<string>", line 1, in <module>
NameError: name 'SelectionMixin' is not defined
An error has occured during the function import
Traceback (most recent call last):
  File "C:\ProgramData\Anaconda3\lib\site-packages\pp-1.6.4.4-py3.6.egg\ppworker
.py", line 96, in run
    six.exec_(__fobj)
  File "<string>", line 1, in <module>
NameError: name 'NDFrame' is not defined
 Job execution statistics:
 job count | % of all jobs | job time sum | time per job | job server
        67 |        100.00 |     203686.8125 |  3040.101679 | local
Time elapsed since server creation 3360.755722761154
0 active tasks, 67 cores