def mzn_run(file_mzn, file_instance, data_forecasts, tmpdir, mzn_solver='mzn-g12mip', mzn_dir=None, print_output=False, verbose=0, check_win_hack=True): # ./instance2dzn.py ../smallinstances/demo_00/instance.txt # TODO: maybe this should (have) use(d) Instance() from checker... data = i2dzn.read_instance(file_instance) i2dzn.make_offset1(data) dzn_data = i2dzn.get_dzn(data) dzn_instance = join(tmpdir, "%s.dzn"%basename(file_instance)) with open(dzn_instance, 'w') as fout: fout.write(dzn_data) if verbose >= 2: print "Written dzn_instance to",dzn_instance # ./forecast2dzn.py -t 30 forecast.txt # starts from actual data dzn_data_forecasts = f2dzn.get_forecast_dzn(data_forecasts) dzn_forecast = join(tmpdir, "forecast.dzn") with open(dzn_forecast, 'w') as fout: fout.write(dzn_data_forecasts) if verbose >= 2: print "Written dzn_forecast to:", dzn_forecast # every more checks in case people don't set their path... env = os.environ.copy() if mzn_dir: env['PATH'] += os.pathsep + mzn_dir if not which('minizinc'): print "Error: '%s' not on PATH (nor in --mzn-dir)"%'minizinc' sys.exit(1) mzn_solver_bin = which(mzn_solver) if mzn_solver_bin == None: print "Error: '%s' not on PATH (nor in --mzn-dir)"%mzn_solver sys.exit(1) # Special hack for some windows users: if check_win_hack and os.name == "nt": if not mzn_solver_bin.endswith('.bat'): mzn_solver_bin += '.bat' # mzn-g12mip energy_noupdown.mzn ../smallinstances/demo_00/instance.dzn forecast.dzn > minizinc.out cmd = [mzn_solver_bin, file_mzn, dzn_instance, dzn_forecast] if verbose >= 1: print "Running:", " ".join(cmd) time_start = time.time() p = subprocess.Popen(cmd, stdout=subprocess.PIPE, stderr=subprocess.PIPE, env=env) out, err = p.communicate() timing = (time.time() - time_start) if print_output or verbose >= 1: print "Output: \"\"\"" print out print "\"\"\"" if err != None and err.strip() != "": print "Error running '%s':"%(' '.join(cmd)) print err else: out = out.split('\n') #print "done, ",[x for x in out if x.startswith('Cost=')] return (timing, out) return (timing, None)
def mzn_runcheck(file_mzn, file_instance, file_forecast, tmpdir, mzn_solver='mzn-g12mip', mzn_dir=None, print_output=False, pretty_print=False, print_header=True, verbose=0): # ./instance2dzn.py ../smallinstances/demo_00/instance.txt # TODO: maybe this should (have) use(d) Instance() from checker... data = i2dzn.read_instance(file_instance) i2dzn.make_offset1(data) dzn_data = i2dzn.get_dzn(data) dzn_instance = join(tmpdir, "%s.dzn" % basename(file_instance)) with open(dzn_instance, 'w') as fout: fout.write(dzn_data) if verbose >= 2: print "Written dzn_instance to", dzn_instance # ./forecast2dzn.py -t 30 forecast.txt timestep = data['time_step'] data_forecasts = f2dzn.read_forecast(file_forecast) data_forecasts = f2dzn.rescale(timestep, data_forecasts) dzn_data_forecasts = f2dzn.get_forecast_dzn(data_forecasts) dzn_forecast = join(tmpdir, "%s.dzn" % basename(file_forecast)) with open(dzn_forecast, 'w') as fout: fout.write(dzn_data_forecasts) if verbose >= 2: print "Written dzn_forecast to:", dzn_forecast # every more checks in case people don't set their path... env = os.environ.copy() if mzn_dir: env['PATH'] += os.pathsep + mzn_dir if not which('minizinc'): print "Error: '%s' not on PATH (nor in --mzn-dir)" % 'minizinc' sys.exit(1) mzn_solver_bin = which(mzn_solver) if mzn_solver_bin == None: print "Error: '%s' not on PATH (nor in --mzn-dir)" % mzn_solver sys.exit(1) # mzn-g12mip energy_noupdown.mzn ../smallinstances/demo_00/instance.dzn forecast.dzn > minizinc.out cmd = [mzn_solver_bin, file_mzn, dzn_instance, dzn_forecast] if verbose >= 1: print "Running:", " ".join(cmd) time_start = time.time() p = subprocess.Popen(cmd, stdout=subprocess.PIPE, stderr=subprocess.PIPE, env=env) out, err = p.communicate() time_stop = time.time() if print_output or verbose >= 1: print "Output: \"\"\"" print out print "\"\"\"" if err != None and err.strip() != "": print "Error running '%s':" % (' '.join(cmd)) print err else: out = out.split('\n') #print "done, ",[x for x in out if x.startswith('Cost=')] # ./checker_mzn.py ../smallinstances/demo_01 instance = Instance() # read standard instance and load forecast instance.read_instance(file_instance) instance.load_forecast(data_forecasts) # load minizinc solution from 'out' chkmzn.read_mznsolution(instance, out) if pretty_print or verbose >= 1: chkmzn.pretty_print(instance) instance.verify() errstr = instance.geterrorstring() if errstr: print "%s; %s; Error; Error trying to verify the instance: '%s'" % ( file_instance, file_forecast, errstr) print >> sys.stderr, errstr else: # csv print: timing = (time_stop - time_start) chkmzn.print_instance_csv(file_instance, file_forecast, instance, timing=timing, header=print_header)
def mzn_run(file_mzn, file_instance, data_forecasts, tmpdir, mzn_solver='mzn-g12mip', mzn_dir=None, print_output=False, verbose=0, check_win_hack=True): # ./instance2dzn.py ../smallinstances/demo_00/instance.txt # TODO: maybe this should (have) use(d) Instance() from checker... data = i2dzn.read_instance(file_instance) i2dzn.make_offset1(data) dzn_data = i2dzn.get_dzn(data) dzn_instance = join(tmpdir, "%s.dzn" % basename(file_instance)) with open(dzn_instance, 'w') as fout: fout.write(dzn_data) if verbose >= 2: print "Written dzn_instance to", dzn_instance # ./forecast2dzn.py -t 30 forecast.txt # starts from actual data dzn_data_forecasts = f2dzn.get_forecast_dzn(data_forecasts) dzn_forecast = join(tmpdir, "forecast.dzn") with open(dzn_forecast, 'w') as fout: fout.write(dzn_data_forecasts) if verbose >= 2: print "Written dzn_forecast to:", dzn_forecast # every more checks in case people don't set their path... env = os.environ.copy() if mzn_dir: env['PATH'] += os.pathsep + mzn_dir if not which('minizinc'): print "Error: '%s' not on PATH (nor in --mzn-dir)" % 'minizinc' sys.exit(1) mzn_solver_bin = which(mzn_solver) if mzn_solver_bin == None: print "Error: '%s' not on PATH (nor in --mzn-dir)" % mzn_solver sys.exit(1) # Special hack for some windows users: if check_win_hack and os.name == "nt": if not mzn_solver_bin.endswith('.bat'): mzn_solver_bin += '.bat' # mzn-g12mip energy_noupdown.mzn ../smallinstances/demo_00/instance.dzn forecast.dzn > minizinc.out cmd = [mzn_solver_bin, file_mzn, dzn_instance, dzn_forecast] if verbose >= 1: print "Running:", " ".join(cmd) time_start = time.time() p = subprocess.Popen(cmd, stdout=subprocess.PIPE, stderr=subprocess.PIPE, env=env) out, err = p.communicate() timing = (time.time() - time_start) if print_output or verbose >= 1: print "Output: \"\"\"" print out print "\"\"\"" if err != None and err.strip() != "": print "Error running '%s':" % (' '.join(cmd)) print err else: out = out.split('\n') #print "done, ",[x for x in out if x.startswith('Cost=')] return (timing, out) return (timing, None)
tmpdir = "" if args.tmp: tmpdir = args.tmp os.mkdir(args.tmp) else: tmpdir = tempfile.mkdtemp() # single or multiple instances f_instances = [args.file_instance] if os.path.isdir(args.file_instance): globpatt = os.path.join(args.file_instance, 'day*.txt') f_instances = sorted(glob.glob(globpatt)) # prep data_forecasts (same for all instances here) data_forecasts = f2dzn.read_forecast(args.file_forecast) timestep = i2dzn.read_instance(f_instances[0])['time_step'] data_forecasts = f2dzn.rescale(timestep, data_forecasts) # the actual stuff for (i, f) in enumerate(f_instances): (timing, out) = mzn_run(args.file_mzn, f, data_forecasts, tmpdir, mzn_dir=args.mzn_dir, mzn_solver=args.mzn_solver, print_output=args.print_output, verbose=args.v) instance = mzn_toInstance(f, out, data_forecasts,
def mzn_runcheck(file_mzn, file_instance, file_forecast, tmpdir, mzn_solver='mzn-g12mip', mzn_dir=None, print_output=False, pretty_print=False, print_header=True, verbose=0): # ./instance2dzn.py ../smallinstances/demo_00/instance.txt # TODO: maybe this should (have) use(d) Instance() from checker... data = i2dzn.read_instance(file_instance) i2dzn.make_offset1(data) dzn_data = i2dzn.get_dzn(data) dzn_instance = join(tmpdir, "%s.dzn"%basename(file_instance)) with open(dzn_instance, 'w') as fout: fout.write(dzn_data) if verbose >= 2: print "Written dzn_instance to",dzn_instance # ./forecast2dzn.py -t 30 forecast.txt timestep = data['time_step'] data_forecasts = f2dzn.read_forecast(file_forecast) data_forecasts = f2dzn.rescale(timestep, data_forecasts) dzn_data_forecasts = f2dzn.get_forecast_dzn(data_forecasts) dzn_forecast = join(tmpdir, "%s.dzn"%basename(file_forecast)) with open(dzn_forecast, 'w') as fout: fout.write(dzn_data_forecasts) if verbose >= 2: print "Written dzn_forecast to:", dzn_forecast # every more checks in case people don't set their path... env = os.environ.copy() if mzn_dir: env['PATH'] += os.pathsep + mzn_dir if not which('minizinc'): print "Error: '%s' not on PATH (nor in --mzn-dir)"%'minizinc' sys.exit(1) mzn_solver_bin = which(mzn_solver) if mzn_solver_bin == None: print "Error: '%s' not on PATH (nor in --mzn-dir)"%mzn_solver sys.exit(1) # mzn-g12mip energy_noupdown.mzn ../smallinstances/demo_00/instance.dzn forecast.dzn > minizinc.out cmd = [mzn_solver_bin, file_mzn, dzn_instance, dzn_forecast] if verbose >= 1: print "Running:", " ".join(cmd) time_start = time.time() p = subprocess.Popen(cmd, stdout=subprocess.PIPE, stderr=subprocess.PIPE, env=env) out, err = p.communicate() time_stop = time.time() if print_output or verbose >= 1: print "Output: \"\"\"" print out print "\"\"\"" if err != None and err.strip() != "": print "Error running '%s':"%(' '.join(cmd)) print err else: out = out.split('\n') #print "done, ",[x for x in out if x.startswith('Cost=')] # ./checker_mzn.py ../smallinstances/demo_01 instance = Instance() # read standard instance and load forecast instance.read_instance(file_instance) instance.load_forecast(data_forecasts) # load minizinc solution from 'out' chkmzn.read_mznsolution(instance, out) if pretty_print or verbose >= 1: chkmzn.pretty_print(instance) instance.verify() errstr = instance.geterrorstring() if errstr: print "%s; %s; Error; Error trying to verify the instance: '%s'"%(file_instance, file_forecast, errstr) print >> sys.stderr, errstr else: # csv print: timing = (time_stop - time_start) chkmzn.print_instance_csv(file_instance, file_forecast, instance, timing=timing, header=print_header)
tmpdir = "" if args.tmp: tmpdir = args.tmp os.mkdir(args.tmp) else: tmpdir = tempfile.mkdtemp() # single or multiple instances f_instances = [args.file_instance] if os.path.isdir(args.file_instance): globpatt = os.path.join(args.file_instance, 'day*.txt') f_instances = sorted(glob.glob(globpatt)) # prep data_forecasts (same for all instances here) data_forecasts = f2dzn.read_forecast(args.file_forecast) timestep = i2dzn.read_instance(f_instances[0])['time_step'] data_forecasts = f2dzn.rescale(timestep, data_forecasts) # the actual stuff for (i,f) in enumerate(f_instances): (timing, out) = mzn_run(args.file_mzn, f, data_forecasts, tmpdir, mzn_dir=args.mzn_dir, mzn_solver=args.mzn_solver, print_output=args.print_output, verbose=args.v) instance = mzn_toInstance(f, out, data_forecasts, pretty_print=args.print_pretty, verbose=args.v) # csv print: chkmzn.print_instance_csv(f, args.file_forecast, instance, timing=timing, header=(i==0))