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 = 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_all_constraints_multi_output.xls')
def test_price_ratio_restrictions_medium_env_model():
    categories, data, dmu_name, sheet_name = read_data(
        'tests/dataFromDEAbook_page181.xls')
    coefficients, has_same_dmus = convert_to_dictionary(data)
    assert has_same_dmus is False
    assert validate_data(categories, coefficients) is True
    data = construct_input_data_instance(categories, coefficients)
    print(data.categories)
    data.add_input_category('Doctors')
    data.add_input_category('Nurses')
    data.add_output_category('Outpatients')
    data.add_output_category('Inpatients')

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

    ratio_bounds = {('Nurses', 'Doctors'): (0.2, 5),
                    ('Inpatients', 'Outpatients'): (0.2, 5)}
    model = MultiplierModelWithPriceRatioConstraints(model, ratio_bounds)

    model_solution = model.run()
    utils_for_tests.check_if_category_is_within_price_ratio_constraints(
        model_solution, ratio_bounds)

    utils_for_tests.check_optimal_solution_status_and_sizes(
        model_solution, data)
    dmus = ['H1', 'H2', 'H3', 'H4', 'H5', 'H6', 'H7',
            'H8', 'H9', 'H10', 'H11', 'H12', 'H13', 'H14']
    utils_for_tests.check_efficiency_scores(dmus, [0.926, 1, 1, 0.634, 0.82, 1,
                                                   0.803, 0.872, 0.982, 1,
                                                   0.849,
                                                   0.93, 0.74, 0.929],
                                            model_solution, data, 1e-3)
    clean_up_pickled_files()
