An algorithm of polynomial complexity is proposed that produces an optimal task encoder for tasks that are generated according to some given law and that need to be described using a given number of labels. It thus minimizes the expectation (or ρ-th moment) of the number of tasks that share the label of a randomly-generated task.