コード例 #1
0
def test_abs_weight_restrictions_multiplier_model(data):

    base_model = MultiplierModelBase(data, 0, MultiplierInputOrientedModel())

    bounds = {'I2': (0.01, 0.5)}
    model = MultiplierModelWithAbsoluteWeightRestrictions(base_model, bounds)
    start_time = datetime.datetime.now()
    model_solution = model.run()
    end_time = datetime.datetime.now()
    utils_for_tests.check_if_category_is_within_abs_limits(
        model_solution, bounds)

    work_book = Workbook()
    writer = XLSWriter(Parameters(), work_book, datetime.datetime.today(),
                       (end_time - start_time).total_seconds())
    writer.write_data(model_solution)
    work_book.save('tests/test_abs_weights_multi_output.xls')

    bounds = {'I2': (None, 0.05)}
    model = MultiplierModelWithAbsoluteWeightRestrictions(base_model, bounds)

    start_time = datetime.datetime.now()
    model_solution = model.run()
    end_time = datetime.datetime.now()

    utils_for_tests.check_if_category_is_within_abs_limits(
        model_solution, bounds)

    work_book2 = Workbook()
    writer = XLSWriter(Parameters(), work_book2, datetime.datetime.today(),
                       (end_time - start_time).total_seconds())
    writer.write_data(model_solution)
    work_book2.save('tests/test_abs_weights_upper_bound_multi_output.xls')
コード例 #2
0
def test_create_params_str():
    params = Parameters()
    params.update_parameter('ORIENTATION', 'input')
    params.update_parameter('RETURN_TO_SCALE', 'VRS')
    assert dea_utils.create_params_str(params) == 'input orientation, VRS'
    params.update_parameter('RETURN_TO_SCALE', 'CRS')
    assert dea_utils.create_params_str(params) == 'input orientation, CRS'
コード例 #3
0
def test_super_efficiency_medium(DEA_example2_data):
    data = DEA_example2_data
    data.add_input_category('I1')
    data.add_input_category('I2')
    data.add_input_category('I3')
    data.add_output_category('O1')
    data.add_output_category('O2')
    model = EnvelopmentModelBase(
        data,
        EnvelopmentModelInputOriented(generate_supper_efficiency_upper_bound),
        DefaultConstraintCreator())
    super_efficiency_model = SupperEfficiencyModel(model)
    start_time = datetime.datetime.now()
    solution = super_efficiency_model.run()
    end_time = datetime.datetime.now()
    utils_for_tests.check_optimal_solution_status_and_sizes(solution, data)
    dmus = ['A', 'B', 'C', 'D', 'E', 'F', 'G', 'H', 'I', 'J', 'K']
    utils_for_tests.check_efficiency_scores(dmus, [
        1.652173892, 0.6485875732, 0.909247759, 1.116838534, 0.5490716156,
        2.485294106, 1.244945494, 0.824120607, 2.299898658, 0.6267333816,
        1.088235274
    ], solution, data, 1e-6)
    work_book = xlwt.Workbook()
    writer = XLSWriter(Parameters(), work_book, datetime.datetime.today(),
                       (end_time - start_time).total_seconds())
    writer.write_data(solution)
    work_book.save('tests/test_super_efficiency_output.xls')
コード例 #4
0
def test_all_constraints_multiplier_model(data):

    model = MultiplierModelBase(data, 0, MultiplierInputOrientedModel())

    bounds = {'I1': (None, 0.4)}
    model = MultiplierModelWithVirtualWeightRestrictions(model, bounds)

    abs_bounds = {'I2': (None, 0.2)}
    model = MultiplierModelWithAbsoluteWeightRestrictions(model, abs_bounds)

    ratio_bounds = {('I1', 'I2'): (None, 0.4), ('O1', 'O2'): (0.01, None)}
    model = MultiplierModelWithPriceRatioConstraints(model, ratio_bounds)
    start_time = datetime.datetime.now()
    model_solution = model.run()
    end_time = datetime.datetime.now()
    utils_for_tests.check_if_category_is_within_abs_limits(
        model_solution, abs_bounds)
    utils_for_tests.check_if_category_is_within_virtual_limits(
        model_solution, bounds)
    utils_for_tests.check_if_category_is_within_price_ratio_constraints(
        model_solution, ratio_bounds)

    work_book = Workbook()
    writer = XLSWriter(Parameters(), work_book, datetime.datetime.today(),
                       (end_time - start_time).total_seconds())
    writer.write_data(model_solution)
    work_book.save('tests/test_all_constraints_multi_output.xls')
