浏览代码

[research/lotterysim] fix update vesting, and negative apr

ertosns 3 年之前
父节点
当前提交
6e6bf0ba33

+ 4 - 4
script/research/lotterysim/core/constants.py

@@ -1,7 +1,7 @@
 from decimal import Decimal as Num
 
 # number approximation terms
-N_TERM = 2
+N_TERM = 5
 # analogue controller enum
 CONTROLLER_TYPE_ANALOGUE = -1
 # discrete controller enum
@@ -9,7 +9,7 @@ CONTROLLER_TYPE_DISCRETE = 0
 # takahashi controller enum
 CONTROLLER_TYPE_TAKAHASHI = 1
 # initial distribution of tokens (random value for sake of experimentation)
-ERC20DRK = 0
+ERC20DRK = 10000
 # initial distribution
 PREMINT = ERC20DRK
 # group base/order
@@ -25,7 +25,7 @@ REWARD_MAX = 1000
 # slot length in seconds
 SLOT = 90
 # epoch length in slots
-EPOCH_LENGTH = 100
+EPOCH_LENGTH = 10
 # one month in slots
 ONE_MONTH = 60*60*24*30/SLOT
 # one year in slots
@@ -53,7 +53,7 @@ EPSILON = 1
 # window of accuracy calculation
 ACC_WINDOW = int(EPOCH_LENGTH)*10
 # headstart airdrop period
-HEADSTART_AIRDROP = ONE_MONTH
+HEADSTART_AIRDROP = 0
 # threshold of randomly slashing stakeholder
 SLASHING_RATIO = 0.000005
 # number of nodes

+ 11 - 5
script/research/lotterysim/core/darkie.py

@@ -40,8 +40,10 @@ class Darkie():
     @returns: apr
     """
     def apr_scaled_to_runningtime(self):
-        initial_stake = self.vesting_wrapped_initial_stake()
-        #assert self.stake >= initial_stake, 'stake: {}, initial_stake: {}, slot: {}, current: {}, previous: {} vesting'.format(self.stake, initial_stake, self.slot, self.current_vesting(), self.prev_vesting())
+        #initial_stake = self.vesting_wrapped_initial_stake()
+        initial_stake = self.initial_stake[-1]
+        # note the following will not hold if fee is enabled.
+        #assert self.stake >= initial_stake or math.fabs(initial_stake - self.stake) < EPSILON , 'stake: {}, initial_stake: {}, slot: {}, current: {}, previous: {} vesting'.format(self.stake, initial_stake, self.slot, self.current_vesting(), self.prev_vesting())
         if self.slot < HEADSTART_AIRDROP:
             # during this phase, it's only called at end of epoch
             apr_period = self.slot%EPOCH_LENGTH
@@ -51,8 +53,8 @@ class Darkie():
             apr_period = self.slot-HEADSTART_AIRDROP
         apr_scaled = ((self.stake - initial_stake) / initial_stake) / apr_period if initial_stake>0  and apr_period>0 else 0
         apr = apr_scaled * ONE_YEAR if initial_stake > 0 and apr_period>0 and self.slot>0 else 0
-        #if apr>0 and self.stake-initial_stake>0:
-            #print("apr: {}, stake: {}, initial_stake: {}".format(apr, self.stake, initial_stake))
+        if self.slot < HEADSTART_AIRDROP:
+            assert apr>=0, 'apr: {}, apr_scaled: {}, initial_stake: {}, stake: {}, apr_period: {}'.format(apr, apr_scaled, initial_stake, self.stake, apr_period)
         return apr
 
 
@@ -60,6 +62,7 @@ class Darkie():
     add vesting to initial stake
     @returns: vesting plus initial stake
     """
+    '''
     def vesting_wrapped_initial_stake(self):
         #returns  vesting stake plus initial stake gained from zero coin headstart during aridrop period
         vesting = self.current_vesting()
@@ -70,7 +73,8 @@ class Darkie():
         else:
             initial_stake = self.initial_stake[int(HEADSTART_AIRDROP/EPOCH_LENGTH)-1]
         return vesting + initial_stake
-
+        #return initial_stake
+    '''
     """
     update stake with vesting return every scheduled vesting period
     """
