<SYSTEM_TASK:>
Solve the following problem using Python, implementing the functions described below, one line at a time
<END_TASK>
<USER_TASK:>
Description:
 def set_objective(self, expression):
 """Set objective expression of the problem."""


        if isinstance(expression, numbers.Number):
            # Allow expressions with no variables as objective,
            # represented as a number
            expression = Expression(offset=expression)

        linear = []
        quad = []

        # Reset previous objective.
        for var in self._non_zero_objective:
            if var not in expression:
                if not isinstance(var, Product):
                    linear.append((self._variables[var], 0))
                else:
                    t = self._variables[var[0]], self._variables[var[1]], 0
                    quad.append(t)

        self._non_zero_objective.clear()

        # Set actual objective values
        for var, value in expression.values():
            if not isinstance(var, Product):
                self._non_zero_objective.add(var)
                linear.append((self._variables[var], float(value)))
            else:
                if len(var) > 2:
                    raise ValueError('Invalid objective: {}'.format(var))
                self._non_zero_objective.add(var)
                var1 = self._variables[var[0]]
                var2 = self._variables[var[1]]
                if var1 == var2:
                    value *= 2
                quad.append((var1, var2, float(value)))

        # We have to build the set of variables to
        # update so that we can avoid calling set_linear if the set is empty.
        # This is due to set_linear failing if the input is an empty
        # iterable.
        if len(linear) > 0:
            self._cp.objective.set_linear(linear)
        if len(quad) > 0:
            self._cp.objective.set_quadratic_coefficients(quad)

        if hasattr(self._cp.objective, 'set_offset'):
            self._cp.objective.set_offset(float(expression.offset))