コード例 #5
0
def test_maximize_slacks_usual_weakly_disposable_vars():
    categories, xls_data, dmu_name, sheet_name = read_data(
        'tests/DEA_example2_data.xls')
    coefficients, has_same_dmus = convert_to_dictionary(xls_data)
    assert has_same_dmus is False
    assert validate_data(categories, coefficients) is True
    data = construct_input_data_instance(categories, coefficients)
    data.add_input_category('I1')
    data.add_input_category('I2')
    data.add_input_category('I3')
    data.add_output_category('O1')
    data.add_output_category('O2')
    model = EnvelopmentModelVRSDecorator(
        EnvelopmentModelBase(
            data,
            EnvelopmentModelInputOriented(
                generate_upper_bound_for_efficiency_score),
            DisposableVarsConstraintCreator(set(['I2', 'O1']))))
    model = MaximizeSlacksModel(model)
    start_time = datetime.datetime.now()
    model_solution = model.run()
    end_time = datetime.datetime.now()
    utils_for_tests.check_optimal_solution_status_and_sizes(
        model_solution, data)
    dmus = ['A', 'B', 'C', 'D', 'E', 'F', 'G', 'H', 'I', 'J', 'K']
    utils_for_tests.check_efficiency_scores(
        dmus, [1, 0.86998617, 1, 1, 1, 1, 1, 1, 1, 0.6386574, 1],
        model_solution, data)
    work_book = xlwt.Workbook()
    writer = XLSWriter(Parameters(), work_book, datetime.datetime.today(),
                       (end_time - start_time).total_seconds())
    writer.write_data(model_solution)
    work_book.save('tests/test_max_slacks_weakly_disp_vars_output.xls')
    clean_up_pickled_files()
コード例 #6
0
def text_frame_price_ratio(request):
    categories = ['I1', 'I2', 'O1', 'O2']
    text_frame = TextForWeights(None,
                                'price ratio', 'Input <= 0.5', categories,
                                Parameters(), 'PRICE_RATIO_RESTRICTIONS', True)
    request.addfinalizer(text_frame.destroy)
    return text_frame
コード例 #7
0
def test_run_with_categorical_dmus(categorical_from_book):
    data = categorical_from_book
    model = MultiplierModelBase(data, 0,
                                MultiplierInputOrientedModel())
    categorical_model = ModelWithCategoricalDMUs(model, 'Category')
    start_time = datetime.datetime.now()
    solution = categorical_model.run()
    end_time = datetime.datetime.now()
    dmus = ['L1', 'L2', 'L3', 'L4', 'L5', 'L6', 'L7', 'L8', 'L9', 'L10',
            'L11', 'L12',
            'L13', 'L14', 'L15', 'L16', 'L17', 'L18', 'L19', 'L20',
            'L21', 'L22', 'L23']
    utils_for_tests.check_efficiency_scores(dmus, [0.377, 0.879, 0.936, 1, 1, 1,
                                                   0.743, 0.648, 1, 0.815,
                                                   0.646,
                                                   0.835, 0.794, 0.835, 1,
                                                   0.687, 1, 0.787, 1,
                                                   0.849, 0.787, 0.681, 1],
                                            solution, data, 1e-3)
    work_book = Workbook()
    writer = XLSWriter(Parameters(), work_book, datetime.datetime.today(),
                       (end_time - start_time).total_seconds(),
                       categorical='Category')
    writer.write_data(solution)
    work_book.save('tests/test_categorical_output.xls')
コード例 #8
0
def build_models(params, model_input):
    ''' Creates several models and parameters in the case when
        RETURN_TO_SCALE or ORIENTATION is set to both. If neither
        RETURN_TO_SCALE
        nor ORIENTATION is set to both, then no new models will be created,
        a copy of the given model and parameters will be returned.

        Args:
            params (Parameters): parameters.
            model_input (InputData): data instance.

        Returns:
            tuple of list of ModelBase, list of Parameters: tuple with two
                lists. The first list contains all created models, the
                second list contains corresponding parameters.
    '''
    model_factory.add_input_and_output_categories(params, model_input)
    rts_type = params.get_parameter_value('RETURN_TO_SCALE')
    list_of_param_objects = []
    original_params = Parameters()
    original_params.copy_all_params(params)
    list_of_param_objects.append(original_params)
    if rts_type == 'both':
        original_params.update_parameter('RETURN_TO_SCALE', 'VRS')
        params_with_crs = Parameters()
        params_with_crs.copy_all_params(original_params)
        params_with_crs.update_parameter('RETURN_TO_SCALE', 'CRS')
        list_of_param_objects.append(params_with_crs)
    orientation_type = original_params.get_parameter_value('ORIENTATION')
    nb_param_objs = len(list_of_param_objects)
    if orientation_type == 'both':
        possible_orientation = ['input', 'output']
        for count in range(nb_param_objs):
            assert(count < 2)
            param_obj = list_of_param_objects[count]
            param_obj.update_parameter('ORIENTATION',
                                       possible_orientation[count])
            new_param_obj = Parameters()
            new_param_obj.copy_all_params(param_obj)
            new_param_obj.update_parameter('ORIENTATION',
                                           possible_orientation[1 - count])
            list_of_param_objects.append(new_param_obj)

    models = []
    for param_object in list_of_param_objects:
        models.append(model_factory.create_model(param_object, model_input))
    return models, list_of_param_objects
