I have 3 different python scripts: lap.py, lap1.py, and lap2.py. They are as follows:
lap.py
import sys
import copy
sys.path.append('c:\\svns\\code\\python')
from parseCSV import parseCSV
def initialize(fname, show):
costs = parseCSV(fname)
people = []
tasks = []
for k in range(len(costs)):
people.append(-1)
tasks.append(-1)
if show:
print '{} people, {} tasks, {}x{} costs, lb = {:.2f}'.format(
len(people),
len(tasks),
len(costs),
len(costs),
simple_lb(costs))
return costs, people, tasks
def show_solution(costs, people, tasks):
for k in range(len(people)):
if people[k] > -1:
task = 'T{}'.format(people[k])
cost = costs[k][people[k]]
else:
task = 'n/a'
cost = 0.0
print '\tP{}, {}, {:.2f} '.format(k, task, cost)
print '\nTotal cost: {:.2f} (lower bound: {:.2f})'.format(
calc_cost(costs, people)[0],
simple_lb(costs)
)
def calc_cost(costs, people):
total_cost = 0
num_assigned = 0
for k in range(len(people)):
if people[k] != -1:
total_cost += costs[k][people[k]]
num_assigned += 1
return total_cost, num_assigned
def low_cost_task(costs, person, tasks):
min_cost = 1e308
min_idx = -1
for k in range(len(tasks)):
if tasks[k] == -1:
if costs[person][k] < min_cost:
min_cost = costs[person][k]
min_idx = k
return min_idx, min_cost
def simple_lb(costs):
total_cost1 = 0;
for k in range(len(costs)) :
total_cost1 += min(costs[k])
total_cost2 = 0;
for k in range(len(costs)):
total_cost2 += min([c[k] for c in costs])
return max(total_cost1, total_cost2)
def clear_solution(people, tasks):
for k in range(len(people)):
people[k] = -1
for k in range(len(tasks)):
tasks[k] = -1
def store_solution(people, tasks, cost, seq):
solution = {}
solution['people'] = copy.copy(people)
solution['tasks'] = copy.copy(tasks)
solution['obj_val'] = cost
solution['seq'] = copy.copy(seq)
return solution
def main():
fname = 'r.csv'
if len(sys.argv) > 1:
fname = sys.argv[1]
costs, people, tasks = initialize(fname, 1)
for k in range(len(people)):
people[k] = k;
tasks[k] = k;
print '\nSolution:'
show_solution(costs, people, tasks)
if __name__ == '__main__' : main()
lap1.py
import sys
import lap
show_intermediate = 1
fname = 'r.csv'
if len(sys.argv) > 1:
fname = sys.argv[1]
if len(sys.argv) > 2:
show_intermediate = int(sys.argv[2])
costs, people, tasks = lap.initialize(fname, 1)
for k in range(len(people)):
task, min_cost = lap.low_cost_task(costs, k, tasks)
people[k] = task
tasks[task] = k
if show_intermediate:
if people[k] != -1:
print 'Assigned P{} task T{} at cost {:.2f}'.format(
k,
people[k],
min_cost
)
print '\nFinal solution:'
lap.show_solution(costs, people, tasks)
lap2.py
import sys
from random import sample
import lap
def solve(costs, people, tasks, seq):
total_cost = 0
for k in seq:
task, min_cost = lap.low_cost_task(costs, k, tasks)
people[k] = task
tasks[task] = k
total_cost += min_cost
return total_cost
def main():
nreps = 100
fname = 'r.csv'
if len(sys.argv) > 1:
fname = sys.argv[1]
if len(sys.argv) > 2:
try:
nreps = int(sys.argv[2])
except:
print 'Invalid parameters. Syntax: lab_h2.py fname nreps'
exit()
costs, people, tasks = lap.initialize(fname, 1)
cost = solve(costs, people, tasks, range(len(people)))
solution = lap.store_solution(people, tasks, cost, range(len(people)))
print 'Iteration {:3d} Cost: {:.2f}'.format(-1, solution['obj_val'])
for k in range(nreps):
lap.clear_solution(people, tasks)
seq = sample(range(len(people)), len(people))
cost = solve(costs, people, tasks, seq)
print 'Iteration {:3d} Cost: {:.2f}'.format(k, cost)
if cost < solution['obj_val'] :
solution = lap.store_solution(people, tasks, cost, seq)
print '\nFinal solution:'
print 'Sequence: {}'.format(solution['seq'])
lap.show_solution(costs, solution['people'], solution['tasks'])
if __name__ == '__main__' : main()
I would like to implement the random pairs pairwise interchange heuristic for the LAP, using 100,000 random pairs, but I do not know how to continue after developing the three scripts. Any help would be appreciated
Aucun commentaire:
Enregistrer un commentaire