def test_with_zero_data():
    categories, xls_data, dmu_name, sheet_name = read_data(
        'tests/with_zeros.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('rein')
    data.add_output_category('aus')
    model = MultiplierModelBase(data, 0, MultiplierOutputOrientedModel())
    model_solution = model.run()
    clean_up_pickled_files()
def test_with_zero_data():
    categories, xls_data, dmu_name, sheet_name = read_data(
        'tests/with_zeros.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('rein')
    data.add_output_category('aus')
    model = MultiplierModelBase(data, 0,
                                MultiplierOutputOrientedModel())
    model_solution = model.run()
    clean_up_pickled_files()
def test_VRS_multi_output_oriented_with_weakly_disposable_vars_small(data):
    model = MultiplierModelVRSDecorator(MultiplierModelWithDisposableCategories(
        MultiplierModelBase(data, 1e-12,
                            MultiplierOutputOrientedModel()), set(['q'])))
    model_solution = model.run()
    utils_for_tests.check_optimal_solution_status_and_sizes(
        model_solution, data)
    dmus = ['A', 'B', 'C', 'D', 'E']
    utils_for_tests.check_efficiency_scores(
        dmus, [0.5, 1, 1, 1, 1], model_solution, data)

    utils_for_tests.check_lambda_variables('A', 'B', 1, model_solution, data)
    utils_for_tests.check_lambda_variables('B', 'B', 1, model_solution, data)
    utils_for_tests.check_lambda_variables('C', 'C', 1, model_solution, data)
    utils_for_tests.check_lambda_variables('E', 'E', 1, model_solution, data)
    utils_for_tests.check_lambda_variables('D', 'D', 1, model_solution, data)

    utils_for_tests.check_categories_for_dmu(
        'A', ['x1', 'x2', 'q'], [0.25, 0, 1], model_solution, data)
    utils_for_tests.check_categories_for_dmu(
        'B', ['x1', 'x2', 'q'], [0.125, 0, 0.5], model_solution, data)
    utils_for_tests.check_categories_for_dmu(
        'C', ['x1', 'x2', 'q'], [0.083333333, 0, 0.33333333], model_solution,
        data)
    utils_for_tests.check_categories_for_dmu(
        'D', ['x1', 'x2', 'q'], [0.33333333, 0.66666667, 1], model_solution,
        data)
    utils_for_tests.check_categories_for_dmu(
        'E', ['x1', 'x2', 'q'], [0, 0.125, 0.5], model_solution, data)

    utils_for_tests.check_VRS_duals(
        dmus, [1.5, 0.75, 0.5, -1.3333333, 0.75], model_solution, data)
 def get_basic_model(cls, model_input, concrete_model,
                     weakly_disposal_categories, params):
     ''' See base class.
     '''
     tolerance = float(
         params.get_parameter_value('MULTIPLIER_MODEL_TOLERANCE'))
     return MultiplierModelBase(model_input, tolerance, concrete_model)
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')
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')
def test_CRS_multi_output_oriented_with_weakly_disposable_vars_small(data):
    model = MultiplierModelWithDisposableCategories(
        MultiplierModelBase(data, 1e-12, MultiplierOutputOrientedModel()),
        set(['x1']))
    model_solution = model.run()
    utils_for_tests.check_optimal_solution_status_and_sizes(
        model_solution, data)
    dmus = ['A', 'B', 'C', 'D', 'E']
    utils_for_tests.check_efficiency_scores(
        dmus, [0.5, 1, 0.8333333458, 0.7142857143, 1], model_solution, data)

    utils_for_tests.check_lambda_variables('A', 'B', 1, model_solution, data)
    utils_for_tests.check_lambda_variables('B', 'B', 1, model_solution, data)
    utils_for_tests.check_lambda_variables('C', 'B', 1.2, model_solution, data)
    utils_for_tests.check_lambda_variables('C', 'E', 0.6, model_solution, data)
    utils_for_tests.check_lambda_variables('D', 'B', 0.3, model_solution, data)
    utils_for_tests.check_lambda_variables('D', 'E', 0.4, model_solution, data)
    utils_for_tests.check_lambda_variables('E', 'E', 1, model_solution, data)

    utils_for_tests.check_categories_for_dmu('A', ['x1', 'x2', 'q'], [1, 0, 1],
                                             model_solution, data)
    utils_for_tests.check_categories_for_dmu('B', ['x1', 'x2', 'q'],
                                             [0.5, 0, 0.5], model_solution,
                                             data)
    utils_for_tests.check_categories_for_dmu(
        'C', ['x1', 'x2', 'q'], [0.066666667, 0.13333333, 0.33333333],
        model_solution, data)
    utils_for_tests.check_categories_for_dmu('D', ['x1', 'x2', 'q'],
                                             [0.2, 0.4, 1], model_solution,
                                             data)
    utils_for_tests.check_categories_for_dmu('E', ['x1', 'x2', 'q'],
                                             [0.1, 0.2, 0.5], model_solution,
                                             data)
def test_run_with_categorical_dmus_invalid_data(categorical_from_book):
    data = categorical_from_book
    model = MultiplierModelBase(data, 0,
                                MultiplierInputOrientedModel())
    with pytest.raises(ValueError) as excinfo:
        categorical_model = ModelWithCategoricalDMUs(model, 'Non-existant')
    assert str(excinfo.value) == 'Category <Non-existant> does not exist'
def test_price_ratio_multiplier_model(data):

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

    bounds = {('I1', 'I2'): (None, 0.4), ('O1', 'O2'): (0.01, None)}
    model = MultiplierModelWithPriceRatioConstraints(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_multi_output.xls')
def test_virtual_weight_restrictions_multiplier_model(data):

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

    bounds = {'I1': (None, 0.5)}
    model = MultiplierModelWithVirtualWeightRestrictions(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 = 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_virtual_weights_multi_output.xls')
def test_CRS_multi_output_oriented_with_non_discretionary_vars_with_error(
        data):
    with pytest.raises(ValueError) as excinfo:
        model = MultiplierModelOutputOrientedWithNonDiscVars(
            MultiplierModelBase(data, 1e-12, MultiplierOutputOrientedModel()),
            set(['q']))

    assert str(
        excinfo.value) == ('Too many non-discretionary categories. At least'
                           ' one output must be discretionary')
def _create_large_model_CRS_multi_output_oriented_with_non_discretionary(
        DEA_example2_data):
    DEA_example2_data.add_input_category('I1')
    DEA_example2_data.add_input_category('I2')
    DEA_example2_data.add_input_category('I3')
    DEA_example2_data.add_output_category('O1')
    DEA_example2_data.add_output_category('O2')
    model = MultiplierModelOutputOrientedWithNonDiscVars(
        MultiplierModelBase(DEA_example2_data, 1e-12,
                            MultiplierOutputOrientedModel()), set(['O1']))
    return model
def test_price_ratio_restrictions_medium_env_model():
    categories, data, dmu_name, sheet_name = read_data(
        'tests/dataFromDEAbook_page181.xls')
    coefficients, has_same_dmus = convert_to_dictionary(data)
    assert has_same_dmus is False
    assert validate_data(categories, coefficients) is True
    data = construct_input_data_instance(categories, coefficients)
    print(data.categories)
    data.add_input_category('Doctors')
    data.add_input_category('Nurses')
    data.add_output_category('Outpatients')
    data.add_output_category('Inpatients')

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

    ratio_bounds = {
        ('Nurses', 'Doctors'): (0.2, 5),
        ('Inpatients', 'Outpatients'): (0.2, 5)
    }
    model = MultiplierModelWithPriceRatioConstraints(model, ratio_bounds)

    model_solution = model.run()
    utils_for_tests.check_if_category_is_within_price_ratio_constraints(
        model_solution, ratio_bounds)

    utils_for_tests.check_optimal_solution_status_and_sizes(
        model_solution, data)
    dmus = [
        'H1', 'H2', 'H3', 'H4', 'H5', 'H6', 'H7', 'H8', 'H9', 'H10', 'H11',
        'H12', 'H13', 'H14'
    ]
    utils_for_tests.check_efficiency_scores(dmus, [
        0.926, 1, 1, 0.634, 0.82, 1, 0.803, 0.872, 0.982, 1, 0.849, 0.93, 0.74,
        0.929
    ], model_solution, data, 1e-3)
    clean_up_pickled_files()
Exemple #16
0
def test_peel_the_onion_VRS_multi(categorical_data):
    categorical_data.add_input_category('rein')
    categorical_data.add_output_category('aus')
    model = MultiplierModelVRSDecorator(
        MultiplierModelBase(categorical_data, 0,
                            MultiplierInputOrientedModel()))

    solution, ranks, state = peel_the_onion_method(model)
    utils_for_tests.check_optimal_solution_status_and_sizes(
        solution, categorical_data)
    dmus = ['B', 'C', 'D', 'E', 'F', 'G']
    utils_for_tests.check_efficiency_scores(dmus, [1, 0.5, 1, 0.99999998,
                                                   0.75, 0.50000001],
                                            solution, categorical_data, 1e-7)

    expected_ranks = [1, 2, 1, 1, 2, 2]
    utils_for_tests.check_onion_ranks(
        model.input_data, dmus, expected_ranks, ranks)
def test_CRS_multi_input_oriented_with_non_discretionary_vars(data):
    model = MultiplierModelInputOrientedWithNonDiscVars(
        MultiplierModelBase(data, 1e-12, MultiplierInputOrientedModel()),
        set(['x1']))

    model_solution = model.run()
    utils_for_tests.check_optimal_solution_status_and_sizes(
        model_solution, data)
    dmus = ['A', 'B', 'C', 'D', 'E']
    utils_for_tests.check_efficiency_scores(dmus,
                                            [0.3, 1, 0.750000012, 0.5, 1],
                                            model_solution, data)

    utils_for_tests.check_lambda_variables('A', 'B', 0.25, model_solution,
                                           data)
    utils_for_tests.check_lambda_variables('A', 'E', 0.25, model_solution,
                                           data)
    utils_for_tests.check_lambda_variables('B', 'B', 1, model_solution, data)
    utils_for_tests.check_lambda_variables('C', 'B', 0.75, model_solution,
                                           data)
    utils_for_tests.check_lambda_variables('C', 'E', 0.75, model_solution,
                                           data)
    utils_for_tests.check_lambda_variables('D', 'E', 0.5, model_solution, data)
    utils_for_tests.check_lambda_variables('E', 'E', 1, model_solution, data)

    utils_for_tests.check_categories_for_dmu('A', ['x1', 'x2', 'q'],
                                             [0.1, 0.2, 0.5], model_solution,
                                             data)
    utils_for_tests.check_categories_for_dmu('B', ['x1', 'x2', 'q'],
                                             [0.125, 0.25, 0.625],
                                             model_solution, data)
    utils_for_tests.check_categories_for_dmu(
        'C', ['x1', 'x2', 'q'], [0.083333333, 0.16666667, 0.41666667],
        model_solution, data)
    utils_for_tests.check_categories_for_dmu('D', ['x1', 'x2', 'q'],
                                             [0, 0.5, 0.5], model_solution,
                                             data)
    utils_for_tests.check_categories_for_dmu('E', ['x1', 'x2', 'q'],
                                             [0, 0.5, 0.5], model_solution,
                                             data)
def test_CRS_multi_input_oriented_with_weakly_disposable_vars_small(data):
    model = MultiplierModelWithDisposableCategories(
        MultiplierModelBase(data, 1e-12, MultiplierInputOrientedModel()),
        ['x1', 'q'])
    model_solution = model.run()
    utils_for_tests.check_optimal_solution_status_and_sizes(
        model_solution, data)
    dmus = ['A', 'B', 'C', 'D', 'E']
    utils_for_tests.check_efficiency_scores(
        dmus, [0.5, 1, 0.83333334, 0.71428571, 1], model_solution, data)

    utils_for_tests.check_lambda_variables('A', 'B', 0.5, model_solution, data)
    utils_for_tests.check_lambda_variables('B', 'B', 1, model_solution, data)
    utils_for_tests.check_lambda_variables('C', 'B', 1, model_solution, data)
    utils_for_tests.check_lambda_variables('C', 'E', 0.5, model_solution, data)
    utils_for_tests.check_lambda_variables('D', 'B', 0.21428571,
                                           model_solution, data)
    utils_for_tests.check_lambda_variables('D', 'E', 0.28571429,
                                           model_solution, data)
    utils_for_tests.check_lambda_variables('E', 'E', 1, model_solution, data)

    utils_for_tests.check_categories_for_dmu('A', ['x1', 'x2', 'q'],
                                             [0.5, 0, 0.5], model_solution,
                                             data)
    utils_for_tests.check_categories_for_dmu('B', ['x1', 'x2', 'q'],
                                             [0.5, 0, 0.5], model_solution,
                                             data)
    utils_for_tests.check_categories_for_dmu(
        'C', ['x1', 'x2', 'q'], [0.055555556, 0.11111111, 0.27777778],
        model_solution, data)
    utils_for_tests.check_categories_for_dmu(
        'D', ['x1', 'x2', 'q'], [0.14285714, 0.28571429, 0.71428571],
        model_solution, data)
    utils_for_tests.check_categories_for_dmu('E', ['x1', 'x2', 'q'],
                                             [0.1, 0.2, 0.5], model_solution,
                                             data)
def model(request, data):
    model = MultiplierModelBase(data, 1e-12, MultiplierOutputOrientedModel())
    return model
def model(request, data):
    model = MultiplierModelVRSDecorator(
        MultiplierModelBase(data, 0, MultiplierInputOrientedModel()))
    return model