コード例 #9
0
def test_add_empty_input_and_output_categories(DEA_example_data):
    allParams = Parameters()
    allParams.update_parameter('INPUT_CATEGORIES', '')
    allParams.update_parameter('OUTPUT_CATEGORIES', 'O1; O2')
    with pytest.raises(ValueError) as excinfo:
        factory.add_input_and_output_categories(allParams, DEA_example_data)
    assert str(
        excinfo.value) == 'Both input and output categories must be specified'
コード例 #10
0
 def __init__(self, parent):
     super().__init__(parent)
     self.options_frame = OptionsFrameMock(parent)
     self.input_categories_frame = CategoryFrameMock()
     self.output_categories_frame = CategoryFrameMock()
     self.clear_all_called = False
     self.weight_frame_name = ''
     self.params = Parameters()
コード例 #11
0
def weight_frame(request):
    parent = Tk()
    current_categories = ['I1', 'I2', 'I3']
    params = ParamsFrameMock(parent)
    weight_frame = WeightFrame(params, current_categories, Parameters(),
                               StringVar(master=parent))
    request.addfinalizer(parent.destroy)
    return weight_frame
コード例 #12
0
def box(request):
    parent = Tk()
    box = CategoriesCheckBox(parent, 'text', True,
                             Parameters(), 'INPUT_CATEGORIES')
    global categories
    for category in categories:
        box.add_category(category)
    request.addfinalizer(parent.destroy)
    return box
コード例 #13
0
def options_frame(request):
    parent = Tk()
    params = Parameters()
    current_categories = []
    options_frame = OptionsFrameMock(parent, params, current_categories,
                                     CategoryFrameMock(),
                                     CategoryFrameMock())
    request.addfinalizer(parent.destroy)
    return options_frame
コード例 #14
0
def test_add_input_and_output_categories(DEA_example_data):
    allParams = Parameters()
    allParams.update_parameter('INPUT_CATEGORIES', 'I1; I2')
    allParams.update_parameter('OUTPUT_CATEGORIES', 'O1; O2')
    factory.add_input_and_output_categories(allParams, DEA_example_data)
    assert 'I1' in DEA_example_data.input_categories
    assert 'I2' in DEA_example_data.input_categories
    assert 'O1' in DEA_example_data.output_categories
    assert 'O2' in DEA_example_data.output_categories
コード例 #15
0
def test_write_data_xls(model):
    start_time = datetime.datetime.now()
    model_solution = model.run()
    end_time = datetime.datetime.now()
    work_book = xlwt.Workbook()
    params = Parameters()
    writer = XLSWriter(params, work_book, datetime.datetime.today(),
                       (end_time - start_time).total_seconds())
    writer.write_data(model_solution)
    work_book.save('tests/test_output.xls')
コード例 #16
0
def test_show_solution(sol_frame, model):
    model_solution, ranks, state = peel_the_onion_method(model)
    sol_frame.show_solution([model_solution],
                            Parameters(), ['params to print'],
                            datetime.datetime.today(),
                            5,
                            ranks=[ranks])
    assert sol_frame.nb_tabs == 8
    assert len(sol_frame.all_tabs) == 8
    assert sol_frame.nb_filled_tabs == 8
    for count, tab in enumerate(sol_frame.all_tabs):
        assert sol_frame.tab(count, 'text') == tab.name

    model_solution = model.run()
    solutions = [model_solution]
    sol_frame.show_solution(solutions, Parameters(), ['params to print'],
                            datetime.datetime.today(), 5)
    assert sol_frame.nb_tabs == 7
    assert len(sol_frame.all_tabs) == 7
    assert sol_frame.nb_filled_tabs == 7
    for count, tab in enumerate(sol_frame.all_tabs):
        assert sol_frame.tab(count, 'text') == tab.name
