def test_famer_netx(self): """ solve the ef and check some post solution code""" shutil.copyfile(self.farmpath + os.sep + "concreteNetX" +\ os.sep + "ReferenceModel.py", self.tdir + os.sep + "ReferenceModel.py") import ReferenceModel as RM g = RM.pysp_scenario_tree_model_callback() stsolver = rapper.StochSolver("ReferenceModel", fsfct = "pysp_instance_creation_callback", tree_model = g) ef_sol = stsolver.solve_ef(solvername) assert(ef_sol.solver.termination_condition \ == pyo.TerminationCondition.optimal) obj = stsolver.root_E_obj() assert(abs(-108385 - obj) < 100) # any solver should get this close
def test_famer_netx(self): """ solve the ef and check some post solution code""" shutil.copyfile(self.farmpath + os.sep + "concreteNetX" +\ os.sep + "ReferenceModel.py", self.tdir + os.sep + "ReferenceModel.py") import ReferenceModel as RM g = RM.pysp_scenario_tree_model_callback() stsolver = rapper.StochSolver("ReferenceModel", fsfct="pysp_instance_creation_callback", tree_model=g) ef_sol = stsolver.solve_ef(solvername) assert(ef_sol.solver.termination_condition \ == pyo.TerminationCondition.optimal) obj = stsolver.root_E_obj() assert (abs(-108385 - obj) < 100) # any solver should get this close
alt_args = [ dict(inputs_dir="inputs", time_sample="rps_mini"), dict(inputs_dir="inputs_tiny", time_sample="tiny"), dict(inputs_dir="inputs_tiny_tiny", time_sample="tiny", exclude_technologies=args["exclude_technologies"] + ('CentralTrackingPV', 'Wind')), ] for a in alt_args: # clone the arguments dictionary and update it with settings from the alt_args entry, if any active_args = dict(args.items() + a.items()) scenario_data.write_tables(**active_args) if cmd_line_args['write_rhos']: # calculate and store the per-variable rho values print "creating rho settings files (takes several minutes)..." import ReferenceModel inputs_dirs = set() inputs_dirs.add(args["inputs_dir"]) for a in alt_args: inputs_dirs.add(a["inputs_dir"]) for inputs_dir in inputs_dirs: ReferenceModel.inputs_dir = inputs_dir ReferenceModel.create_model() ReferenceModel.load_inputs() ReferenceModel.save_rho_file() else: print "SKIPPING rho settings files." print "Use --write_rhos flag to write the rho settings files."
def main(**kwargs): """iou - Find optimal transactions.""" color = kwargs['color'] try: if not any([kwargs[key] for key in ['data']]): help_str = "{}".format(click.get_current_context().get_help()) click.secho(help_str) click.get_current_context().exit() def show_item(item): if item is not None: return item click.echo("") click.secho("Find the Optimal Number of Transactions:", fg=color, bold=True) click.echo("") with click.progressbar( ('Getting data', 'Creating model', 'Solving', 'Finished'), label='iou:', item_show_func=show_item) as bar: for item in bar: if item == 'Getting data': path_to_file = kwargs['data'] array, people = data.get_transactions_from_file(path_to_file) net_value = data.compress_edges(data.create_graph(array, people)) elif item == 'Creating model': model = ReferenceModel.create_model(net_value) elif item == 'Solving': ReferenceModel.solve(model) if kwargs['verbose']: color = kwargs['verbose_color'] click.echo("") for row_number, row in enumerate(array): number_of_contributors = sum([float(item) for item in row[3:]]) click.secho("{number_of_contributors}".format(number_of_contributors=number_of_contributors), bold=True, fg=color, nl=False) click.secho(" people owe ", nl=False) click.secho("{person}".format(person=row[2]), bold=True, fg=color, nl=False) click.secho(" ", nl=False) click.secho("{amount}".format(amount=row[1]), bold=True, fg=color, nl=False) click.secho(" for ", nl=False) click.secho("{expense}".format(expense=row[0]), bold=True, fg=color) click.echo("") for person, value in net_value.items(): if value > 0: click.secho("{person}".format( person=person), bold=True, fg=color, nl=False) click.secho("'s net spend =", nl=False) click.secho(" {value}".format(value=value), bold=True, fg=color) click.echo("") for person, value in net_value.items(): if value < 0: click.secho("{person}".format( person=person), bold=True, fg=color, nl=False) click.secho("'s net spend =", nl=False) click.secho(" {value}".format(value=value), bold=True, fg=color) click.echo("") color = kwargs['color'] click.secho("iou results:") click.echo("") for i in model.Amount: if model.Amount[i].value == 0: pass else: click.secho("{}".format(i[0]), fg=color, bold=True, nl=False) click.secho(" owes", nl=False) click.secho(" {}".format(i[1]), fg=color, bold=True, nl=False) click.secho(" a total of ", nl=False) click.secho("${0:.2f}".format(model.Amount[i].value), fg=color, bold=True) click.echo("") except Exception as e: click.echo('') raise click.ClickException("{}\n\nCheck the help (--help) on how to use iou or contact the developer.".format( e.message))
instance.preprocess() _results = solver.solve(instance, suffixes=['dual']) instance.solutions.store_to(_results) return(_results) def display_results(_results, model): """Display results from results.json and model.""" print("Optimal solution:\n") for website in sorted(model.Websites): for item in sorted(model.Items): if model.Quantity[website, item].value > 0: print("Buy {q} item(s) of {i} from {w}".format(q=int(model.Quantity[website, item].value), i=item, w=website,)) print('') print("Shipping Cost = {}".format(model.Cost['Shipping'].value)) print("Product Cost = {}".format(model.Cost['Item'].value)) print('') for i in _results['Solution']: print("Total Cost = {}".format(i['Objective']['Objective']['Value'])) if __name__ == '__main__': from data import price, quantity, shipping model = ReferenceModel.create_model(price, quantity, shipping) display_results(solve_instance(model), model)
def main(**kwargs): """'Find Optimal Number of Orders' aka fono.""" color = kwargs.pop('color') fono_color = kwargs.pop('fono_color') try: if not any([kwargs[key] for key in kwargs]): help_str = "{}".format(click.get_current_context().get_help()) click.secho(help_str) click.get_current_context().exit() def show_item(item): if item is not None: return item click.echo("") click.secho("Find the Optimal Number of Orders:", fg=fono_color, bold=True) click.echo("") with click.progressbar( ('Getting data', 'Creating model', 'Solving', 'Finished'), label='fono:', item_show_func=show_item) as bar: for item in bar: if item == 'Getting data': if kwargs['folder']: price, quantity, shipping = data.get_input(kwargs['folder']) elif kwargs['quantity'] and kwargs['price'] and kwargs['shipping']: quantity = data.get_quantity(kwargs['quantity']) price = data.get_price(kwargs['price']) shipping = data.get_shipping(kwargs['shipping']) elif item == 'Creating model': model = ReferenceModel.create_model(price, quantity, shipping) elif item == 'Solving': mipgap = kwargs.get('mipgap') solve.solve_instance(model, mipgap=mipgap), model # solve.display_results(solve.solve_instance(model), model) click.echo("") click.secho("fono results:", fg=fono_color, bold=True) for website in sorted(model.Websites): click.secho("") click.secho("{}".format(website), fg=color, bold=True, nl=False) click.secho(":") for item in sorted(model.Items): if model.Quantity[website, item].value > 0: click.echo("Buy ", nl=False) click.secho("{} ".format(int(model.Quantity[website, item].value)), fg=color, bold=True, nl=False) click.echo("item(s) of ", nl=False) click.secho("{} ".format(item), fg=color, bold=True, nl=False) click.echo("for a total of ", nl=False) click.secho("{} ".format(price[(website, item)] * model.Quantity[website, item].value), fg=color, bold=True, nl=False) click.echo("dollars", nl=False) click.secho(".") click.echo("") item_costs = model.Cost['Item'].value shipping_costs = model.Cost['Shipping'].value total_costs = item_costs + shipping_costs click.secho("Total product costs = {} dollars".format(item_costs), bold=True) click.secho("Total shipping costs = {} dollars".format(shipping_costs), bold=True) click.echo("") click.secho("Total costs = {} dollars".format(total_costs), fg=fono_color, bold=True) click.echo("") except Exception as e: click.echo('') raise click.ClickException("{}\n\nCheck the help (--help) on how to use fono or contact the developer.".format( e.message))
dict(inputs_dir="inputs_tiny", time_sample="tiny"), dict( inputs_dir="inputs_tiny_tiny", time_sample="tiny", exclude_technologies=args["exclude_technologies"] + ("CentralTrackingPV", "Wind"), ), ] for a in alt_args: # clone the arguments dictionary and update it with settings from the alt_args entry, if any active_args = dict(args.items() + a.items()) scenario_data.write_tables(**active_args) if cmd_line_args["write_rhos"]: # calculate and store the per-variable rho values print "creating rho settings files (takes several minutes)..." import ReferenceModel inputs_dirs = set() inputs_dirs.add(args["inputs_dir"]) for a in alt_args: inputs_dirs.add(a["inputs_dir"]) for inputs_dir in inputs_dirs: ReferenceModel.inputs_dir = inputs_dir ReferenceModel.create_model() ReferenceModel.load_inputs() ReferenceModel.save_rho_file() else: print "SKIPPING rho settings files." print "Use --write_rhos flag to write the rho settings files."