Optimizer

class code_generation.optimizer.ProducerOrdering(global_producers, scope, producer_ordering)[source]

Class used to check if the producer ordering is correct, If it is not, the Optimize function will auto-correct it. Additionally, the optimize attempts to put filters at the top of the list, or as far up as possible. A wrong configuration due to missing inputs will also be caught here.

If the scope is not global, the outputs generated by producers in the global scope are also considered.

Init function

Parameters:
  • config – The configuration dictionary

  • global_producers (List[Producer | ProducerGroup]) – The list of producers in the global scope

  • scope (str) – The scope of the producer ordering

  • producer_ordering (List[Producer | ProducerGroup]) – The producer ordering to be optimized

MoveFiltersUp()[source]

Function used to relocate all filters to the top of the ordering, preserving the order of the filters given in the config file.

Parameters:

None

Returns:

None

Return type:

None

Optimize()[source]

The main function of this class. During the optimization, finding a correct ordering is attempted. This is done as follows:

  1. Bring all filters to the beginning of the ordering.

  2. Check if the ordering is already correct. The ordering is correct, if, for all producers in the ordering, all inputs can be found in the outputs of preceding producers. If the scope is not global, all outputs from producers in the global scope are also considered.

  3. If the ordering is correct, return.

  4. If the ordering is not correct,

    1. find all inputs, that have to be produced before the wrong producer

    2. put one producer, which is responsible for creating the input, in front of the wrong producer

    3. repeat from step 2

The sorting algorithm should take at most 2*(number of producers) steps. If this limit is reached, the optimization is considered to be failed and an Exception is raised. If a missing input cant be found in all outputs, the Optimize function will raise an Exception.

Parameters:

None

Returns:

None

Return type:

None

check_ordering()[source]

Function used to check the ordering. If at least of one the inputs of a producer cannot be found in the list of all preceding outputs, the ordering is not correct. If the whole odering is correct, the optimized flag is set to true and the ordering is considered to be correct.

Parameters:

None

Returns:

A tuple of the wrong producer and a list of the inputs that are not found in the outputs

Return type:

Tuple[Producer | ProducerGroup | None, List[Quantity]]

find_inputs(producer, inputs)[source]

Function used to locate the producers responsible for creating the given inputs. The function return a list of producers, that have to be run before the tested producer. If a needed input is not found, the function raise an Exception.

Parameters:
Returns:

A list of producers, that have to be run before the given producer

Return type:

producers_to_relocate

get_global_outputs()[source]

Function used to generate a list of all outputs generated by the global scope.

Parameters:

None

Returns:

A list of all outputs generated by the global scope

Return type:

List[Quantity]

get_position(producer)[source]

Helper Function to get the position of a producer in the ordering list

Parameters:

producer (Producer | ProducerGroup) – The producer to get the position of

Returns:

The position of the producer in the current ordering

Return type:

int

get_producer(position)[source]

Helper function to get the producer at a given position

Parameters:

position (int) – The position of the producer

Returns:

The producer at the given position

Return type:

Producer | ProducerGroup

invalid_inputs(inputs, outputs)[source]

Helper function used to check, if for a list of inputs, a match in a list of outputs can be found.

Parameters:
  • inputs (List[Quantity]) – The list of quantity inputs to check

  • outputs (List[Quantity]) – The list of quantity outputs to check

Returns:

A list of inputs, that are not in the outputs list

Return type:

wrong_inputs

relocate_producer(producer, old_position, new_position)[source]

Function used to relocate a producer to a given position.

Parameters:
  • producer (Producer | ProducerGroup) – The producer to relocate

  • old_position (int) – The old position of the producer in the ordering

  • new_position (int) – The new position of the producer in the ordering

Return type:

None