Beispiel #1
0
def run_steepest_func_2():
    csv_table = CSVFormatter()

    # x_0 * x_1 + (1-x_0)^2 + (2-x_1)^2+ (3-x_2)^2+ (4-x_3)^2+ (5-x_4)^2+ (6-x_5)^2+ (7-x_6)^2+ (8-x_7)^2+ (9-x_8)^2+ (10-x_9)^2
    ## 10 Dimension
    test_func = lambda x: x[0] * x[1] + sum([(ele - x[ele - 1])**2
                                             for ele in range(1, 11)])
    fprime = lambda x: [x[1] + -2 * (1 - x[0]),
                        x[0] + -2 * (2 - x[1])] + \
                        [-2*(ele - x[ele - 1]) for ele in range(3, 11)]

    csv_table.add_row(STEEP_HEADER)

    x0_param = [[random.gauss(-10, 10) for _ in range(10)]
                for _ in range(NUM_RUNS)]

    eps = 1e-14
    for i in range(10):
        gc_iterations_param_dist = [10] * NUM_RUNS
        epsilon_abs_param_dist = [eps] * NUM_RUNS
        run_report_list = TestHarness.test_optimizer(
            alternate_method, test_func, x0_param,
            [fprime for _ in range(NUM_RUNS)], epsilon_abs_param_dist)
        stats = StatsGenerator.generate_stats(run_report_list, (1, 1), 1)
        print(stats)
        csv_table.add_row([
            epsilon_abs_param_dist[i],
            CELL_FORMAT_STR.format(stats[0][0], stats[0][1]),
            CELL_FORMAT_STR.format(stats[1][0], stats[1][1])
        ])
        eps *= 10

    csv_table.export_table("steepest_func2.csv")
    print("Finished steepest decent function 2")
    csv_table.clear_table()
Beispiel #2
0
def run_steepest_func_1():
    csv_table = CSVFormatter()

    ## FIRST FUNCTION
    ## Rosenblock
    test_func = lambda x: (1 - x[0])**2 + 100 * (x[1] - x[0]**2)**2 + 1
    fprime = lambda x: (2 *
                        (200 * x[0]**3 - 200 * x[0] * x[1] + x[0] - 1), 200 *
                        (x[1] - x[0]**2))

    csv_table.add_row(STEEP_HEADER)
    # Func 1 epsilon variation
    x0_param = [(random.gauss(-10, 10), random.gauss(-10, 10))
                for _ in range(NUM_RUNS)]

    eps = 1e-14
    for i in range(10):
        gc_iterations_param_dist = [10] * NUM_RUNS
        epsilon_abs_param_dist = [eps] * NUM_RUNS
        run_report_list = TestHarness.test_optimizer(
            alternate_method, test_func, x0_param,
            [fprime for _ in range(NUM_RUNS)], epsilon_abs_param_dist)
        stats = StatsGenerator.generate_stats(run_report_list, (1, 1), 1)
        print(stats)
        csv_table.add_row([
            epsilon_abs_param_dist[i],
            CELL_FORMAT_STR.format(stats[0][0], stats[0][1]),
            CELL_FORMAT_STR.format(stats[1][0], stats[1][1])
        ])
        eps *= 10

    csv_table.export_table("steepest_func1.csv")
    print("Finished steepest decent function 1")
    csv_table.clear_table()
 def setUp(self):
     DefaultTestFixture.setUp(self)
     self.data = _DataHolder()
     self.data.configure(['timestamp', 'column-A'])
     for x in range(1, 20):
         self.data.add_entry([time.time(), x])
     exporters = _ParentHolder(self.data)
     exporter = _ParentHolder(exporters)
     exporter.log = self.data
     self.formatter = CSVFormatter()
     self.formatter.configure({'name': 'csvformatter', 'parent': exporter})
