Skip to content

Commit

Permalink
implemented sgd linear regr
Browse files Browse the repository at this point in the history
  • Loading branch information
Nikhil Pimpalkhare authored and Nikhil Pimpalkhare committed Oct 15, 2020
1 parent 3ed24c0 commit a5686cf
Show file tree
Hide file tree
Showing 3 changed files with 33 additions and 12 deletions.
2 changes: 2 additions & 0 deletions medleysolver/classifiers.py
Original file line number Diff line number Diff line change
Expand Up @@ -102,6 +102,8 @@ def __init__(self):
def get_ordering(self, point, count):
point = np.array(point).reshape(1, -1)
if self.fitted:
scores = self.clf.predict_proba(point)
order = sorted(list(range(len(SOLVERS))), key=lambda x: -1 * scores[x])
choice = self.clf.predict(point)
order = [list(SOLVERS.keys())[int(choice)]]
else:
Expand Down
8 changes: 4 additions & 4 deletions medleysolver/runner.py
Original file line number Diff line number Diff line change
Expand Up @@ -21,21 +21,21 @@ def execute(problems, output, classifier, time_manager, timeout, feature_setting
times = classifier.get_nearby_times(point, c)
end = time.time()

solver, elapsed, result, rewards, time_spent = apply_ordering(prob, order, timeout - (end - start), time_manager, extra_time_to_first, times, reward)
solver, elapsed, result, rewards, time_spent = apply_ordering(prob, order, timeout - (end - start), time_manager, extra_time_to_first, times, reward, point)
solved_prob = Solved_Problem(prob, point, solver, elapsed + (end - start), result, order, time_spent)

classifier.update(solved_prob, rewards)

writer.writerow(solved_prob)


def apply_ordering(problem, order, timeout, time_manager, extra_time_to_first, times, reward):
def apply_ordering(problem, order, timeout, time_manager, extra_time_to_first, times, reward, point):
elapsed = 0
rewards = [-1 for _ in SOLVERS] # negative rewards should be ignored.
time_spent = []
timeout = int(timeout)

budgets = [int(time_manager.get_timeout(solver, times))+1 for solver in order]
budgets = [int(time_manager.get_timeout(solver, times, point))+1 for solver in order]

for i in range(len(budgets)):
budgets[i] = min(budgets[i], max(0, int(timeout - sum(budgets[:i]))))
Expand Down Expand Up @@ -66,6 +66,6 @@ def apply_ordering(problem, order, timeout, time_manager, extra_time_to_first, t
time_spent.append(res.elapsed)

elapsed += res.elapsed
time_manager.update(solver, res.elapsed, timeout, is_solved(res.result), is_error(res.result))
time_manager.update(solver, res.elapsed, timeout, is_solved(res.result), is_error(res.result), point)
if elapsed >= timeout or is_solved(res.result) or i == len(order) - 1:
return solver, elapsed, res.result, rewards, time_spent
35 changes: 27 additions & 8 deletions medleysolver/timers.py
Original file line number Diff line number Diff line change
@@ -1,31 +1,32 @@
from medleysolver.distributions import ExponentialDist
from medleysolver.constants import SOLVERS
from sklearn.linear_model import SGDRegressor

class TimerInterface(object):
def get_timeout(self, solver, position):
def get_timeout(self, solver, times, point):
raise NotImplementedError

def update(self, solver, time, success, error):
def update(self, solver, time, success, error, point):
raise NotImplementedError

class Constant(TimerInterface):
def __init__(self, const):
self.const = const

def get_timeout(self, solver, position):
def get_timeout(self, solver, times, point):
return self.const

def update(self, solver, time, timeout, success, error):
def update(self, solver, time, timeout, success, error, point):
pass

class Exponential(TimerInterface):
def __init__(self, init_lambda, confidence, T):
self.timers = {solver:ExponentialDist(init_lambda, confidence, T) for solver in SOLVERS}

def get_timeout(self, solver, position):
def get_timeout(self, solver, times, point):
return self.timers[solver].get_cutoff()

def update(self, solver, time, timeout, success, error):
def update(self, solver, time, timeout, success, error, point):
assert(not success or not error)
if success:
self.timers[solver].add_sample(time)
Expand All @@ -41,13 +42,31 @@ def __init__(self, init_lambda, confidence, T):
self.confidence = confidence
self.T = T

def get_timeout(self, solver, times):
def get_timeout(self, solver, times, point):
# want time based on times for same solver at nearby points
timer = ExponentialDist(self.init_lambda, self.confidence, self.T)
for (s, t) in times:
if s == solver:
timer.add_sample(t)
return timer.get_cutoff()

def update(self, solver, time, timeout, success, error):
def update(self, solver, time, timeout, success, error, point):
pass

class SGD(TimerInterface):
def __init__(self, init_lambda, confidence, T):
self.fitted = [False for _ in SOLVERS]
self.models = [SGDRegressor() for _ in SOLVERS]

def get_timeout(self, solver, times, point):
if not self.fitted[solver]: return 60
clf = self.models[solver]
return clf.predict(point)

def update(self, solver, time, timeout, success, error, point):
clf = self.models[solver]
if self.fitted[solver]:
clf.partial_fit(point, time)
else:
clf.fit(point, time)

0 comments on commit a5686cf

Please sign in to comment.