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_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()
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')
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')
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_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')
def post_process_solutions(self, solutions, params, param_strs, all_ranks, run_date, total_seconds): ''' See base class. ''' output_file = auto_name_if_needed(self.params, self.output_format, self.output_dir) if output_file: categorical = self.params.get_parameter_value( 'CATEGORICAL_CATEGORY') if not categorical.strip(): categorical = None if output_file.endswith('.xls'): work_book = xlwt.Workbook() elif output_file.endswith('.xlsx'): work_book = XlsxWorkbook() elif output_file.endswith('.csv'): work_book = TxtWriter(os.path.splitext(output_file)[0]) else: raise ValueError( 'File {0} has unsupported output format'.format( output_file)) writer = XLSWriter(self.params, work_book, run_date, total_seconds, ranks=all_ranks, categorical=categorical) try: for count, sol in enumerate(solutions): writer.write_data(sol, param_strs[count]) work_book.save(output_file) except ValueError: work_book = TxtWriter(os.path.splitext(output_file)[0]) writer = XLSWriter(self.params, work_book, run_date, total_seconds, ranks=all_ranks, categorical=categorical) for count, sol in enumerate(solutions): writer.write_data(sol, param_strs[count]) work_book.save(output_file)
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')
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')
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')
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')
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')
def on_save_solution(self): ''' Saves solution to file. This method is called when save solution button is pressed. If there is a solution, this method will ask user to provide a file name where solution should be stored. If a valid name is provided, solution is saved to that file. Allowed file formats are: .xls and .xlsx. Default file extension is .xlsx. If the user checked 'csv' as solution output format, then the user will be asked to choose a directory where all csv files will be written. ''' if self.model_solutions is not None: assert (self.param_strs is not None) if (self.solution_format_var.get() == 1 or self.solution_format_var.get() == 2): file_name = self.ask_file_name_to_save( self.solution_format_var.get()) dir_name = '' else: dir_name = askdirectory() file_name = '' if file_name or dir_name: print(file_name) self.status_lbl.config(text='Saving solution to file...') if file_name.endswith('.xls'): work_book = xlwt.Workbook() elif file_name.endswith('.xlsx'): work_book = XlsxWorkbook() else: # all not supported formats will be written to csv assert (dir_name) work_book = TxtWriter(dir_name) writer = XLSWriter(self.params, work_book, self.run_date, self.total_seconds, ranks=self.ranks, categorical=self.categorical) nb_models = len(self.model_solutions) # +1 for parameters sheet, it is stored separately nb_sheets = len(writer.worksheets) + 1 progress_recorder = GuiProgress(self.progress_bar, nb_models, nb_sheets) try: for count, sol in enumerate(self.model_solutions): writer.write_data(sol, self.param_strs[count], progress_recorder) work_book.save(file_name) except ValueError: # can happen if maximum number of rows is exceeded self.status_lbl.config( text='File is too large for xls format,' ' it will be saved to csv instead') work_book = TxtWriter(os.path.splitext(file_name)[0]) writer = XLSWriter(self.params, work_book, self.run_date, self.total_seconds, ranks=self.ranks, categorical=self.categorical) progress_recorder.set_position(0) for count, sol in enumerate(self.model_solutions): writer.write_data(sol, self.param_strs[count], progress_recorder) work_book.save(file_name) progress_recorder.set_position(100) self.parent.change_solution_tab_name('Solution') self.status_lbl.config(text='Solution saved')