Exemplo n.º 1
0
def run_experiment_perturbation(exp_type,
                                exp_num,
                                K,
                                I,
                                T,
                                settings,
                                rel_perturbation,
                                symmetric,
                                starting_seed=1000,
                                solver_settings=SCLP_settings(),
                                use_adaptive_T=False,
                                get_raw_tau=True,
                                **kwargs):

    num_feasible = 0
    true_objective = float("inf")
    perturbed_obj_vals = list()

    # 1. generate the "true" MCQN data
    G0, H0, F0, gamma0, c0, d0, alpha0, a0, b0, TT0, buffer_cost0, xcost0 = generate_MCQN_data(
        starting_seed, K, I, **settings)

    # 2. Solve using SCLP
    solution0, STEPCOUNT0, param_line0, res0 = SCLP(G0, H0, F0, a0, b0, c0, d0,
                                                    alpha0, gamma0, T,
                                                    solver_settings)
    t, x, q, u, p, pivots, obj, err, NN, tau, maxT = solution0.get_final_solution(
    )
    if True:
        true_objective = obj

    for seed in range(starting_seed + 1, starting_seed + 1 + exp_num):

        print("\n\n\n==============")
        print(seed)
        print("==============\n\n\n")

        # 3. Perturb the MCQN data
        G, H, F, a, b, c, d, alpha, gamma = perturb_MCQN_data(
            seed, rel_perturbation, symmetric, G0, H0, F0, a0, b0, c0, d0,
            alpha0, gamma0)

        # 4. Solve the SCLP with perturbed MCQN
        solution, STEPCOUNT, param_line, res = SCLP(G, H, F, a, b, c, d, alpha,
                                                    gamma, T, solver_settings)

        # 5. Test
        # a. Check if the "true" values are feasible under perturbation
        is_feasible = solution.is_other_feasible(solution0)
        num_feasible += int(is_feasible)

        # b. Assuming feasibility, get the objective value of the "true" solution under the perturbation
        if is_feasible:
            perturbed_obj_vals.append(solution.other_objective(solution0))

    return num_feasible, true_objective, perturbed_obj_vals
Exemplo n.º 2
0
# settings - data generation parameters, including
#   - \alpha_k = mean_alpha * alpha_rate1 + mean_alpha * U(0, alpha_rate2), where mean_alpha = 15 -15(k-1)/K  initial amount of fluids
#   - h_j ~ U(0, cost_scale) holding costs
#   - c_j ~ U(0, c_scale) linearly changing control cost
#   - there are other possible parameters changing distributions, etc.

I = 60
K = 1200
settings = {"c_scale": 0, "cost_scale": 10, "alpha_rate1": 0.8, "alpha_rate2": 0.45}
seed = 1000
G, H, F, gamma, c, d, alpha, a, b, TT, total_buffer_cost, buffer_cost = generate_simple_reentrant_data(seed, K, I, **settings)
# calculating total buffer cost for the target T:
#   tot_buf_cost = h' \alpha T + h' a T^2/2
tot_buf_cost = total_buffer_cost[0]*TT+total_buffer_cost[1]*TT*TT/2.0
# solver_settings - parameters fot SCLP solver
solver_settings = SCLP_settings()
# set suppress_printing = False if you would like to see summary of each iteration
solver_settings.suppress_printing = True
solver_settings.memory_management = False
import time
start_time = time.time()
solution, STEPCOUNT, param_line, res = SCLP(G, H, F, a, b, c, d, alpha, gamma, TT,  solver_settings)
t, x, q, u, p, pivots, obj, err, NN, tau, maxT = solution.get_final_solution(False)
# problem objective is h'x(t) or h' \alpha T + h' a T^2/2 - V(SCLP)
pobj = tot_buf_cost - obj
print("SCLP objective:", obj, "Problem objective:", pobj, "steps:", STEPCOUNT, "intervals:", len(tau))
sol_time = time.time() - start_time
print("Solution time: %s seconds" % (sol_time))
print("----------------------------------------------")
# preparing CPLEX .dat file name
ps = {'K': K, 'I': I, 'T': TT}
Exemplo n.º 3
0
import sys
import os
proj = os.path.realpath(
    os.path.join(os.path.dirname(os.path.realpath(__file__)), '..'))
sys.path.append(proj)
from SCLP import SCLP, SCLP_settings
from doe.data_generators.MCQN import generate_MCQN_data
from subroutines.utils import relative_to_project
from doe.results_producer import write_results_to_csv

K = 400
I = 40
import time
solver_settings = SCLP_settings(find_alt_line=False,
                                check_intermediate_solution=False,
                                memory_management=False,
                                suppress_printing=False)

settings = {
    'alpha_rate': 1,
    'cost_scale': 2,
    'a_rate': 0.05,
    'sum_rate': 0.95,
    'nz': 0.5,
    'gamma_rate': 0,
    'c_scale': 0,
    'h_rate': 0.2
}
seed = 1009
G, H, F, gamma, c, d, alpha, a, b, TT, total_buffer_cost, buffer_cost = generate_MCQN_data(
    seed, K, I, **settings)
