def create_output(self): dict_of_variables = { 'production_variables': self.production_variables, 'inventory_variables': self.inventory_variables } output_df = write_outputs(dict_of_variables, attr='x') write_to_csv(output_df)
# ================== Optimization ================== if model_params['write_lp']: logger.info('Writing the lp file!') model.export_as_lp('./{}.lp'.format(model.name)) logger.info('Optimization starts!') # If CPLEX is installed locally, we can use that to solve the problem. # Otherwise, we can use DOcplexcloud. For docloud solve, we need valid 'url' and 'key'. # Note, that if 'url' and 'key' parameters are present, # the solve will be started on DOcplexcloud even if CPLEX is available. # I've provided more info on this in optimization_model_docplex.py # For now, a simple way to handle local or docloud solve if model_params['cplex_cloud']: model.solve(url=model_params['url'], key=model_params['api_key']) else: model.solve() if model.solve_details.status == 'optimal': logger.info('The solution is optimal and the objective value ' 'is ${:,.2f}!'.format(model.objective_value)) # ================== Output ================== dict_of_variables = {'production_variables': production_variables, 'inventory_variables': inventory_variables} output_df = process_data.write_outputs(dict_of_variables, attr='solution_value') helper.write_to_csv(output_df) logger.info('Outputs are written to csv!')
total_production_cost = grb.quicksum(row['production_cost'] * production_variables[index] for index, row in input_df_dict['input_data'].iterrows()) objective = total_holding_cost + total_production_cost model.setObjective(objective, grb.GRB.MINIMIZE) logger.info('Model creation time in sec: {:.4f}'.format(time() - start)) # ================== Optimization ================== if model_params['write_lp']: logger.info('Writing the lp file!') model.write(model.ModelName + '.lp') if not model_params['write_log']: model.setParam('OutputFlag', 0) logger.info('Optimization starts!') model.optimize() if model.Status == grb.GRB.OPTIMAL: logger.info('The solution is optimal and the objective value ' 'is ${:,.2f}!'.format(model.objVal)) # ================== Output ================== dict_of_variables = {'production_variables': production_variables, 'inventory_variables': inventory_variables} output_df = process_data.write_outputs(dict_of_variables, attr='x') helper.write_to_csv(output_df) logger.info('Outputs are written to csv!')