示例#1
0
def solve(ca):
    n, k = map(int, raw_input().split())
    problem = pulp.LpProblem('pname', pulp.LpMinimize)

    sumi = map(int, raw_input().split())

    xi = [pulp.LpVariable('x%d' % i, -MAX, MAX, 'Integer') for i in range(n)]
    #linex = [pulp.LpVariable('li%d'%i, 0, 1, 'Binary') for i in range(n)]
    maxx = pulp.LpVariable('maxx', -MAX, MAX, 'Integer')
    minx = pulp.LpVariable('minx', -MAX, MAX, 'Integer')
    problem += pulp.lpSum([xi[i] for i in range(k)]) == sumi[0]

    for i in range(n - k):
        problem += sumi[i] + xi[i + k] == sumi[i + 1] + xi[i]
    for i in range(n):
        problem += xi[i] <= maxx
        problem += xi[i] >= minx

    problem += maxx - minx

    #print problem
    #status = problem.solve(pulp.GLPK())
    #status = problem.solve(pulp.COIN(msg = True))
    status = problem.solve(pulp.COIN())
    #status = problem.solve()

    ans = maxx.value() - minx.value()

    # for i in linex:
    #     print i, i.value(),
    # print
    print "Case #%d: %.0f" % (ca, ans)
def perform_experiment_phase_II(infrastructure_file,
                                workload_file,
                                year,
                                phase_I_solution,
                                output_file,
                                frac_gap=None,
                                max_seconds=10*60):

    solution_I = lloovia.SolutionI.load(phase_I_solution)
    if solution_I.solving_stats.status == "aborted":
        # There is no solution of Phase I, so Phase II cannot
        # be performed
        print("Case '%s' has no solution for Phase I. Skipping" %
              (phase_I_solution))
        # Save empty dataframe as solution
        pd.DataFrame().to_pickle(output_file)
        return

    problem = lloovia.Problem(load_infrastructure(infrastructure_file),
                              load_workload(workload_file, year))
    solver = pulp.COIN(threads=2, fracGap=frac_gap,
                       maxSeconds=max_seconds, msg=1)
    phaseII = lloovia.PhaseII(problem, solution_I, solver=solver)

    phaseII.solve_period()
    phaseII.solution.save(output_file)
def perform_experiment_multiregion_phase_II(
                            azure_infrastructure_file,
                            amazon_infrastructure_file,
                            workload_file,
                            year,
                            providers_to_use,
                            phase_I_solution,
                            output_file,
                            frac_gap=None,
                            max_seconds=10*60):

    infrastructure = get_infrastructure_for_providers(
                            providers_to_use,
                            azure_infrastructure_file,
                            amazon_infrastructure_file
                            )
    solution_I = lloovia.SolutionI.load(phase_I_solution)
    if solution_I.solving_stats.status == "aborted":
        print("Case '%s' has no solution for Phase I. Skipping" %
              (phase_I_solution))
        # Save empty dataframe as solution
        pd.DataFrame().to_pickle(output_file)
        return
    problem = lloovia.Problem(infrastructure,
                              load_workload(workload_file, year))
    solver = pulp.COIN(threads=2, fracGap=frac_gap,
                       maxSeconds=max_seconds, msg=1)
    phaseII = lloovia.PhaseII(problem, solution_I, solver=solver)
    phaseII.solve_period()
    phaseII.solution.save(output_file)
示例#4
0
 def test_cbc(self, ilp):
     """Test method for CBC."""
     try:
         ilp.solve(pulp.COIN())
         assert round(pulp.value(ilp.objective), 0) == 5
     except pulp.solvers.PulpSolverError:
         pytest.fail(f"Solver not installed")
