-
Notifications
You must be signed in to change notification settings - Fork 0
/
RandomSearch.py
36 lines (30 loc) · 1.44 KB
/
RandomSearch.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
__author__ = 'max'
import MachineBoss
import time
import random
import math
class RandomSearch:
def __init__(self,machines,jobs):
self.machines = machines
self.jobs = jobs
self.bestAssignment = [0,[]]
for job in jobs:
kNumberOfJobs = len(job[1])
self.bestMakeSpan = 100**10
for trials in range(len(self.machines.machines)*1):
candidateAssignment = [0,[]]
fakeMachines = []
for m in machines.machines:
fakeMachines.append(m.makeSpan)
candidateAssignment[0] = random.randint(0,self.machines.numberOfMachines-1) #i-th element assignment
for i in range(kNumberOfJobs):
randIndex = random.randint(0,self.machines.numberOfMachines-1)
candidateAssignment[1].append(randIndex) #k element assignments
fakeMachines[candidateAssignment[1][i]] += job[1][i]
fakeMachines[candidateAssignment[0]] += job[0]
#we've computed a random assignment and stored it in candidateAssignment
if max(fakeMachines) < self.bestMakeSpan:
self.bestMakeSpan = max(fakeMachines)
self.bestAssignment = list(candidateAssignment)
#Found best assignemnt out of n-trials
self.machines.machines[self.bestAssignment[0]].addJob(job[0]) #and i-job to determined machine