Skip to content

Commit

Permalink
phenom
Browse files Browse the repository at this point in the history
  • Loading branch information
DavisWeaver committed Dec 7, 2023
1 parent 5f8a4bb commit b5c20e0
Show file tree
Hide file tree
Showing 12 changed files with 26 additions and 11 deletions.
Binary file modified evodm/__pycache__/dpsolve.cpython-39.pyc
Binary file not shown.
Binary file modified evodm/__pycache__/evol_game.cpython-39.pyc
Binary file not shown.
Binary file modified evodm/__pycache__/exp.cpython-39.pyc
Binary file not shown.
Binary file modified evodm/__pycache__/landscapes.cpython-39.pyc
Binary file not shown.
Binary file modified evodm/__pycache__/learner.cpython-39.pyc
Binary file not shown.
9 changes: 6 additions & 3 deletions evodm/dpsolve.py
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,7 @@ class dp_env:
"""
def __init__(self, N, sigma,
correl = np.linspace(-1.0,1.0,51),
num_drugs = 4, drugs = None, noinit = False):
num_drugs = 4, drugs = None, noinit = False, phenom = 0):
#define the drugs if there aren't any
if drugs is None:
## Generate landscapes - use whatever parameters were set in main()
Expand All @@ -35,8 +35,11 @@ def __init__(self, N, sigma,
#define the landscapes
landscapes = [Landscape(ls = i, N=N, sigma = sigma, dense = False) for i in drugs]
#get the transition matrix for each landscape
self.tm = [i.get_TM() for i in landscapes]

#if phenom > 0:
# self.tm = [i.get_TM_phenom(phenom) for i in landscapes]
#else:
# self.tm = [i.get_TM() for i in landscapes]
self.tm = [i.get_TM_phenom(phenom) for i in landscapes]
#get number of states
self.nS = pow(2,N)
self.nA = num_drugs
Expand Down
9 changes: 6 additions & 3 deletions evodm/evol_game.py
Original file line number Diff line number Diff line change
Expand Up @@ -71,10 +71,10 @@ def __init__(self, drugs = None, N = 5, sigma = 0.5, correl = np.linspace(-1.0,
self.DENSE= dense
self.CS = cs
self.num_drugs = num_drugs
self.PHENOM = phenom

#Defining these in case self.reset is called
self.correl = correl
self.phenom = phenom

#define victory counters
self.player_wcount = 0
Expand Down Expand Up @@ -145,8 +145,11 @@ def define_landscapes(self, drugs, normalize_drugs):
self.drugs = normalize_landscapes(self.drugs)
self.landscapes = [Landscape(ls = i, N=self.N, sigma = self.sigma,
dense = self.DENSE) for i in self.drugs]

[i.get_TM() for i in self.landscapes] #pre-compute TM
#if self.PHENOM > 0:
# [i.get_TM_phenom(phenom = self.PHENOM) for i in self.landscapes]
#else:
# [i.get_TM() for i in self.landscapes] #pre-compute TM
[i.get_TM_phenom(phenom = self.PHENOM) for i in self.landscapes]

return

Expand Down
3 changes: 2 additions & 1 deletion evodm/exp.py
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,7 @@ def evol_deepmind(savepath = None, num_evols = 1, N = 5, episodes = 50,
starting_genotype = 0, train_freq = 100,
compute_implied_policy_bool = False,
dense = False, master_memory = True,
delay = 0):
delay = 0, phenom = 0):
"""
evol_deepmind is the main function that initializes and trains a learner to switch between n drugs
to try and minimize the fitness of a population evolving on a landscape.
Expand Down Expand Up @@ -115,6 +115,7 @@ def evol_deepmind(savepath = None, num_evols = 1, N = 5, episodes = 50,
hp.DENSE = dense
hp.MASTER_MEMORY = master_memory
hp.DELAY= int(delay)
hp.PHENOM = phenom

#gotta modulate epsilon decay based on the number of episodes defined
#0.005 = epsilon_decay^episodes
Expand Down
2 changes: 1 addition & 1 deletion evodm/landscapes.py
Original file line number Diff line number Diff line change
Expand Up @@ -149,7 +149,7 @@ def define_adjMut(self, mut, i):
return adjMut


def get_TM_phenom(self, phenom, store=False):
def get_TM_phenom(self, phenom, store=True):
"""
Returns the transition matrix for this landscape, with phenomenological stepping (see Tan and Gore 2012). If store=True, it will
be saved in a field of this object (TM) for later use. If a stored copy already
Expand Down
9 changes: 6 additions & 3 deletions evodm/learner.py
Original file line number Diff line number Diff line change
Expand Up @@ -91,6 +91,7 @@ def __init__(self):
self.NUM_DRUGS = 4
self.MIRA = True
self.TOTAL_RESISTANCE = False
self.PHENOM = 0
#wright-fisher controls
self.WF = False
self.POP_SIZE = 10000
Expand Down Expand Up @@ -154,7 +155,8 @@ def __init__(self, hp, drugs= None):
starting_genotype = self.hp.STARTING_GENOTYPE,
total_resistance= self.hp.TOTAL_RESISTANCE,
dense=self.hp.DENSE,
delay=self.hp.DELAY)
delay=self.hp.DELAY,
phenom=self.hp.PHENOM)

# main model # gets trained every step
self.model = self.create_model()
Expand Down Expand Up @@ -229,7 +231,7 @@ def train(self):

# Now we need to enumerate our batches
X,y = self.enumerate_batch(minibatch = minibatch, future_qs_list = future_qs_list,
current_qs_list= current_qs_list)
current_qs_list = current_qs_list)

self.model.fit(X, y, batch_size=self.hp.MINIBATCH_SIZE,
verbose=0, shuffle=False, callbacks=None)
Expand Down Expand Up @@ -381,7 +383,8 @@ def compute_optimal_policy(agent, discount_rate = 0.99, num_steps = 20):
'''

env = dp_env(N = agent.env.N, sigma = agent.env.sigma,
drugs = agent.env.drugs, num_drugs= len(agent.env.drugs))
drugs = agent.env.drugs, num_drugs= len(agent.env.drugs),
phenom = agent.env.PHENOM)

policy, V = backwards_induction(env = env, discount_rate= discount_rate, num_steps=num_steps)

Expand Down
Binary file modified evodm/test/__pycache__/test_exp.cpython-39-pytest-7.4.0.pyc
Binary file not shown.
5 changes: 5 additions & 0 deletions evodm/test/test_exp.py
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,11 @@ def test_mdp_mira_sweep():
bools_list = list(chain.from_iterable(bools_list))
assert any(bools_list)

def test_evol_deepmind_phenom():
#make sure the phenom landscapes don't crash everything
out = evol_deepmind(train_input = 'fitness', episodes=5, num_drugs=15, N=4,
mira=True, normalize_drugs=False, phenom=20)

def test_evol_deepmind_wf():
#just make sure it runs quietly at a minimum
out = evol_deepmind(wf = True, train_input='fitness', episodes=5,
Expand Down

0 comments on commit b5c20e0

Please sign in to comment.