Ejemplo n.º 1
0
    if len(sys.argv) < 2:
        print "USAGE: python sga.py <input filename>"
        exit(1)
    else:	
        scores = []
        results = []
        iters = 1 if len(sys.argv) == 2 else 1
        instance = read_instance(sys.argv[1])
	sga = SGA(instance)
	max_iters = 200
	population_size = 40
	m = 10
	for i in xrange(iters):
		# matching 100, 100, 10
		# matching 50, 100, 10
	        logs, _ =  sga.go(population_size, max_iters, m, 0.9, 0.4)
		results.append(logs)	
        print logs[1][-1]
        #scores.sort()
        #print "Best score: ", scores[0]
        #print "Average score:", 1. * sum([e for e, s in scores]) / len(scores),
        #print 1. * sum([s for e, s in scores]) / len(scores)
	brut = Brut(instance)
	brut_result = brut.run(len(logs[0])*m+population_size)
	results.append((logs[0], [brut_result]*len(logs[0])))
    print brut_result
    if len(sys.argv) > 2:
        inst = sys.argv[1].split('/')[-1]
        plot(results, 'SGA + PMX + Swap & Shift Mutations (instance: %s)'
            % (inst), '%s_%s' % (inst, sys.argv[2]))
Ejemplo n.º 2
0
    def binary_random(self, p):
        return random.random() < p

    def initial_probability_vector(self):
        return [0.5]*self.d

    def random_individual(self, ps):
        return [self.binary_random(ps[x]) for x in xrange(0, self.d)]

    def random_population(self, ps, n):
        return [self.random_individual(ps) for x in xrange(0, n)]


if __name__ == '__main__':
    if len(sys.argv) < 2:
        print "USAGE: python sga.py <input filename>"
        exit(1)
    else:
        instance = read_instance(sys.argv[1])
        pbil = PBIL(instance.bids_count, 100)
        best, best_score, trace = pbil.run(instance, 20, 0.2, 1./instance.bids_count, 0.1)
        brut = Brut(instance)
        brut_result = brut.run(100*20)
        print instance.evaluate_bin(trace[-1])
        print brut_result
        inst = sys.argv[1].split('/')[-1]
        results = []
        results.append( ([i for i in xrange(len(trace))], map((lambda x: instance.evaluate_bin(x)), trace) ) )
        results.append( ([1, len(trace)-1], [brut_result]*2) )
        plot(results, "PBIL", "%s_%s" % (inst, "pbil.png"))