Commit 80b2d08b authored by Unknown's avatar Unknown
Browse files

Merge branch 'CarbonModelCleanup' into dev

parents 32aa19f1 605da138
Loading
Loading
Loading
Loading
Loading
+9 −1
Original line number Diff line number Diff line
@@ -50,7 +50,7 @@ class EHubModel:
    Represents a black-box Energy Hub.
    """

    def __init__(self, *, excel=None, request=None, big_m=99999):
    def __init__(self, *, excel=None, request=None, big_m=99999, max_carbon=None):
        """Create a new Energy Hub using some input data.

        Args:
@@ -61,6 +61,7 @@ class EHubModel:
        self._compiled = False
        self._objective = None
        self.BIG_M = big_m
        self.MAX_CARBON = max_carbon

        if excel:
            request = excel_to_request_format.convert(excel)
@@ -816,6 +817,13 @@ class EHubModel:
            else:
                raise RuntimeError

    @constraint()
    def max_carbon_level(self):
        """Constraint to set a max carbon cap."""
        if self.MAX_CARBON is not None:
            return self.total_carbon <= float(self.MAX_CARBON)


    # *****************************************************************************************************************
    # EVERYTHING BELOW HERE IS A HELPER FUNCTION, NOT PART OF THE MODEL
    # *****************************************************************************************************************
+17 −40
Original line number Diff line number Diff line
@@ -59,8 +59,8 @@ class NetworkModel(EHubModel):
    A subclass that allows connections between hubs.
    """

    def __init__(self, *, excel=None, request=None, name=None, network=None, network_request=None, hub_id=None):
        super().__init__(excel=excel, request=request)
    def __init__(self, *, excel=None, request=None, name=None, network=None, network_request=None, hub_id=None, max_carbon=None):
        super().__init__(excel=excel, request=request, max_carbon=max_carbon)
        self.name = name
        self.hub_id = hub_id
        if network:
@@ -152,27 +152,6 @@ class NetworkModel(EHubModel):

            setattr(self, name, variable)

class NetworkModelWithTotalCarbon(NetworkModel):
    """
    A subclass that allows connections between hubs.
    """

    MAX_CARBON = 0

    def __init__(self, *, excel=None, request=None, name=None, n=0, network=None, network_request=None, hub_id=None):
        super().__init__(excel=excel, request=request, name=name, network=network, network_request=network_request, hub_id=hub_id)
        if n is not None:
            self.MAX_CARBON = n

    @constraint()
    def max_carbon_level(self):
        """
        A constraint for epsilon constraint method.
        """
        return self.total_carbon <= float(self.MAX_CARBON)



# NETWORK Link specific constraints:

@constraint()
@@ -237,21 +216,23 @@ def linear_power_flow_constraint(power, angle_from, angle_to, time, reactance):
        yield power[t] == 1/reactance * (angle_from[t] - angle_to[t])
    

def multiple_hubs(minimize_carbon=False, output_filename=None, input_files=None, network_excel=None, network_request=None, carbon_value=0, n=0):
def multiple_hubs(minimize_carbon=False, output_filename=None, input_files=None, network_excel=None, network_request=None, max_carbon=None, n=0, solver='glpk'):
    """
    Core function for solving of multiple PyEHub models.
    """
    if carbon_value is None:
        carbon_value = 0

    # The default excel file for network, in the network directory
    if network_excel is None:
        network_excel_file = 'Input_files/network_input/network.xlsx'
    # Additional option for tests for multiple_hubs. To input a different network excel files.
    else:
    if network_excel:
        network_excel_file = network_excel
    network = network_to_request_format.convert(network_excel_file)
    _net_data = InputData(network)
        network_request = network_to_request_format.convert(network_excel_file)

        if network_request:
            _net_data = InputData(network_request)
        else:
            # The default excel file for network, in the network directory
            # network_excel_file = 'Input_files/network_input/network.xlsx'
            # network_request = network_to_request_format.convert(network_excel_file)
            # _net_data = InputData(network_request)
            raise RuntimeError("Can't create a network with no network data.")

    # Create all the hubs
    hubs = []
@@ -264,12 +245,8 @@ def multiple_hubs(minimize_carbon=False, output_filename=None, input_files=None,
            file_name = f'{input_files}{i+1}.xlsx'
        excel_file = file_name

        if minimize_carbon:
            hub = NetworkModelWithTotalCarbon(excel=excel_file, name=f'hub{i+1}', n=carbon_value,
                                              network=network_excel_file, hub_id=i)
        else:
            hub = NetworkModel(excel=excel_file, name=f'hub{i+1}',
                                                 network=network_excel_file, hub_id=i)
        hub = NetworkModel(excel=excel_file, name=f'hub{i+1}', max_carbon=max_carbon,
                                            network=network_excel_file, network_request=network_request, hub_id=i)
        hubs.append(hub)

    # Creating a list of all the constraints from all the hubs
@@ -427,7 +404,7 @@ def multiple_hubs(minimize_carbon=False, output_filename=None, input_files=None,
        objective += hub.objective

    # Now solve this model.
    status = pylp.solve(objective=objective, constraints=constraints, minimize=True)
    status = pylp.solve(objective=objective, constraints=constraints, minimize=True, solver=solver)

    #: Define the different sheets that we output
    sheets = [
+12 −12
Original line number Diff line number Diff line
@@ -37,19 +37,19 @@ from pyehub.energy_hub.utils import constraint
DEFAULT_CONFIG_FILE = 'config.yaml'
DEFAULT_LOG_FILE = 'logs.log'

class CarbonEHubModel(EHubModel):
    """Modified EHubModel to minimize cost with a max carbon cap."""
    MAX_CARBON = 0
# class CarbonEHubModel(EHubModel):
#     """Modified EHubModel to minimize cost with a max carbon cap."""
#     MAX_CARBON = 0

    def __init__(self, *, excel=None, request=None, max_carbon=0, big_m=99999):
        super().__init__(excel=excel, request=request, big_m=big_m)
        if max_carbon is not None:
            self.MAX_CARBON = max_carbon
#     def __init__(self, *, excel=None, request=None, max_carbon=0, big_m=99999):
#         super().__init__(excel=excel, request=request, big_m=big_m)
#         if max_carbon is not None:
#             self.MAX_CARBON = max_carbon

    @constraint()
    def max_carbon_level(self):
        """Constraint to set a max carbon cap."""
        return self.total_carbon <= float(self.MAX_CARBON)
#     @constraint()
#     def max_carbon_level(self):
#         """Constraint to set a max carbon cap."""
#         return self.total_carbon <= float(self.MAX_CARBON)


def main():
@@ -60,7 +60,7 @@ def main():
    settings = parse_arguments(arguments)

    if settings['carbon']:
        model = CarbonEHubModel(excel=settings['input_file'], max_carbon=settings['max_carbon'], big_m=settings['big_m'])
        model = EHubModel(excel=settings['input_file'], max_carbon=settings['max_carbon'], big_m=settings['big_m'])
    else:
        model = EHubModel(excel=settings['input_file'], big_m=settings['big_m'])

+30.1 KiB

File added.

No diff preview for this file type.

+30.1 KiB

File added.

No diff preview for this file type.

Loading