コード例 #17
0
def test_auto_name_if_needed():
    params = Parameters()
    file_name = 'out.txt'
    params.update_parameter('OUTPUT_FILE', file_name)
    assert dea_utils.auto_name_if_needed(params, 'txt') == file_name
    params.update_parameter('OUTPUT_FILE', '')
    data_file_name = 'dataFileForAutoName.xls'
    params.update_parameter('DATA_FILE', data_file_name)
    assert dea_utils.auto_name_if_needed(
        params, 'csv') == 'dataFileForAutoName_result.csv'
    params.update_parameter('OUTPUT_FILE', 'auto')
    assert dea_utils.auto_name_if_needed(
        params, 'xls') == 'dataFileForAutoName_result.xls'
    with pytest.raises(ValueError) as excinfo:
        dea_utils.auto_name_if_needed(params, 'haha')
    assert str(excinfo.value) == 'haha is not supported output format'
コード例 #18
0
 def __init__(self, parent, current_categories, data_from_params_file,
              str_var_for_input_output_boxes, weights_status_str, *args,
              **kw):
     Notebook.__init__(self, parent, *args, **kw)
     self.parent = parent
     self.params = Parameters()
     self.current_categories = current_categories
     self.input_categories_frame = None
     self.output_categories_frame = None
     self.params_from_file_lbl = None
     self.data_from_params_file = data_from_params_file
     self.str_var_for_input_output_boxes = str_var_for_input_output_boxes
     self.weight_tab = None
     self.load_without_data = IntVar()
     self.options_frame = None
     self.create_widgets(weights_status_str)
コード例 #19
0
def test_on_save_solution_ok(solution_tab, model):
    model_solution = model.run()
    solutions = [model_solution]
    params = Parameters()
    param_strs = ['param_strs']
    run_date = datetime.datetime.today()
    solution_tab.show_solution(solutions, params, param_strs, run_date, 5)
    assert solution_tab.model_solutions == solutions
    assert solution_tab.params == params
    assert solution_tab.param_strs == param_strs
    assert solution_tab.run_date == run_date
    assert solution_tab.total_seconds == 5

    solution_tab.on_save_solution()
    assert os.path.isfile(solution_tab.ask_file_name_to_save(1))
    os.remove(solution_tab.ask_file_name_to_save(1))
コード例 #20
0
def params(request):
    params = Parameters()
    params.update_parameter('INPUT_CATEGORIES', 'I1; I2')
    params.update_parameter('OUTPUT_CATEGORIES', 'O1; O2')
    params.update_parameter('DEA_FORM', 'env')
    params.update_parameter('RETURN_TO_SCALE', 'CRS')
    params.update_parameter('ORIENTATION', 'input')
    params.update_parameter('NON_DISCRETIONARY_CATEGORIES', '')
    params.update_parameter('WEAKLY_DISPOSAL_CATEGORIES', '')
    params.update_parameter('USE_SUPER_EFFICIENCY', '')
    params.update_parameter('ABS_WEIGHT_RESTRICTIONS', '')
    params.update_parameter('VIRTUAL_WEIGHT_RESTRICTIONS', '')
    params.update_parameter('PRICE_RATIO_RESTRICTIONS', '')
    params.update_parameter('MAXIMIZE_SLACKS', '')
    params.update_parameter('MULTIPLIER_MODEL_TOLERANCE', '0')
    params.update_parameter('CATEGORICAL_CATEGORY', '')
    params.update_parameter('PEEL_THE_ONION', '')

    return params
コード例 #21
0
def test_price_ratio_restrictions_env_model(data):
    model = EnvelopmentModelBase(
        data,
        EnvelopmentModelInputOriented(
            generate_upper_bound_for_efficiency_score),
        DefaultConstraintCreator())

    bounds = {('I1', 'I2'): (None, 0.4), ('O1', 'O2'): (0.01, None)}
    model = EnvelopmentModelWithPriceRatioConstraints(model, bounds)
    start_time = datetime.datetime.now()
    model_solution = model.run()
    end_time = datetime.datetime.now()
    utils_for_tests.check_if_category_is_within_price_ratio_constraints(
        model_solution, bounds)

    work_book = Workbook()
    writer = XLSWriter(Parameters(), work_book, datetime.datetime.today(),
                       (end_time - start_time).total_seconds())
    writer.write_data(model_solution)
    work_book.save('tests/test_price_ratio_env_output.xls')