Exemplo n.º 4
0
    'a_rate': 0.05,
    'sum_rate': 0.95,
    'nz': 0.5,
    'gamma_rate': 0,
    'c_scale': 0,
    'h_rate': 0.2
}
seed = 1000
G, H, F, gamma, c, d, alpha, a, b, TT, total_buffer_cost, buffer_cost = generate_MCQN_data(
    seed, K, I, **settings)
TT = 100
# calculating total buffer cost for the target T:
#   tot_buf_cost = h' \alpha T + h' a T^2/2
tot_buf_cost = total_buffer_cost[0] * TT + total_buffer_cost[1] * TT * TT / 2.0
# solver_settings - parameters fot SCLP solver
solver_settings = SCLP_settings(find_alt_line=False)
# set suppress_printing = False if you would like to see summary of each iteration
solver_settings.suppress_printing = True
solver_settings.memory_management = False
import time
start_time = time.time()
# run simplex-type algorithm to solve SCLP
solution, STEPCOUNT, param_line, res = SCLP(G, H, F, a, b, c, d, alpha, gamma,
                                            TT, solver_settings)
# extract detailed solution including primal and dual controls
t, x, q, u, p, pivots, obj, err, NN, tau, maxT = solution.get_final_solution(
    False)
# problem objective is h'x(t) or h' \alpha T + h' a T^2/2 - V(SCLP)
pobj = tot_buf_cost - obj
print("SCLP objective:", obj, "Problem objective:", pobj, "steps:", STEPCOUNT,
      "intervals:", len(tau))
Exemplo n.º 5
0
def run_experiment_series(exp_type,
                          exp_num,
                          K,
                          I,
                          T,
                          settings,
                          starting_seed=1000,
                          solver_settings=None,
                          use_adaptive_T=False,
                          get_raw_tau=True,
                          **kwargs):
    failure_trials = 0
    ps = {'K': K, 'I': I, 'T': T}
    for k, v in kwargs.items():
        ps[k] = v
    for k, v in settings.items():
        if isinstance(v, object) and hasattr(v, '__name__'):
            ps[k] = v.__name__[:4]
        else:
            ps[k] = str(v)
    #pu = path_utils(os.path.expanduser('~/Box/SCLP comparison/data'))
    pu = path_utils("C:/DataD/SCLP_data")
    results = []
    files = []
    if get_raw_tau:
        raw_tau = []
    else:
        raw_tau = None
    for seed in range(starting_seed, starting_seed + exp_num):
        ps['seed'] = seed
        if exp_type == 'MCQN':
            G, H, F, gamma, c, d, alpha, a, b, TT, total_buffer_cost, buffer_cost = generate_MCQN_data(
                seed, K, I, **settings)
        elif exp_type == 'reentrant':
            G, H, F, gamma, c, d, alpha, a, b, TT, total_buffer_cost, buffer_cost = generate_reentrant_data(
                seed, K, I, **settings)
        elif exp_type == 'simple_reentrant':
            G, H, F, gamma, c, d, alpha, a, b, TT, total_buffer_cost, buffer_cost = generate_simple_reentrant_data(
                seed, K, I, **settings)
        else:
            raise Exception('Undefined experiment type!')
        if T is None:
            if TT is None:
                raise Exception('Undefined time horizon!')
            else:
                T = TT
        if solver_settings is None:
            solver_settings = SCLP_settings(find_alt_line=False)
        solver_settings.file_name = pu.get_tmp_data_file_name(exp_type)
        import time
        start_time = time.time()
        solution, STEPCOUNT, param_line, res = SCLP(G, H, F, a, b, c, d, alpha,
                                                    gamma, T, solver_settings)
        t, x, q, u, p, pivots, obj, err, NN, tau, maxT = solution.get_final_solution(
            False)
        time_to_solve = time.time() - start_time
        print(obj, err, solution.last_T, maxT)
        print("--- %s seconds ---" % time_to_solve)
        print("--- seed %s ---" % seed)
        Tres = param_line.T
        if res == 0 or use_adaptive_T:
            if res != 0:
                ps['T'] = 'adpt'
            else:
                ps['T'] = T
            full_file_name = pu.get_CPLEX_data_file_name(exp_type, **ps)
            write_CPLEX_dat(full_file_name, Tres, G, H, alpha, a, b, gamma, c,
                            buffer_cost)
            path, filename = os.path.split(full_file_name)
            buf_cost = total_buffer_cost[0] * Tres + total_buffer_cost[
                1] * Tres * Tres / 2.0
            r = {
                'file': filename,
                'seed': seed,
                'result': res,
                'objective': obj,
                'time': time_to_solve,
                'steps': STEPCOUNT,
                'intervals': NN,
                'T': Tres,
                'maxT': maxT,
                'mean_tau': np.mean(tau),
                'max_tau': np.max(tau),
                'min_tau': np.min(tau),
                'std_tau': np.std(tau),
                'buffer_cost': buf_cost,
                'real_objective': buf_cost - obj
            }
            results.append(r)
            if get_raw_tau:
                raw_tau.append({'file': filename, 'raw_tau': tau})
            files.append(full_file_name)
        else:
            failure_trials += 1
    return results, failure_trials, files, raw_tau