yannickoswald / covpol

COVPOL: An agent-based model of the international COVID-19 policy response
3 stars 1 forks source link

Code at the beginning of class source files #10

Closed nickmalleson closed 1 year ago

nickmalleson commented 1 year ago

In https://github.com/eeyouol/Covid_policy_response_abm/blob/master/code/agent_class.py there is a load of code at the beginning of the script that reads data and sets some variables. @ksuchak1990 may know more about this, but I think it may be bad practice because it means whenever this script is accessed by pythom, then that code will be run, even if you don't want it to. E.g. just putting import agent_class will cause all the code to be run but you might not actually want that (maybe you want to use another function in agent_class and don't care about loading data first). Or if you start a parallel process it may re-read that file and run the code in each of the child processes (we came across something similar before).

Instead, you could put that code into the CountryAgent class, so that it is only run the first time an object of tyle CountryAgent is created. (I think). Or you could put it into a specific function in CountryAgent and call it directly. Lets discuss

yannickoswald commented 1 year ago

this was a pretty odd design choice, true, but i am pretty sure that i deleted some of the unnecessary things in the agent class already. because as far as i can tell none of the loaded data up there in the agent class was actually necessary for that module. And I remember deleting those things. I think somethin went wrong with the version control here at some point.

however, i now did change things again and made the following necessary function a method


    ### this a function from here
    ### https://www.geeksforgeeks.org/program-distance-two-points-earth/
    ### for calculating the distance between points on earth

    def geo_distance(self, lat1, lat2, lon1, lon2):
            # The math module contains a function named
            # radians which converts from degrees to radians.
            lon1 = radians(lon1)
            lon2 = radians(lon2)
            lat1 = radians(lat1)
            lat2 = radians(lat2)
            # Haversine formula
            dlon = lon2 - lon1
            dlat = lat2 - lat1
            a = sin(dlat / 2)**2 + cos(lat1) * cos(lat2) * sin(dlon / 2)**2
            c = 2 * asin(sqrt(a))
            # Radius of earth in kilometers. Use 3956 for miles
            r = 6371
            # calculate the result
            return c * r

And also defined the necessary constants just as agent properties


       #### of variables across agent and important 
       #### for the computation of the distance metric
       self.range_income = 117075.04
       self.range_politicalregime = 8.739999999999998
        ## max distance between two points on earth =
        ## earth circumference divided by two
       self.max_distance_on_earth = 40075.017/2 ```