コード例 #22
0
def test_virual_restrictions_env_model(data):
    model = EnvelopmentModelBase(
        data,
        EnvelopmentModelInputOriented(
            generate_upper_bound_for_efficiency_score),
        DefaultConstraintCreator())

    bounds = {'I2': (0.01, 0.5)}
    model = EnvelopmentModelWithVirtualWeightRestrictions(model, bounds)
    start_time = datetime.datetime.now()
    model_solution = model.run()
    end_time = datetime.datetime.now()
    utils_for_tests.check_if_category_is_within_virtual_limits(
        model_solution, bounds)

    work_book = Workbook()
    writer = XLSWriter(Parameters(), work_book, datetime.datetime.today(),
                       (end_time - start_time).total_seconds())
    writer.write_data(model_solution)
    work_book.save('tests/test_virtual_constraints_env_output.xls')
コード例 #23
0
def test_super_efficiency_with_VRS(DEA_example2_data):
    data = DEA_example2_data
    data.add_input_category('I1')
    data.add_input_category('I2')
    data.add_input_category('I3')
    data.add_output_category('O1')
    data.add_output_category('O2')
    model = EnvelopmentModelBase(
        data,
        EnvelopmentModelInputOriented(generate_supper_efficiency_upper_bound),
        DefaultConstraintCreator())
    model = EnvelopmentModelVRSDecorator(model)
    super_efficiency_model = SupperEfficiencyModel(model)
    start_time = datetime.datetime.now()
    solution = super_efficiency_model.run()
    end_time = datetime.datetime.now()
    work_book = xlwt.Workbook()
    writer = XLSWriter(Parameters(), work_book, datetime.datetime.today(),
                       (end_time - start_time).total_seconds())
    writer.write_data(solution)
    work_book.save('tests/test_super_efficiency_with_VRS.xls')
コード例 #24
0
def test_peel_the_onion_CRS_multi_output_oriented(DEA_example2_data):
    model = _create_large_model_CRS_multi_output_oriented_with_non_discretionary(
        DEA_example2_data)
    start_time = datetime.datetime.now()
    solution, ranks, state = peel_the_onion_method(model)
    end_time = datetime.datetime.now()
    _check_large_model_CRS_multi_output_oriented_with_non_discretionary(
        solution, model.input_data)
    dmus = ['A', 'B', 'C', 'D', 'E', 'F', 'G', 'H', 'I', 'J', 'K']
    expected_ranks = [1, 3, 2, 1, 3, 1, 1, 2, 1, 2, 1]
    utils_for_tests.check_onion_ranks(
        model.input_data, dmus, expected_ranks, ranks)

    work_book = Workbook()
    ranks_as_list = []
    ranks_as_list.append(ranks)
    writer = XLSWriter(Parameters(), work_book, datetime.datetime.today(),
                       (end_time - start_time).total_seconds(),
                       ranks=ranks_as_list)
    writer.write_data(solution)
    work_book.save('tests/test_peel_the_onion.xls')
コード例 #25
0
def test_abs_restrictions_env_model_output(data):
    filename = 'tests/DEA_Harish_parameters.txt'
    params = parse_parameters_from_file(filename)
    categories, data, dmu_name, sheet_name = read_data(
        params.get_parameter_value('DATA_FILE'))
    coefficients, has_same_dmus = convert_to_dictionary(data)
    model_input = construct_input_data_instance(categories, coefficients)
    models, all_params = build_models(params, model_input)
    assert len(models) == 1 and len(all_params) == 1
    model = models[0]

    start_time = datetime.datetime.now()
    model_solution = model.run()
    end_time = datetime.datetime.now()
    bounds = {'Urban Roads (%)': (None, 0.003)}
    utils_for_tests.check_if_category_is_within_abs_limits(
        model_solution, bounds)

    work_book = Workbook()
    writer = XLSWriter(Parameters(), work_book, datetime.datetime.today(),
                       (end_time - start_time).total_seconds())
    writer.write_data(model_solution)
    work_book.save('tests/test_abs_constraints_env_outoriented_output.xls')
コード例 #26
0
def text_frame(request):
    categories = ['I1', 'I2', 'O1', 'O2']
    text_frame = TextForWeights(None, 'Absolute', 'Input <= 0.5', categories,
                                Parameters(), 'ABS_WEIGHT_RESTRICTIONS')
    request.addfinalizer(text_frame.destroy)
    return text_frame