Beispiel #4
0
def problem_1_b():
    csvf = CSVFormatter()
    csvf.add_row(["Time (ms)"])
    color_set = range(3)
    problem_size = 10
    num_runs = 100

    cg = GraphColoring(color_set)

    harness_param_x0 = [[
        random.choice(color_set) for _ in range(problem_size)
    ] for _ in range(num_runs)]
    harness_allowed = [[color_set for _ in range(problem_size)]] * num_runs
    harness_t0 = [25000] * num_runs
    harness_t_final = [0.1] * num_runs
    run_report_list = TestHarness.test_optimizer(simulated_annealing, cg.solve,
                                                 harness_param_x0,
                                                 harness_allowed, harness_t0,
                                                 harness_t_final)

    print("Finished coloring graph runs")
    time_stats = StatsGenerator.stats_to_string(
        StatsGenerator.generate_stats(
            list(map(lambda x: x[0], run_report_list))))
    completion_stats = StatsGenerator.stats_to_string(
        StatsGenerator.generate_stats(
            list(map(lambda x: x[2], run_report_list))))
    print(time_stats, completion_stats)
    csvf.add_row([time_stats, completion_stats])

    csvf.export_table("sa_graph_coloring")
 def setUp(self):
     DefaultTestFixture.setUp(self)
     self.data = _DataHolder()
     self.data.configure(['timestamp','column-A'])
     for x in range(1,20):
         self.data.add_entry([time.time(),x])
     exporters = _ParentHolder(self.data)
     exporter = _ParentHolder(exporters)
     exporter.log = self.data
     self.formatter = CSVFormatter()
     self.formatter.configure({'name':'csvformatter','parent':exporter})
class TestCase(DefaultTestFixture):
    def setUp(self):
        DefaultTestFixture.setUp(self)
        self.data = _DataHolder()
        self.data.configure(['timestamp', 'column-A'])
        for x in range(1, 20):
            self.data.add_entry([time.time(), x])
        exporters = _ParentHolder(self.data)
        exporter = _ParentHolder(exporters)
        exporter.log = self.data
        self.formatter = CSVFormatter()
        self.formatter.configure({'name': 'csvformatter', 'parent': exporter})

    def test_1(self):
        columns = ''
        formatter = self.formatter
        stream = formatter.format(self.data)
        csv = ''
        data = stream.read(1024)
        while data:
            csv += data
            data = stream.read(1024)
        list = string.split(csv)
        columns = string.split(list[0], ',')
        if 'timestamp' in columns:
            if len(columns) > 2:
                self.fail('first line has to much information:\n ' +
                          string.join(columns, ','))
        else:
            self.fail('timestamp column was not found on the first line')

    def test_2(self):
        columns = ''
        formatter = self.formatter
        stream = formatter.format(self.data)
        csv = ''
        data = stream.read(1024)
        while data:
            csv += data
            data = stream.read(1024)
        list = string.split(csv)
        values = string.split(list[1], ',')
        if len(values) == 2:
            t = time.strptime(values[0], '%Y-%m-%dT%H:%M:%S')
        else:
            self.fail('wrong number of values in line #2')

    def test_3(self):
        columns = ''
        formatter = self.formatter
        stream = formatter.format(self.data)
        csv = ''
        data = stream.read(1024)
        while data:
            csv += data
            data = stream.read(1024)
        list = string.split(csv)
        values = string.split(list[1], ',')
        for v in list[2:]:
            if len(string.split(v, ',')) != 2:
                self.fail('wrong number of values in line')

    def test_EInconsistantFormat(self):
        formatter = self.formatter
        data = self.data
        for x in range(1, 100):
            entry = {}
            entry['timestamp'] = time.time()
            entry['column-TEST'] = x
            data.append(entry)
        try:
            stream = formatter.format(self.data)
            csv = ''
            data = stream.read(1024)
            while data:
                csv += data
                data = stream.read(1024)
            raise 'test_2 failed, did not raise EBreakupTransfer'
        except EBreakupTransfer, e:
            pass
        except:
class TestCase(DefaultTestFixture):
    def setUp(self):
        DefaultTestFixture.setUp(self)
        self.data = _DataHolder()
        self.data.configure(['timestamp','column-A'])
        for x in range(1,20):
            self.data.add_entry([time.time(),x])
        exporters = _ParentHolder(self.data)
        exporter = _ParentHolder(exporters)
        exporter.log = self.data
        self.formatter = CSVFormatter()
        self.formatter.configure({'name':'csvformatter','parent':exporter})
    def test_1(self):
        columns = ''
        formatter = self.formatter
        stream = formatter.format(self.data)
        csv = ''
        data = stream.read(1024)
        while data:
            csv += data
            data = stream.read(1024)
        list = string.split(csv)
        columns = string.split(list[0],',')  
        if 'timestamp' in columns:
            if len(columns) > 2:
                self.fail('first line has to much information:\n ' +
                          string.join(columns,','))            
        else:
            self.fail('timestamp column was not found on the first line')
    def test_2(self):
        columns = ''
        formatter = self.formatter
        stream = formatter.format(self.data)
        csv = ''
        data = stream.read(1024)
        while data:
            csv += data
            data = stream.read(1024)
        list = string.split(csv)
        values = string.split(list[1],',')    
        if len(values) == 2:
            t = time.strptime(values[0],'%Y-%m-%dT%H:%M:%S')
        else:
            self.fail('wrong number of values in line #2')
    def test_3(self):
        columns = ''
        formatter = self.formatter
        stream = formatter.format(self.data)
        csv = ''
        data = stream.read(1024)
        while data:
            csv += data
            data = stream.read(1024)
        list = string.split(csv)
        values = string.split(list[1],',')
        for v in list[2:]:
            if len(string.split(v,',')) != 2:
                self.fail('wrong number of values in line')
    def test_EInconsistantFormat(self):
        formatter = self.formatter
        data = self.data
        for x in range(1,100):
            entry = {}
            entry['timestamp'] = time.time()
            entry['column-TEST'] = x
            data.append(entry)
        try:
            stream = formatter.format(self.data)
            csv = ''
            data = stream.read(1024)
            while data:
                csv += data
                data = stream.read(1024)
            raise 'test_2 failed, did not raise EBreakupTransfer'
        except EBreakupTransfer,e:
            pass
        except:
