I have created four classes: experiment, experiment_type1, experiment_type2 and experiment_type3
experiment
is an abstract class, it cannot be instantiated. It has 2 methods, __init__(self)
and run(self)
where run(self)
is abstract.
experiment_type1
and experiment_type2
are derived from experiment. They inherit the __init__(self)
from experiment
(so they share the same constructor) but they implement run(self)
differently from each other.
My problem is with experiment_type3
class. It also only has the run(self)
method, implemented differently from experiment_type1
and experiment_type2
, but its constructor takes an additional argument. Its constructor is of the form __init__(self, parameter)
Ideally I would like experiment_type3
to be derived from experiment
. But there is a constructor mismatch. What is the best way to handle this? Programming in python in this case.
Edit: This is the code for experiment and experiment_type3. As you can see it depends on self.epsilon which does not exist.
import numpy as np from abc import ABC, abstractmethod from Bandit import Bandit
class experiment(ABC):
def __init__(self, num_iter, bandit_list): #epsilon is the chance to explore, num_iter is num of iterations, bandit_list is the list of the bandits
self.num_iter = num_iter
self.bandit_list = bandit_list
self.best_bandit = np.random.choice(len(bandit_list))
@abstractmethod
def run(self):
raise NotImplementedError('derived class must implement run() method!')
class eg_experiment(experiment):
def run(self):
for iteration in range(self.num_iter):
bandit = np.random.choice(len(self.bandit_list))
if(np.random.random() > self.epsilon):
bandit = self.best_bandit
self.bandit_list[self.best_bandit].pull()
self.best_bandit = np.argmax([bandit.current_mean for bandit in self.bandit_list])