Variables

In LCA as Code, you can use formulas to express exchange quantities. For instance, imagine a customer in a shop who wants to buy chocolate bars and candies. She has a budget of B = 20 units. The unit price of a chocolate bar is p_choco = 4 units. The unit price of a candy is p_candy = 2 units. Under a utility function of the form u = n_choco^a_choco * n_candy^a_candy, the demand that maximizes utility under the budget constraint is

  • n_choco = B * a_choco / p_choco
  • n_candy = B * a_candy / p_candy

In LCA as Code, this can be modelled as a process

process consumption {
    products {
        1 u consumer
    }
    inputs {
        200 u * 2 u / 4 u chocolate_bar
        200 u * 1 u / 2 u candy
    }
}

Local variables

It is often convenient to name parts of a formula to make it easier to read, i.e., to introduce (local) variables. For that, we use a block variables.

process consumption {
    products {
        1 u consumer
    }
    variables {
        budget = 200 u
        a_choco = 2 u
        p_choco = 4 u
        a_candy = 1 u
        p_candy = 2 u
    }
    inputs {
        budget * a_choco / p_choco chocolate_bar
        budget * a_candy / p_candy candy
    }
}

Note that variables are not parameters: you cannot assign a value to a variable when invoking a process.

process p {
    // products omitted in this example
    inputs {
        // error : budget is not a parameter
        1 u consumer from consumption(budget = 400 u)
    }
}

The purpose of a variable is simply to give a name to a quantity within the scope of a process. You can define new variables out of previous variables. For instance:

process consumption {
    params {
        budget = 200 u
    }
    products {
        1 u consumer
    }
    variables {
        a_choco = 2 u
        p_choco = 4 u
        a_candy = 1 u
        p_candy = 2 u
        q_choco = budget * a_choco / p_choco
        q_candy = budget * a_candy / p_candy
    }
    inputs {
        q_choco chocolate_bar
        q_candy candy
    }
}

Global variables

Sometimes, one may want to a variable to be available globally. For instance, the unit prices of chocolate bar and candy can be set once and for all. To do that, simply declare a block variables outside any process.

variables {
    p_choco = 4 u
    p_candy = 2 u
}

process consumption {
    params {
        budget = 200 u
    }
    products {
        1 u consumer
    }
    variables {
        a_choco = 2 u
        a_candy = 1 u
        q_choco = budget * a_choco / p_choco
        q_candy = budget * a_candy / p_candy
    }
    inputs {
        q_choco chocolate_bar
        q_candy candy
    }
}