示例#5
0
	def solve(self,msg=0,**kwarg):
		# kind = 'CBC'
		if 'kind' in kwarg:
			kind = kwarg['kind']
		time_limit = None
		if 'time_limit' in kwarg:
			time_limit = float(kwarg['time_limit'])
		random_seed = None
		if 'random_seed' in kwarg:
			random_seed = kwarg['random_seed']
		ratio_gap = None
		if 'ratio_gap' in kwarg:
			ratio_gap = float(kwarg['ratio_gap'])
		start_time = time.time()
		# select solver for pl
		if kind == 'CPLEX':
			if time_limit is not None:
				# pulp does currently not support a timelimit in 1.5.9
				self.mip.solve(pl.CPLEX_CMD(msg=msg, timelimit=time_limit))
			else:
				self.mip.solve(pl.CPLEX_CMD(msg=msg))
		elif kind == 'GLPK':
			self.mip.solve(pl.GLPK_CMD(msg=msg))
		elif kind == 'SCIP':
			self.mip.solve(SCIP_CMD(msg=msg,time_limit=time_limit,ratio_gap=ratio_gap))
		elif kind == 'CBC' or kind == 'COIN':
			options = []
			if time_limit is not None:
				options.extend(['sec', str(time_limit)])
			if random_seed is not None:
				options.extend(['randomSeed', str(random_seed)])
				options.extend(['randomCbcSeed', str(random_seed)])
			if ratio_gap is not None:
				options.extend(['ratio', str(ratio_gap)])
			if kind == 'CBC':
				self.mip.solve(pl.PULP_CBC_CMD(msg=msg, options=options))
			elif kind == 'COIN':
				self.mip.solve(pl.COIN(msg=msg, options=options))
		elif kind == 'GUROBI':
			# GUROBI_CMD does not support a timelimit or epgap
			# GUROBI cannot dispatch parameters from options correctly
			options=[]
			if time_limit is not None:
				if ratio_gap is not None:
					self.mip.solve(pl.GUROBI(msg=msg,timeLimit=time_limit,epgap=ratio_gap))
			elif time_limit is not None:
				self.mip.solve(pl.GUROBI(msg=msg, timeLimit=time_limit))
			elif ratio_gap is not None:
				self.mip.solve(pl.GUROBI(msg=msg, epgap=ratio_gap))
			else:
				self.mip.solve(pl.GUROBI_CMD(msg=msg))

		else:
			raise Exception('ERROR: solver ' + kind + ' not known')

		if msg:
			print('INFO: execution time for solving mip (sec) = ' + str(time.time() - start_time))
		if self.mip.status == 1 and msg:
			print('INFO: objective = ' + str(pl.value(self.mip.objective)))
def perform_experiment_phase_I(infrastructure_file,
                               workload_file,
                               year,
                               max_bins,
                               output_file,
                               frac_gap=0.01,
                               max_seconds=10*60):
    problem = lloovia.Problem(load_infrastructure(infrastructure_file),
                              load_workload(workload_file, year))
    phaseI = lloovia.PhaseI(problem)
    solver = pulp.COIN(threads=2, fracGap=frac_gap,
                       maxSeconds=max_seconds, msg=1)
    phaseI.solve(max_bins=max_bins, solver=solver)
    phaseI.solution.save(output_file)
示例#7
0
    def _get_solver(solver_name, timelimit):
        if solver_name == "cplex":
            return pulp.CPLEX_PY(msg=0, timeLimit=timelimit)
        elif solver_name == "gurobi":
            return pulp.GUROBI(msg=0, timeLimit=timelimit)
        elif solver_name == "glpk":
            return pulp.GLPK(msg=0, options=["--tmlim", str(timelimit)])
        elif solver_name == "cbc":
            return pulp.COIN(msg=0, maxSeconds=timelimit)
        elif solver_name == "scip":
            return pulp.SCIP(msg=0,
                             options=["-c", f"set limits time {timelimit}"])

        raise ValueError("Invalid Solver Name")
def perform_experiment_multiregion_phase_I(
                                   azure_infrastructure_file,
                                   amazon_infrastructure_file,
                                   workload_file,
                                   year,
                                   max_bins,
                                   providers_to_use,
                                   output_file,
                                   frac_gap=None,
                                   max_seconds=60*60):

    infrastructure = get_infrastructure_for_providers(
                            providers_to_use,
                            azure_infrastructure_file,
                            amazon_infrastructure_file
                            )
    problem = lloovia.Problem(infrastructure,
                              load_workload(workload_file, year))
    phaseI = lloovia.PhaseI(problem)
    solver = pulp.COIN(threads=2, fracGap=frac_gap,
                       maxSeconds=max_seconds, msg=1)
    phaseI.solve(max_bins=max_bins, solver=solver)
    phaseI.solution.save(output_file)
