def construct_constraint(year, index):
    """
    This function is a convenience function that returns a valid demand constraint for one year.
    The wrapper function construct_constraint is needed because we need a reference to year in the wrapped
    meet_demand function in model.buy[year, supplier, block] and model.implemented[supplier, year]
    :param year: reference to year
    :param index: index of the year. The first year is 2009 so the index is 0. For 2010 the index is 1.
    :return:
    """

    def meet_demand(model, block):
        """
        Enforce the meeting of the demand
        :param model:
        :param block:
        :return:
        """

        # dsm_savints = (YEAR - 2008) * total initial savings for the implemented dsms
        # the first part (index+1) == (YEAR - 2008) because in our model the dsm options reduce the demand not only once
        # but in every consecutive year. So we have a cumulative saving over the years.
        dsm_savings = (index+1)*sum(model.dsm_implemented[dsm] * model.dsm_MW_savings[dsm, block] for dsm in model.DSM)

        # demand - savings by dsm == supply <=> demand == supply + savings_by_dsm
        return (model.demand[block]) == sum(model.buy[year, supplier, block] for supplier in model.SUPPLIER if model.implemented[supplier, year]) + dsm_savings

    return meet_demand