def set_objective(self, expression):
if isinstance(expression, numbers.Number):<EOL><INDENT>expression = Expression(offset=expression)<EOL><DEDENT>linear = []<EOL>quad = []<EOL>for var in self._non_zero_objective:<EOL><INDENT>if var not in expression:<EOL><INDENT>if not isinstance(var, Product):<EOL><INDENT>linear.append((self._variables[var], <NUM_LIT:0>))<EOL><DEDENT>else:<EOL><INDENT>t = self._variables[var[<NUM_LIT:0>]], self._variables[var[<NUM_LIT:1>]], <NUM_LIT:0><EOL>quad.append(t)<EOL><DEDENT><DEDENT><DEDENT>self._non_zero_objective.clear()<EOL>for var, value in expression.values():<EOL><INDENT>if not isinstance(var, Product):<EOL><INDENT>self._non_zero_objective.add(var)<EOL>linear.append((self._variables[var], float(value)))<EOL><DEDENT>else:<EOL><INDENT>if len(var) > <NUM_LIT:2>:<EOL><INDENT>raise ValueError('<STR_LIT>'.format(var))<EOL><DEDENT>self._non_zero_objective.add(var)<EOL>var1 = self._variables[var[<NUM_LIT:0>]]<EOL>var2 = self._variables[var[<NUM_LIT:1>]]<EOL>if var1 == var2:<EOL><INDENT>value *= <NUM_LIT:2><EOL><DEDENT>quad.append((var1, var2, float(value)))<EOL><DEDENT><DEDENT>if len(linear) > <NUM_LIT:0>:<EOL><INDENT>self._cp.objective.set_linear(linear)<EOL><DEDENT>if len(quad) > <NUM_LIT:0>:<EOL><INDENT>self._cp.objective.set_quadratic_coefficients(quad)<EOL><DEDENT>if hasattr(self._cp.objective, '<STR_LIT>'):<EOL><INDENT>self._cp.objective.set_offset(float(expression.offset))<EOL><DEDENT>
Set objective expression of the problem.