@@ -158,12 +162,14 @@ class Darkie():
     """
     def update_stake(self, reward):
         if self.won_hist[-1]:
+            assert reward>=0
             self.stake += reward
 
     """
     update stake after fork finalization
     """
     def resync_stake(self, reward):
+        assert reward>=0
         self.stake += reward
 
 

+ 1 - 44
script/research/lotterysim/core/strategy.py

@@ -125,22 +125,6 @@ class ZeroTip(Tip):
     def get_tip(self, last_reward, apr, size, last_tip):
         return 0
 
-class TenthOfReward(Tip):
-    def __init__(self):
-        super().__init__()
-        self.type = '10th'
-
-    def get_tip(self, last_reward, apr, size, last_tip):
-        return last_reward/10
-
-class HundredthOfReward(Tip):
-    def __init__(self):
-        super().__init__()
-        self.type = '100th'
-
-    def get_tip(self, last_reward, apr, size, last_tip):
-        return last_reward/100
-
 class MilthOfReward(Tip):
     def __init__(self):
         super().__init__()
@@ -159,33 +143,6 @@ class RewardApr(Tip):
         apr_relu = min(apr_relu, 1)
         return last_reward*apr_relu
 
-class TenthRewardApr(Tip):
-    def __init__(self):
-        super().__init__()
-        self.type = 'reward_apr'
-
-    def get_tip(self, last_reward, apr, size, last_tip):
-        apr_relu = max(apr, 0)
-        apr_relu = min(apr_relu, 1)
-        return last_reward*apr_relu/10
-
-
-class TenthCCApr(Tip):
-    def __init__(self):
-        super().__init__()
-        self.type = "cc_apr_10"
-
-    def get_tip(self, last_reward, apr, size, last_tip):
-        return size/MAX_BLOCK_SIZE/10
-
-class HundredthCCApr(Tip):
-    def __init__(self):
-        super().__init__()
-        self.type = "cc_apr_100"
-
-    def get_tip(self, last_reward, apr, size, last_tip):
-        return size/MAX_BLOCK_SIZE/100
-
 class MilthCCApr(Tip):
     def __init__(self):
         super().__init__()
@@ -212,4 +169,4 @@ class Generous(Tip):
 
 
 def random_tip_strategy():
-    return random.choice([ZeroTip(), RewardApr(), TenthReward(), HundredthOfReward(), TenthRewardApr(), MilthOfReward(), TenthCCApr(), HundredthCCApr(), MilthCCApr(), Conservative(), Generous()])
+    return random.choice([ZeroTip(), RewardApr(),   MilthOfReward(),  MilthCCApr(), Conservative(), Generous()])

+ 3 - 3
script/research/lotterysim/discrete_instance.py

@@ -10,14 +10,14 @@ from draw import draw
 os.system("rm log/*_feedback.hist; rm log/*_output.hist")
 
 RUNNING_TIME = int(input("running time:"))
-NODES=100
+NODES=1000
 
 if __name__ == "__main__":
     egalitarian = ERC20DRK/NODES
     darkies = []
 
     for id in range(int(NODES)):
-        darkie = Darkie(random.gauss(egalitarian, egalitarian*0.1), strategy=random_strategy())
+        darkie = Darkie(random.gauss(egalitarian, egalitarian*0.1), strategy=random_strategy(), idx=id)
         darkies += [darkie]
 
     #TODO try rpid with 0mint
@@ -32,7 +32,7 @@ if __name__ == "__main__":
     dt = DarkfiTable(airdrop, RUNNING_TIME, CONTROLLER_TYPE_DISCRETE, kp=-0.010399999999938556, ki=-0.0365999996461878, kd=0,  r_kp=-0.63, r_ki=3.35, r_kd=0)
     for darkie in darkies:
         dt.add_darkie(darkie)
-    acc, avg_apy, avg_reward, stake_ratio, avg_apr = dt.background(rand_running_time=False)
+    acc, cc_diff_avg, avg_apy, avg_reward, stake_ratio, avg_apr = dt.background(rand_running_time=False)
     sum_zero_stake = sum([darkie.stake for darkie in darkies[NODES:]])
     print('acc: {}, avg(apr): {}, avg(reward): {}, stake_ratio: {}'.format(acc, avg_apr, avg_reward, stake_ratio))
     print('total stake of 0mint: {}, ratio: {}'.format(sum_zero_stake, sum_zero_stake/ERC20DRK))

+ 1 - 1
script/research/lotterysim/plot_darkies.py

@@ -10,7 +10,7 @@ for darkie in glob.glob('log/darkie[0-9]*.log'):
         buf = f.read()
         lines = buf.split('\n')
         apr = float(lines[2].split(':')[1].strip())
-        aprs = [float(item) for item in lines[3].split(':')[1].split(',')]
+        aprs = [float(item) if item != ' ' else 0 for item in lines[3].split(':')[1].split(',')]
         initial_stake = [float(item) for item in lines[0].split(':')[1].split(',')]
         idx +=1
         darkies += [(initial_stake, apr, aprs, idx)]