Loading pyehub/multiple_hubs.py +37 −109 Original line number Diff line number Diff line Loading @@ -52,22 +52,24 @@ DOMAIN_TO_VARIABLE = { } class NetworkModelWithTotalCarbon(EHubModel): class NetworkModel(EHubModel): """ A subclass that allows connections between hubs. """ MAX_CARBON = 0 def __init__(self, *, excel=None, request=None, name=None, n=0, network=None, hub_id=None): def __init__(self, *, excel=None, request=None, name=None, network=None, network_request=None, hub_id=None): super().__init__(excel=excel, request=request) if n is not None: self.MAX_CARBON = n self.name = name self.hub_id = hub_id if network: network_request = network_to_request_format.convert(network) if network_request: self._net_data = InputData(network_request) if self._net_data: self.links = self._net_data.links_ids self.link_capacities = ConstantOrVar(self.links, model=self, values=self._net_data.link_capacity) self.network_cost = RealVariable() Loading @@ -78,6 +80,9 @@ class NetworkModelWithTotalCarbon(EHubModel): self.FIXED_NETWORK_INVESTMENT_COST = self._net_data.fixed_network_investment_cost self.LINK_PROPORTIONAL_COST = self._net_data.link_proportional_cost else: raise RuntimeError("Can't create a network with no data.") def _add_variables(self): super()._add_variables() self._add_link_capacity_variables() Loading Loading @@ -110,7 +115,8 @@ class NetworkModelWithTotalCarbon(EHubModel): def calc_network_investment_cost(self): """ Calculating investment cost for the links the hub has. Cost split between 2 hubs that are connected with the same link. Cost split between 2 hubs that are connected with the same link """ cost = 0 for i, link in enumerate(self.links): Loading Loading @@ -146,105 +152,28 @@ class NetworkModelWithTotalCarbon(EHubModel): setattr(self, name, variable) @constraint() def max_carbon_level(self): """ A constraint for epsilon constraint method. """ return self.total_carbon <= float(self.MAX_CARBON) class NetworkModelWithoutTotalCarbon(EHubModel): class NetworkModelWithTotalCarbon(NetworkModel): """ A subclass that allows connections between hubs. """ def __init__(self, *, excel=None, request=None, name=None, network=None, hub_id=None): super().__init__(excel=excel, request=request) self.name = name self.hub_id = hub_id network_request = network_to_request_format.convert(network) self._net_data = InputData(network_request) self.links = self._net_data.links_ids self.link_capacities = ConstantOrVar(self.links, model=self, values=self._net_data.link_capacity) self.network_cost = RealVariable() self.is_link_installed = {link: BinaryVariable() for link in self.links} self.LINK_LENGTH = self._net_data.link_length self.FIXED_NETWORK_INVESTMENT_COST = self._net_data.fixed_network_investment_cost self.LINK_PROPORTIONAL_COST = self._net_data.link_proportional_cost def _add_variables(self): super()._add_variables() self._add_link_capacity_variables() for i in self._net_data.capacities: self._data.capacities.append(i) @constraint('links') def link_is_installed(self, link): """ Set binary to 1 if capacity of link is > 0. Args: link: A link """ capacity = self.link_capacities[link] rhs = self.BIG_M * self.is_link_installed[link] return capacity <= rhs @constraint('links') def link_is_installed_2(self, link): """ Set binary to 1 if capacity of link is > 0. Args: link: A link """ installed = self.is_link_installed[link] lhs = self.BIG_M * self.link_capacities[link] return lhs >= installed @constraint() def calc_network_investment_cost(self): """ Calculating investment cost for the links the hub has. Cost split between 2 hubs that are connected with the same link """ cost = 0 for i, link in enumerate(self.links): if (self._net_data.link_start[i] == self.hub_id) or (self._net_data.link_end[i] == self.hub_id): cost += (self.FIXED_NETWORK_INVESTMENT_COST * self.LINK_LENGTH[link] * self.is_link_installed[link] + self.link_capacities[link] * self.LINK_LENGTH[link] * self.LINK_PROPORTIONAL_COST) MAX_CARBON = 0 cost = cost/2 return self.network_cost == cost 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 calc_total_cost(self): def max_carbon_level(self): """ Modifying total cost constraint. Adding network cost to the total cost. A constraint for epsilon constraint method. """ parent_constraint = super().calc_total_cost() old_cost = parent_constraint.rhs return self.total_cost == old_cost + self.network_cost def _add_link_capacity_variables(self): for capacity in self._net_data.capacities: domain = capacity.domain name = capacity.name return self.total_carbon <= float(self.MAX_CARBON) try: variable = DOMAIN_TO_VARIABLE[domain]() except KeyError: raise ValueError( f'Cannot create variable of type: {domain}. Can only be: ' f'{list(DOMAIN_TO_VARIABLE.keys())}' ) setattr(self, name, variable) # NETWORK Link specific constraints: @constraint() def network_constraint(hub, link_end, link_start): Loading Loading @@ -308,7 +237,7 @@ 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, carbon_value=0, n=0): def multiple_hubs(minimize_carbon=False, output_filename=None, input_files=None, network_excel=None, network_request=None, carbon_value=0, n=0): """ Core function for solving of multiple PyEHub models. """ Loading Loading @@ -339,7 +268,7 @@ def multiple_hubs(minimize_carbon=False, output_filename=None, input_files=None, hub = NetworkModelWithTotalCarbon(excel=excel_file, name=f'hub{i+1}', n=carbon_value, network=network_excel_file, hub_id=i) else: hub = NetworkModelWithoutTotalCarbon(excel=excel_file, name=f'hub{i+1}', hub = NetworkModel(excel=excel_file, name=f'hub{i+1}', network=network_excel_file, hub_id=i) hubs.append(hub) Loading Loading @@ -494,7 +423,6 @@ def multiple_hubs(minimize_carbon=False, output_filename=None, input_files=None, # The objective function of the model is the summation of all the hub's # objective function objective = hubs[0].objective for hub in hubs[1:]: objective += hub.objective Loading Loading
pyehub/multiple_hubs.py +37 −109 Original line number Diff line number Diff line Loading @@ -52,22 +52,24 @@ DOMAIN_TO_VARIABLE = { } class NetworkModelWithTotalCarbon(EHubModel): class NetworkModel(EHubModel): """ A subclass that allows connections between hubs. """ MAX_CARBON = 0 def __init__(self, *, excel=None, request=None, name=None, n=0, network=None, hub_id=None): def __init__(self, *, excel=None, request=None, name=None, network=None, network_request=None, hub_id=None): super().__init__(excel=excel, request=request) if n is not None: self.MAX_CARBON = n self.name = name self.hub_id = hub_id if network: network_request = network_to_request_format.convert(network) if network_request: self._net_data = InputData(network_request) if self._net_data: self.links = self._net_data.links_ids self.link_capacities = ConstantOrVar(self.links, model=self, values=self._net_data.link_capacity) self.network_cost = RealVariable() Loading @@ -78,6 +80,9 @@ class NetworkModelWithTotalCarbon(EHubModel): self.FIXED_NETWORK_INVESTMENT_COST = self._net_data.fixed_network_investment_cost self.LINK_PROPORTIONAL_COST = self._net_data.link_proportional_cost else: raise RuntimeError("Can't create a network with no data.") def _add_variables(self): super()._add_variables() self._add_link_capacity_variables() Loading Loading @@ -110,7 +115,8 @@ class NetworkModelWithTotalCarbon(EHubModel): def calc_network_investment_cost(self): """ Calculating investment cost for the links the hub has. Cost split between 2 hubs that are connected with the same link. Cost split between 2 hubs that are connected with the same link """ cost = 0 for i, link in enumerate(self.links): Loading Loading @@ -146,105 +152,28 @@ class NetworkModelWithTotalCarbon(EHubModel): setattr(self, name, variable) @constraint() def max_carbon_level(self): """ A constraint for epsilon constraint method. """ return self.total_carbon <= float(self.MAX_CARBON) class NetworkModelWithoutTotalCarbon(EHubModel): class NetworkModelWithTotalCarbon(NetworkModel): """ A subclass that allows connections between hubs. """ def __init__(self, *, excel=None, request=None, name=None, network=None, hub_id=None): super().__init__(excel=excel, request=request) self.name = name self.hub_id = hub_id network_request = network_to_request_format.convert(network) self._net_data = InputData(network_request) self.links = self._net_data.links_ids self.link_capacities = ConstantOrVar(self.links, model=self, values=self._net_data.link_capacity) self.network_cost = RealVariable() self.is_link_installed = {link: BinaryVariable() for link in self.links} self.LINK_LENGTH = self._net_data.link_length self.FIXED_NETWORK_INVESTMENT_COST = self._net_data.fixed_network_investment_cost self.LINK_PROPORTIONAL_COST = self._net_data.link_proportional_cost def _add_variables(self): super()._add_variables() self._add_link_capacity_variables() for i in self._net_data.capacities: self._data.capacities.append(i) @constraint('links') def link_is_installed(self, link): """ Set binary to 1 if capacity of link is > 0. Args: link: A link """ capacity = self.link_capacities[link] rhs = self.BIG_M * self.is_link_installed[link] return capacity <= rhs @constraint('links') def link_is_installed_2(self, link): """ Set binary to 1 if capacity of link is > 0. Args: link: A link """ installed = self.is_link_installed[link] lhs = self.BIG_M * self.link_capacities[link] return lhs >= installed @constraint() def calc_network_investment_cost(self): """ Calculating investment cost for the links the hub has. Cost split between 2 hubs that are connected with the same link """ cost = 0 for i, link in enumerate(self.links): if (self._net_data.link_start[i] == self.hub_id) or (self._net_data.link_end[i] == self.hub_id): cost += (self.FIXED_NETWORK_INVESTMENT_COST * self.LINK_LENGTH[link] * self.is_link_installed[link] + self.link_capacities[link] * self.LINK_LENGTH[link] * self.LINK_PROPORTIONAL_COST) MAX_CARBON = 0 cost = cost/2 return self.network_cost == cost 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 calc_total_cost(self): def max_carbon_level(self): """ Modifying total cost constraint. Adding network cost to the total cost. A constraint for epsilon constraint method. """ parent_constraint = super().calc_total_cost() old_cost = parent_constraint.rhs return self.total_cost == old_cost + self.network_cost def _add_link_capacity_variables(self): for capacity in self._net_data.capacities: domain = capacity.domain name = capacity.name return self.total_carbon <= float(self.MAX_CARBON) try: variable = DOMAIN_TO_VARIABLE[domain]() except KeyError: raise ValueError( f'Cannot create variable of type: {domain}. Can only be: ' f'{list(DOMAIN_TO_VARIABLE.keys())}' ) setattr(self, name, variable) # NETWORK Link specific constraints: @constraint() def network_constraint(hub, link_end, link_start): Loading Loading @@ -308,7 +237,7 @@ 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, carbon_value=0, n=0): def multiple_hubs(minimize_carbon=False, output_filename=None, input_files=None, network_excel=None, network_request=None, carbon_value=0, n=0): """ Core function for solving of multiple PyEHub models. """ Loading Loading @@ -339,7 +268,7 @@ def multiple_hubs(minimize_carbon=False, output_filename=None, input_files=None, hub = NetworkModelWithTotalCarbon(excel=excel_file, name=f'hub{i+1}', n=carbon_value, network=network_excel_file, hub_id=i) else: hub = NetworkModelWithoutTotalCarbon(excel=excel_file, name=f'hub{i+1}', hub = NetworkModel(excel=excel_file, name=f'hub{i+1}', network=network_excel_file, hub_id=i) hubs.append(hub) Loading Loading @@ -494,7 +423,6 @@ def multiple_hubs(minimize_carbon=False, output_filename=None, input_files=None, # The objective function of the model is the summation of all the hub's # objective function objective = hubs[0].objective for hub in hubs[1:]: objective += hub.objective Loading