def perform_experiment_phase_II_od_only(infrastructure_file,
                                        workload_file,
                                        year,
                                        output_file,
                                        frac_gap=None,
                                        max_seconds=10*60):

    infrastructure = load_infrastructure(infrastructure_file)
    only_ondemand = [vm for vm in infrastructure if not vm.reserved]
    only_reserved = [vm for vm in infrastructure if vm.reserved]
    problem = lloovia.Problem(only_ondemand,
                              load_workload(workload_file, year))
    # Create a fake solution from phase I in which all reserved
    # instances are deactivated
    trivial_stats = lloovia.SolvingStatsI(
                                max_bins=None,
                                workload=lloovia.LlooviaHistogram({0: 0}),
                                frac_gap=None,
                                max_seconds=None,
                                creation_time=0.0,
                                solving_time=0.0,
                                status="optimal",
                                lower_bound=None,
                                optimal_cost=0.0
                            )
    # Solution: Zero reserved VMs of each type
    sol = dict((vm, 0) for vm in only_reserved)
    trivial_allocation = pd.DataFrame([sol])[only_reserved]
    trivial_solution = lloovia.Solution(problem, trivial_stats,
                                        allocation=trivial_allocation)
    solver = pulp.COIN(threads=2, fracGap=frac_gap,
                       maxSeconds=max_seconds, msg=1)
    phaseII = lloovia.PhaseII(problem, trivial_solution, solver=solver)

    phaseII.solve_period()
    phaseII.solution.save(output_file)
示例#10
0
import pulp as solver
from pulp import *
import time
import gc
z = 0
solvers = [
    solver.CPLEX(timeLimit=30),
    solver.GLPK(),
    solver.GUROBI(),
    solver.PULP_CBC_CMD(),
    solver.COIN()
]
solverUsado = 3


