Quantify programming techniques - mathematical optimal solution

from abc Import ABCMeta, AbstractMethod
 Import Six
 Import numpy AS NP
 Import PANDAS AS pd
 Import matplotlib.pyplot AS plt 

# Everybody average life expectancy is 75 years, about 75 * 365 = 27,375 days 
K_INIT_LIVING_DAYS = 27375 class the Person (Object):
     "" " 
        human ", "" DEF __init__ (Self):
         # initialize the average person can live life 
        self.living = K_INIT_LIVING_DAYS
         # initialize happiness index 
        self.happiness = 0
         # initialize wealth value 
        self.wealth = 0
        


    

     # Initialize fame rights 
        self.fame = 0
         # alive in the first few days 
        self.living_day = 0 

    DEF live_one_day (Self, seek):
         "" " 
        can only be a day seek, the seek determines what you seek today to give What 
        type of belonging BaseSeekDay will seek written 
        : param seek: 
        : return: 
        "" " 
        # call do_seek_day each unique BaseSeekDay class will be implemented to achieve today's harvest 
        consume_living, Happiness, Wealth, Fame = seek.do_seek_day ( )
         # every day life minus consumption, some will seek to increase the previous life 
        self.living - = consume_living
         # seek to get the happiness index accumulated 
        self.happiness + = happiness
        # The Seek resulting accumulation of wealth 
        self.wealth + = Wealth
         # the Seek get fame accumulation of power 
        self.fame + = Fame
         # alive after this day 
        self.living_day + = 1 class BaseSeekDay (six.with_metaclass (ABCMeta, Object)):
     DEF __init__ (Self):
         # each daily consumption of constant pursuit of life 
        self.living_consume = 0 # each day exponential constant pursuit of happiness 
        self.happiness_base = 0 # each day to pursue wealth accumulation constant 
        self.wealth_base = 0
         # each pursuit accumulation constant daily claimed fame 
        self.fame_base = 0 #


 

        

        

        Each day of life the pursuit of consumption variable factor sequence 
        self.living_factor = [0] 

        # every day to pursue happiness index variable factor sequence 
        self.happiness_factor = [0] 

        # every day to pursue wealth accumulation variables sequence 
        self = .wealth_factor [0]
         # per day pursuit variables sequence of claim fame 
        self.fame_factor = [0] 

        # pursue a number of days this life 
        self.do_seek_day_cnt = 0
         # subclasses constants and variables sequence set 
        self ._init_self () 

    @abstractmethod 
    DEF _init_self (Self, * args, ** kwargs):
         # subclasses must implement, set the constant consumption of his life, the happiness index constants constant setting 
        Pass 

    @abstractmethod 
    DEF_gen_living_days (Self, * args, ** kwargs):
         # subclasses must implement, set up your own variables sequence 
        Pass 

    DEF do_seek_day (Self):
         "" " 
        pursue every day specifically the Seek 
        : return: 
        " "" 
        # life consumption = living_consume: constant consumption * happiness_factor: variable sequence 
        IF self.do_seek_day_cnt> = len (self.living_factor):
             # exceeded len (self.living_factor), it takes the last living_factor [-1] 
            consume_living = \ 
                self.living_factor [ - 1] * self.living_consume
         the else :
             # each class custom pursue this life of constant consumption, as well as living_factor, such as 
            #HealthSeekDay pursuit of health value, that is, from the negative sequence living_factor -> value 
            # Each subclass living_factor vary the speed and length of the sequence features of its own, resulting in each 
            # pursuit of life consumed with the pursuit of the number varied from 
            consume_living = self.living_factor [self.do_seek_day_cnt] \
                              * self.living_consume
         # happiness index = happiness_base: Well constant * happiness_factor: variable sequence 
        IF self.do_seek_day_cnt> = len (self.happiness_factor):
             # exceeded len (self.happiness_factor), Finally, it takes a 
            # Since the value happiness_factor: n-> 0 so happiness_factor [-1] = 0 
            # i.e., as the number of the pursuit of a thing does not become excessive after happiness 
            happiness = self.happiness_factor [
                             -1] *self.happiness_base
         the else :
             # each class to customize the pursuit of happiness index constant, and happiness_factor 
            # define happiness_factor subclass generally from high -> low variability 
            Happiness = self.happiness_factor [ 
                            self.do_seek_day_cnt] * self.happiness_base
         # wealth accumulation = wealth_base: accumulation constant * wealth_factor: variable sequence 
        IF self.do_seek_day_cnt> = len (self.wealth_factor):
             # exceeded len (self.wealth_factor), to take a final 
            Wealth self.wealth_factor = [-1] * Self. wealth_base
         the else :
             # each class to customize the pursuit of wealth index constant, and wealth_factor
            = Wealth self.wealth_factor [ 
                         self.do_seek_day_cnt] * self.wealth_base
         # claim Accumulation = fame_base: Accumulation Constant * fame_factor: variable sequence 
        IF self.do_seek_day_cnt> = len (self.fame_factor):
             # exceeded len (self.fame_factor), Finally, it takes a 
            fame self.fame_factor = [-1] * self.fame_base
         the else :
             # each custom class index claimed fame this constant pursuit, and fame_factor 
            fame = self.fame_factor [ 
                       self.do_seek_day_cnt] * self.fame_base
         # the pursuit of the number of days in this life + 1
        + = 1 self.do_seek_day_cnt # returns the pursuit of this day consumption of life, to get happiness, wealth, fame right to return consume_living, Happiness, Wealth, Fame DEF regular_mm (Group):
     # minimum - maximum standardization return (Group - Group. min ()) / (group.max () - group.min ()) class HealthSeekDay (BaseSeekDay):
     "" " 
        HealthSeekDay pursue day health and longevity: 
        . the image of: fitness, travel, entertainment, interested in doing something 
        abstract: the pursuit of health and longevity. "" " DEF _init_self (Self):
         # daily life of constant consumption = 1, which represents one day 
        self.living_consume = 1 # happy day exponential constant = 1 
        self.happiness_base = 1 # set variables sequence
        
        
        
        

    



    

    
        
        
        self._gen_living_days () 

    DEF _gen_living_days (Self):
         # generate a sequence of only 12,000, because the following sequence of values by the happiness_factor 1-> 0 
        # pursuit 12,000 times greater than it will simply consume life, does not increase the happiness index 
        # ie as more and more often to do one thing, happiness getting lower and lower, until it is completely not experience happiness 
        Days = np.arange (1, 12000 )
         # basic function selection sqrt, affect the speed of sequence variation 
        living_days = NP. sqrt (Days) 

        "" " 
            life consumption value by a variable factor sequence -1-> 1, that is, when the beginning of the pursuit of a life 
            consumed by negative growth and prolong life, with the number growing to the pursuit of life consumption turned positive 
            number because even if a person exercise every day, eat nutritional supplements every day, and still have natural death of that 
            one day 
        . "" " 
        # purpose * 2-1: regular_mm between 0-1, HealthSeekDay to results between -1,1
        = regular_mm self.living_factor (living_days) * 2 --1 # results between 1-0 [:: --1]: The 0-> 1 converted to l-> 0 
        self.happiness_factor = regular_mm (Days) [:: - 1 ] # initialized me the story of your life: HealthSeekDay 
me = the Person ()
 # initialize the pursuit of health and longevity happy 
seek_health = HealthSeekDay ()
 the while me.living> 0:
     # long as he lives, the pursuit of health and longevity happy     me.live_one_day (seek_health ) Print ( ' only the pursuit of happiness to live long and healthy life {} years, the happiness index {} {} accumulation of wealth, fame powers {} ' .format 
      (round (me.living_day / 365, 2), round (me.happiness, 2 ), 
       me.wealth, me.fame))
        
        
        




       
       
plt.plot(seek_health.living_factor * seek_health.living_consume)
plt.plot(seek_health.happiness_factor * seek_health.happiness_base)
plt.legend(['living_factor', 'happiness_factor'], loc='best')

 

Guess you like

Origin www.cnblogs.com/fangbei/p/11521580.html