def check_environment(): from docplex.mp.environment import Environment env = Environment() if not env.has_cplex: print("CPLEX not installed.") else: print("CPLEX is installed.\n") env.print_information()
def main(instances, verbose=False): env = Environment() env.print_information() dir_path = os.path.dirname(os.path.realpath(__file__)) solutions = [] for index, instance in enumerate(instances): data, h, w, problem_class = instance data = sorted(data, key=itemgetter(0), reverse=True) try: mdl = build_model(data, w, h) if verbose: print(mdl.export_to_string()) mdl.print_information() mdl.export_as_lp(dir_path + "/" + mdl.name + "_" + str(index) + '.lp') if mdl.solve(log_output=verbose): if verbose: mdl.float_precision = 3 print("* model solved as function:") mdl.print_solution() mdl.report_kpis() # Save the CPLEX solution as "solution.json" program output with get_environment().get_output_stream( dir_path + "/" + mdl.name + "_" + str(index) + "_solution.json") as fp: mdl.solution.export(fp, "json") else: print("* model has no solution") except Exception as e: # Problem with more than 1000 variables print(e)
#dd-markdown #dd-markdown Minimum demand is the product of duration (in hours) by the minimum required number of nurses. Thus, in number of #dd-markdown nurse-hours, this demand is stored in another new column `min_demand`. #dd-markdown #dd-markdown Finally, we display the updated shifts DataFrame with all calculated columns. #dd-cell # also compute minimum demand in nurse-hours df_shifts["min_demand"] = df_shifts.min_req * df_shifts.duration # finally check the modified shifts dataframe df_shifts #dd-markdown ### Step 5: Set up the prescriptive model #dd-cell from docplex.mp.environment import Environment env = Environment() env.print_information() #dd-markdown #### Create the DOcplex model #dd-markdown The model contains all the business constraints and defines the objective. #dd-markdown #dd-markdown We now use CPLEX Modeling for Python to build a Mixed Integer Programming (MIP) model for this problem. #dd-cell from docplex.mp.model import Model mdl = Model(name="nurses") #dd-markdown #### Define the decision variables #dd-markdown #dd-markdown For each (nurse, shift) pair, we create one binary variable that is equal to 1 when the nurse is assigned to the shift. #dd-markdown #dd-markdown We use the `binary_var_matrix` method of class `Model`, as each binary variable is indexed by _two_ objects: one nurse and one shift. #dd-cell # first global collections to iterate upon all_nurses = df_nurses.index.values
help='Try and find the "best" solution, rather than ' 'just a feasible solution. Pass "min" to find ' 'the least best solution.') parser.add_argument('-C', '--check', action='store_true', help='Check the result against the (forward) cascade.') parser.add_argument('-l', '--lp-path',type=str, default=None, help='File to write LP constraints to.') args = parser.parse_args() print "Loading cascade..." cascade = Cascade.load(args.cascade) docloud_context = DOcloudContext.make_default_context(DOCLOUD_URL) docloud_context.print_information() env = Environment() env.print_information() print "Solving..." im = inverse_haar(cascade, min_optimize=(args.optimize == "min"), max_optimize=(args.optimize == "max"), time_limit=args.time_limit, docloud_context=docloud_context, lp_path=args.lp_path) cv2.imwrite(args.output, im) print "Wrote {}".format(args.output) if args.check: print "Checking..." ret = cascade.detect(im)