def solver_exact(g, init, final, is_first, is_last, name):
    var = [(i + ',' + str(t)) for i in g.edge for t in range(1, g.z)]
    var2 = [(str(i) + ',' + str(t)) for i in g.vertices for t in range(1, g.z)]

    X = solver.LpVariable.dicts("X", var, cat=solver.LpBinary)
    Y = solver.LpVariable.dicts("Y", var2, cat=solver.LpBinary)
    problem = solver.LpProblem("The_best_Cut", solver.LpMinimize)
    if is_first:
        problem += (solver.lpSum(
            X.get(
                str(i.split(',')[0]) + ',' + str(i.split(',')[1]) + ',' +
                str(t)) * g.mis[i.split(',')[0]][i.split(',')[1]]
            for i in g.edge for t in range(1, g.z)) + solver.lpSum(
                ((g.pis[k.split(',')[0]][k.split(',')[1]])) -
                ((g.mis[k.split(',')[0]][k.split(',')[1]]))
                for k in g.edgeCuts) / 2) + solver.lpSum(
示例#11
0
def solve_problem(perf_factor,
                  instance_names,
                  n_apps,
                  first_app,
                  quant_factor,
                  exp_n,
                  max_priv_new,
                  priv_prev_n,
                  priv_prev_ecus,
                  priv_new_ecus,
                  priv_new_cost,
                  region_names=None,
                  verbose=False,
                  workload_dir="hours"):

    amazon_s3_data = cloud_providers.read_amazon_s3_data('amazon_s3_data.csv')
    amazon_ec2_data = cloud_providers.read_amazon_ec2_data(
        'amazon_ec2_data.csv')

    # This data is also filtered later, but it is done per instance, so if we
    # remove it here, the computation is faster
    amazon_ec2_data = remove_unneded_instances_ec2(amazon_ec2_data)

    ics, _ = create_ics(amazon_ec2_data, amazon_s3_data, instance_names,
                        region_names)

    # Add instance classes for the private cloud
    ics.append(
        malloovia.InstanceClass(id=f'priv',
                                name='priv',
                                limiting_sets=(),
                                price=priv_new_cost,
                                max_vms=max_priv_new,
                                is_reserved=True,
                                is_private=True,
                                time_unit="h",
                                cores=2))

    # Previously bought private instances
    if priv_prev_n > 0:
        ics.append(
            malloovia.InstanceClass(
                id=f'priv_prev',
                name='priv_prev',
                limiting_sets=(),
                price=0.000001,  # Insignificant
                max_vms=priv_prev_n,
                is_reserved=True,
                is_private=True,
                time_unit="h",
                cores=2))

    # Apps
    apps = [malloovia.App(f'a{i}', name=f'{i}') for i in range(n_apps)]

    # This is used for having more apps than the available workloads
    factor_repeat = math.ceil(n_apps / N_AVAILABLE_WLS)

    # Performances
    perfs_per_ecu = [1000, 500, 2000, 300
                     ] * factor_repeat  # Performance for a machine with 1 ECU
    perfs_per_ecu = reordered_list(perfs_per_ecu, first_app)
    perfs = get_perfs(amazon_ec2_data, ics, apps, perf_factor, perfs_per_ecu,
                      priv_prev_ecus, priv_new_ecus)

    perf_list = []
    for a in apps:
        l = list(perfs.values[i, a] for i in ics)
        perf_list.append(l)

    quanta = get_quanta(perf_list, quant_factor)

    # Workloads
    wls = []
    for i in range(N_AVAILABLE_WLS):
        with open(f'{workload_dir}/wl{i}.csv') as f:
            reader = csv.reader(f)
            for row in reader:
                wls.append(tuple(int(x) for x in row))
            wls[i] = wls[i][:WL_LEN]

    wls = reordered_list(wls, first_app)
    wls = wls * factor_repeat
    wls = wls[:n_apps]

    if quant_factor != 0:
        wls = discretize_levels(wls, quanta)
        wls = wls.tolist() * factor_repeat
    else:
        wls = wls * factor_repeat

    ltwp = []
    for app, wl in zip(apps, wls):
        ltwp.append(
            malloovia.Workload(
                "ltwp_{}".format(app.id),
                description="rph for {}".format(app.name),
                app=app,
                time_unit="h",
                values=wl,
            ))

    problem = malloovia.Problem(
        id="Hybrid cloud",
        name="Hybrid cloud",
        workloads=ltwp,
        instance_classes=ics,
        performances=perfs,
    )

    print('Solving', datetime.now().strftime("%H:%M:%S"))
    phase_i = malloovia.PhaseI(problem)
    solver = pulp.COIN(maxSeconds=2200 * 60, msg=1, fracGap=0, threads=8)
    phase_i_solution = phase_i.solve(solver=solver)

    filename = f'sols/{exp_n:03}_sol.p'
    pickle.dump(phase_i_solution, open(filename, 'wb'))

    status = phase_i_solution.solving_stats.algorithm.status
    if status != malloovia.Status.optimal:
        print(f"No optimal solution. Status: {status.name} ({status})")
        print('Time', datetime.now().strftime("%H:%M:%S"))

        raise Exception()

    comp_cost_malloovia = phase_i_solution.solving_stats.optimal_cost
    creation_time_malloovia = phase_i_solution.solving_stats.creation_time
    solving_time_malloovia = phase_i_solution.solving_stats.solving_time

    if verbose:
        print("=" * 80)
        print(f"Computation cost = {comp_cost_malloovia}")
        print("=" * 80)

    return ExpResult(comp_cost_malloovia, creation_time_malloovia,
                     solving_time_malloovia)
示例#12
0
def find_closest_positions(
    funds: float,
    composition: Composition,
    prices: dict[SimpleContract, float],
) -> dict[SimpleContract, int]:
    """
    Constructs the most closely-matching concrete, integral-share Portfolio
    matching this Allocation.

    It is guaranteed that portfolio.gpv <= allocation.

    Using a MILP solver might be overkill for this, but we do, ever-so-rarely,
    round the other way from the target (fractional) allocation. This lets us do
    that correctly without guesswork. Can't be too careful with money.

    :param funds: total amount of money available to spend on the portfolio.
    :param composition: the target composition to approximate
    :param prices: the assumed _unsafe_prices of the securities.
    :return: a mapping from contacts to allocated share counts.
    """

    # will raise if a price is missing
    comp_arr, price_arr = np.array(
        [[composition[c], prices[c]] for c in composition.contracts]
    ).T

    assert np.isclose(comp_arr.sum(), 1.0)
    assert len(composition) == len(prices)

    target_alloc = funds * comp_arr / price_arr

    prob = pulp.LpProblem()

    alloc = lparray.create_anon(
        "Alloc", shape=comp_arr.shape, cat=pulp.LpInteger
    )

    # to avoid zero division later
    (alloc >= 1).constrain(prob, "PositivePositions")

    cost = (alloc @ price_arr).sum()
    (cost <= funds).constrain(prob, "DoNotExceedFunds")

    # TODO bigM here should be the maximum possible value of
    # alloc - target_alloc
    # while 100k should be enough for reasonable uses, we can figure master_eq a
    # proper max
    loss = (alloc - target_alloc).abs(prob, "Loss", bigM=1_000_000).sumit()
    prob += loss

    try:
        pulp.COIN(msg=False).solve(prob)
    except Exception as e:
        raise PortfolioSolverError(e)

    status = prob.status
    if status == LpStatusInfeasible:
        raise ProgrammingError(f"Solver returned infeasible: {prob}.")

    # this means the solver was interrupted -- we propagate that up
    elif status == "Not Solved":
        raise KeyboardInterrupt()

    normed_misalloc = loss.value() / funds
    Policy.MISALLOCATION.validate(normed_misalloc)

    return {c: int(v) for c, v in zip(composition.contracts, alloc.values)}
示例#13
0
def solve_problem(perf_factor, verbose=False):
    ics = [  # Only one
        InstanceClass(id='ic',
                      name='ic',
                      limiting_sets=(),
                      price=0.01,
                      max_vms=3,
                      is_reserved=False,
                      time_unit="h")
    ]

    n_apps = 1

    # Apps
    apps = tuple(App(f'a{i}', name=f'{i}') for i in range(n_apps))

    # Performances
    perfs_per_ecu = [RPH, RPH]
    perfs = get_perfs(ics=tuple(ics),
                      apps=apps,
                      perf_factor=perf_factor,
                      perfs_per_ecu=perfs_per_ecu)

    # Workloads
    wls = ([3 * RPH, RPH] * (WL_LEN // 2), )

    ltwp = []
    for app, wl in zip(apps, wls):
        ltwp.append(
            Workload(
                "ltwp_{}".format(app.id),
                description="rph for {}".format(app.name),
                app=app,
                time_unit="h",
                values=tuple(wl),
            ))

    problem = Problem(
        id="Hybrid cloud",
        name="Hybrid cloud",
        workloads=tuple(ltwp),
        instance_classes=tuple(ics),
        performances=perfs,
    )

    print('Solving', datetime.now().strftime("%H:%M:%S"))
    phase_i = PhaseI(problem)
    solver = pulp.COIN(maxSeconds=2200 * 60, msg=1, fracGap=0.05, threads=8)
    phase_i_solution = phase_i.solve(solver=solver)

    filename = 'sols/3vm.p'
    pickle.dump(phase_i_solution, open(filename, 'wb'))

    yaml = solutions_to_yaml([phase_i_solution])
    with open('sols/3vm.yaml', 'w') as f:
        f.write(yaml)

    status = phase_i_solution.solving_stats.algorithm.status
    if status != Status.optimal:
        print(f"No optimal solution. Status: {status.name} ({status})")
        print('Time', datetime.now().strftime("%H:%M:%S"))

        raise Exception()

    comp_cost_malloovia = phase_i_solution.solving_stats.optimal_cost

    if verbose:
        print("=" * 80)
        print(f"Computation cost = {comp_cost_malloovia}")
        print("=" * 80)