Commit 7cb20c86 authored by Unknown's avatar Unknown
Browse files

Broke the code out into separate functions and added capacity constraint

parent 1de10cfd
Loading
Loading
Loading
Loading
Loading
+35 −6
Original line number Diff line number Diff line
@@ -22,14 +22,20 @@ DOMAIN_TO_VARIABLE = {


@constraint()
def same_converter_constraint(hubs):
    pass
def same_converter_constraint(converter, hubs):
    """
    Constraint to ensure the capacities are kept constant across all the subproblems. 
    """
    first = True
    for i in enumerate(hubs):
        if not first:
            yield hubs[i-1].capacities[converter] == hubs[i].capacities[converter]
            first = False
        

def periodic_subproblem(excel=None, request=None, output_filename=None, max_carbon=None, num_periods=1, len_periods=24, solver='glpk'):
def split_hubs(excel=None, request=None, max_carbon=None, num_periods=1, len_periods=24):
    """
    Core function for splitting a PyEHub model into smaller problems to solve together.
    :param: excel
    Splits a PyEHub into a series of smaller hubs with a given period.
    """
    if excel:
        request = excel_to_request_format.convert(excel)
@@ -41,19 +47,40 @@ def periodic_subproblem(excel=None, request=None, output_filename=None, max_carb
        
    hubs = []

    if ((num_periods*len_periods) > len(request['time_series'][0]['data'])):
        raise IndexError("Not enough data to cover all the periods.")

    for i in range(0, num_periods):
        temp_request = copy.deepcopy(request)
        for stream in temp_request['time_series']:
            stream['data'] = stream['data'][i*len_periods:(i+1)*len_periods]
        hub = EHubModel(request=temp_request, max_carbon=max_carbon)
        hubs.append(hub)
    return hubs

def merge_hubs(hubs):
    """
    Compiles and combines
    """
    constraints = []
    for hub in hubs:
        hub.recompile()
        for constr in hub.constraints:
            constraints.append(constr)

    for converter in hubs[0]._data.converter_names:
        for c in same_converter_constraint(converter, hubs):
            constraints.append(c)

    return constraints

def run_split_period(excel=None, request=None, output_filename=None, max_carbon=None, num_periods=1, len_periods=24, solver='glpk'):
    """
    Core function for splitting a PyEHub model into smaller problems to solve together.
    """
    hubs = split_hubs(excel, request, max_carbon, num_periods, len_periods)
    constraints = merge_hubs(hubs)

    #TODO: Make sure storage looping is working for each sub_hub

    #FIXME: Ralph had specific instructions for how to handle the objective (wanting a single one or something)
@@ -63,6 +90,8 @@ def periodic_subproblem(excel=None, request=None, output_filename=None, max_carb
    for hub in hubs[1:]:
        objective += hub.objective



    status = pylp.solve(objective=objective, constraints=constraints, minimize=True, solver=solver)

    #TODO: Figure out setting up the output
 No newline at end of file