Beispiel #8
0
def run_cg_func_2():
    csv_table = CSVFormatter()

    test_func = lambda x: x[0] * x[1] + sum([(ele - x[ele - 1])**2
                                             for ele in range(1, 11)])
    fprime = lambda x: [x[1] + -2 * (1 - x[0]),
                        x[0] + -2 * (2 - x[1])] + \
                        [-2*(ele - x[ele - 1]) for ele in range(3, 11)]

    x0_param = [[random.gauss(-10, 10) for _ in range(10)]
                for _ in range(NUM_RUNS)]
    epsilon_abs_param_dist = [1.14e-14] * NUM_RUNS

    csv_table.add_row(CG_HEADER)
    for i in range(NUM_VARIATIONS):
        gc_iterations_param_dist = [random.randint(2, NUM_VARIATIONS)
                                    ] * NUM_RUNS
        run_report_list = TestHarness.test_optimizer(
            conjugate_gradients, test_func, x0_param,
            [fprime for _ in range(NUM_RUNS)], gc_iterations_param_dist,
            epsilon_abs_param_dist)
        stats = StatsGenerator.generate_stats(run_report_list,
                                              (0, 2, 3, 4, 5, 6, 7, 8, 9, 10),
                                              1)
        print(stats)
        csv_table.add_row([
            gc_iterations_param_dist[i], epsilon_abs_param_dist[i],
            CELL_FORMAT_STR.format(stats[0][0], stats[0][1]),
            CELL_FORMAT_STR.format(stats[1][0], stats[1][1]),
            CELL_FORMAT_STR.format(stats[2][0], stats[2][1])
        ])

    csv_table.export_table("function2_gc_freq_variation.csv")
    csv_table.clear_table()
    print("Finished function 2 GC restart variation")

    csv_table.add_row(CG_HEADER)
    # Func 1 epsilon variation
    eps = 1e-14
    for i in range(10):
        gc_iterations_param_dist = [10] * NUM_RUNS
        epsilon_abs_param_dist = [eps] * NUM_RUNS
        run_report_list = TestHarness.test_optimizer(
            conjugate_gradients, test_func, x0_param,
            [fprime for _ in range(NUM_RUNS)], gc_iterations_param_dist,
            epsilon_abs_param_dist)
        stats = StatsGenerator.generate_stats(run_report_list,
                                              (0, 2, 3, 4, 5, 6, 7, 8, 9, 10),
                                              1)
        print(stats)
        csv_table.add_row([
            gc_iterations_param_dist[i], epsilon_abs_param_dist[i],
            CELL_FORMAT_STR.format(stats[0][0], stats[0][1]),
            CELL_FORMAT_STR.format(stats[1][0], stats[1][1]),
            CELL_FORMAT_STR.format(stats[2][0], stats[2][1])
        ])
        eps *= 10

    csv_table.export_table("function2_epsilon_variation.csv")
    print("Finished function 2 epsilon variation")
    csv_table.clear_table()
Beispiel #9
0
def run_cg_func_1():
    csv_table = CSVFormatter()

    ## FIRST FUNCTION
    ## Rosenblock
    test_func = lambda x: (1 - x[0])**2 + 100 * (x[1] - x[0]**2)**2 + 1
    fprime = lambda x: (2 *
                        (200 * x[0]**3 - 200 * x[0] * x[1] + x[0] - 1), 200 *
                        (x[1] - x[0]**2))

    # Vary GC restart frequency (2->50 drawn from uniform distribution

    # Func 1 conj variation
    epsilon_abs_param_dist = [1.14e-14] * NUM_RUNS
    x0_param = [(random.gauss(-10, 10), random.gauss(-10, 10))
                for _ in range(NUM_RUNS)]

    csv_table.add_row(CG_HEADER)
    for i in range(NUM_VARIATIONS):
        gc_iterations_param_dist = [random.randint(2, NUM_VARIATIONS)
                                    ] * NUM_RUNS
        run_report_list = TestHarness.test_optimizer(
            conjugate_gradients, test_func, x0_param,
            [fprime for _ in range(NUM_RUNS)], gc_iterations_param_dist,
            epsilon_abs_param_dist)
        stats = StatsGenerator.generate_stats(run_report_list, (1, 1), 1)
        print(stats)
        csv_table.add_row([
            gc_iterations_param_dist[i], epsilon_abs_param_dist[i],
            CELL_FORMAT_STR.format(stats[0][0], stats[0][1]),
            CELL_FORMAT_STR.format(stats[1][0], stats[1][1]),
            CELL_FORMAT_STR.format(stats[2][0], stats[2][1])
        ])

    csv_table.export_table("function1_gc_freq_variation.csv")
    csv_table.clear_table()
    print("Finished function 1 GC restart variation")

    csv_table.add_row(CG_HEADER)
    # Func 1 epsilon variation
    eps = 1e-14
    for i in range(10):
        gc_iterations_param_dist = [10] * NUM_RUNS
        epsilon_abs_param_dist = [eps] * NUM_RUNS
        run_report_list = TestHarness.test_optimizer(
            conjugate_gradients, test_func, x0_param,
            [fprime for _ in range(NUM_RUNS)], gc_iterations_param_dist,
            epsilon_abs_param_dist)
        stats = StatsGenerator.generate_stats(run_report_list, (1, 1), 1)
        print(stats)
        csv_table.add_row([
            gc_iterations_param_dist[i], epsilon_abs_param_dist[i],
            CELL_FORMAT_STR.format(stats[0][0], stats[0][1]),
            CELL_FORMAT_STR.format(stats[1][0], stats[1][1]),
            CELL_FORMAT_STR.format(stats[2][0], stats[2][1])
        ])
        eps *= 10

    csv_table.export_table("function1_epsilon_variation.csv")
    print("Finished function 1 epsilon variation")
    csv_table.clear_table()
Beispiel #10
0
def problem_1_a():

    csvf = CSVFormatter()
    csvf.add_row(["Problem Size", "Time (ms)", "Profit"])
    values = [
        360, 83, 59, 130, 431, 67, 230, 52, 93, 125, 670, 892, 600, 38, 48,
        147, 78, 256, 63, 17, 120, 164, 432, 35, 92, 110, 22, 42, 50, 323, 514,
        28, 87, 73, 78, 15, 26, 78, 210, 36, 85, 189, 274, 43, 33, 10, 19, 389,
        276, 312, 360, 83, 59, 130, 431, 67, 230, 52, 93, 125, 670, 892, 600,
        38, 48, 147, 78, 256, 63, 17, 120, 164, 432, 35, 92, 110, 22, 42, 50,
        323, 514, 28, 87, 73, 78, 15, 26, 78, 210, 36, 85, 189, 274, 43, 33,
        10, 19, 389, 276, 312, 360, 83, 59, 130, 431, 67, 230, 52, 93, 125,
        670, 892, 600, 38, 48, 147, 78, 256, 63, 17, 120, 164, 432, 35, 92,
        110, 22, 42, 50, 323, 514, 28, 87, 73, 78, 15, 26, 78, 210, 36, 85,
        189, 274, 43, 33, 10, 19, 389, 276, 312, 360, 83, 59, 130, 431, 67,
        230, 52, 93, 125, 670, 892, 600, 38, 48, 147, 78, 256, 63, 17, 120,
        164, 432, 35, 92, 110, 22, 42, 50, 323, 514, 28, 87, 73, 78, 15, 26,
        78, 210, 36, 85, 189, 274, 43, 33, 10, 19, 389, 276, 312
    ]

    weights = [
        7, 0, 30, 22, 80, 94, 11, 81, 70, 64, 59, 18, 0, 36, 3, 8, 15, 42, 9,
        0, 42, 47, 52, 32, 26, 48, 55, 6, 29, 84, 2, 4, 18, 56, 7, 29, 93, 44,
        71, 3, 86, 66, 31, 65, 0, 79, 20, 65, 52, 13, 7, 0, 30, 22, 80, 94, 11,
        81, 70, 64, 59, 18, 0, 36, 3, 8, 15, 42, 9, 0, 42, 47, 52, 32, 26, 48,
        55, 6, 29, 84, 2, 4, 18, 56, 7, 29, 93, 44, 71, 3, 86, 66, 31, 65, 0,
        79, 20, 65, 52, 13, 7, 0, 30, 22, 80, 94, 11, 81, 70, 64, 59, 18, 0,
        36, 3, 8, 15, 42, 9, 0, 42, 47, 52, 32, 26, 48, 55, 6, 29, 84, 2, 4,
        18, 56, 7, 29, 93, 44, 71, 3, 86, 66, 31, 65, 0, 79, 20, 65, 52, 13, 7,
        0, 30, 22, 80, 94, 11, 81, 70, 64, 59, 18, 0, 36, 3, 8, 15, 42, 9, 0,
        42, 47, 52, 32, 26, 48, 55, 6, 29, 84, 2, 4, 18, 56, 7, 29, 93, 44, 71,
        3, 86, 66, 31, 65, 0, 79, 20, 65, 52, 13
    ]

    num_runs = 5
    knapsack_problem_size_range = (5, 125, 5)
    max_weight_modifier = 37

    harness_param_t0 = [25000] * num_runs
    harness_param_t_final = [0.1] * num_runs

    for problem_size in range(*knapsack_problem_size_range):
        # create knapsack problem for test
        kp = KnapsackProblem(max_weight=max_weight_modifier * problem_size,
                             values=values[:problem_size],
                             weights=weights[:problem_size])

        harness_param_x0 = [[
            round(random.uniform(0, 1)) for _ in range(problem_size)
        ] for _ in range(num_runs)]
        allowed = [[0, 1] for _ in range(problem_size)]
        harness_param_allowed = [allowed] * num_runs

        run_report_list = TestHarness.test_optimizer(
            simulated_annealing, kp.get_profit, harness_param_x0,
            harness_param_allowed, harness_param_t0, harness_param_t_final)

        time_stats = StatsGenerator.generate_stats(
            list(map(lambda x: x[0], run_report_list)))
        profit_stats = StatsGenerator.generate_stats(
            list(map(lambda x: x[2], run_report_list)))

        table_entry = [
            problem_size,
            StatsGenerator.stats_to_string(time_stats),
            StatsGenerator.stats_to_string(profit_stats)
        ]
        csvf.add_row(table_entry)
        print("Finished problem size:", problem_size)
        print(table_entry)

    csvf.export_table("sa_knapsack_test.csv")
Beispiel #11
0
def problem_2():

    nm_csvf = CSVFormatter()
    sa_csvf = CSVFormatter()

    nm_csvf.add_row(["Time (ms)", "Minimum"])
    sa_csvf.add_row(["Time (ms)", "Minimum"])

    nm_test_function = lambda x: x[0]**2 + 2 * x[1]**2 + 2 * x[0] * x[1]

    num_runs = 1000

    harness_sa_allowed = [[[], []]] * num_runs
    harness_sa_t0 = [25000] * num_runs
    harness_sa_t_final = [0.1] * num_runs

    harness_x0 = [(random.uniform(-100, 100), random.uniform(-100, 100))
                  for _ in range(num_runs)]

    nm_run_report = TestHarness.test_optimizer(nelder_mead, nm_test_function,
                                               harness_x0)
    sa_run_report = TestHarness.test_optimizer(simulated_annealing,
                                               nm_test_function, harness_x0,
                                               harness_sa_allowed,
                                               harness_sa_t0,
                                               harness_sa_t_final)

    nm_time_stats = StatsGenerator.generate_stats(
        list(map(lambda x: x[0], nm_run_report)))
    nm_min_stats = StatsGenerator.generate_stats(
        list(map(lambda x: x[2], nm_run_report)))

    sa_time_stats = StatsGenerator.generate_stats(
        list(map(lambda x: x[0], sa_run_report)))
    sa_min_stats = StatsGenerator.generate_stats(
        list(map(lambda x: x[2], sa_run_report)))

    nm_csvf.add_row([
        StatsGenerator.stats_to_string(nm_time_stats),
        StatsGenerator.stats_to_string(nm_min_stats)
    ])
    sa_csvf.add_row([
        StatsGenerator.stats_to_string(sa_time_stats),
        StatsGenerator.stats_to_string(sa_min_stats)
    ])

    nm_csvf.export_table("tables/nelder_mead_stats.csv")
    sa_csvf.export_table("tables/continuous_simulated_annealing_stats.csv")