opengen.tcp package

Submodules

opengen.tcp.optimizer_tcp_manager module

class opengen.tcp.optimizer_tcp_manager.OptimizerTcpManager(optimizer_path=None, ip=None, port=None)

Bases: object

Client for TCP interface of parametric optimizers

This class is used to start and stop a TCP server, which has been generated by opengen.

__init__(optimizer_path=None, ip=None, port=None)

Constructs instance of OptimizerTcpManager

There are three ways to use this constructor:

  • OptimizerTcpManager(optimizer_path): creates a TCP manager for a local TCP server using the default IP and port of that TCP server (specified upon code generation)

  • OptimizerTcpManager(optimizer_path, ip, port): creates a TCP manager for a local TCP server, but overrides the default IP and port. This way the user can set the address 0.0.0.0, so that the TCP server binds on all IPs, or 127.0.0.1 so that it is accessible only locally, or a VPN IP address, so that the optimizer is accessible only over a private network.

  • OptimizerTcpManager(ip, port): If a path is not provided, then the TCP manager can be used to connect to a remote TCP server, as a client, but cannot be used to start the server.

Parameters:
  • optimizer_path (str) – path to auto-generated optimizer (just to be clear: this is the folder that contains optimizer.yml)

  • ip (str) – the user can provide the IP of a remote TCP server (must be up and running) so as to establish a remote connection. In that case path must be equal to None (see examples above)

  • port (int) – see ip

Returns:

New instance of OptimizerTcpManager

call(p, initial_guess=None, initial_y=None, initial_penalty=None, buffer_len=4096, max_data_size=1048576) SolverResponse

Calls the server

Consumes the parametric optimizer by providing a parameter vector and, optionally, an initial guess

Parameters:
  • p (list of float) – vector of parameters

  • initial_guess (list of float) – initial guess vector

  • initial_y (list of float) – initial vector of Lagrange multipliers

  • initial_penalty (float) – initial penalty parameter

  • buffer_len (int) – buffer length used to read the server response, defaults to 4096

  • max_data_size (int) – maximum data size that is expected to be received from the TCP server, defaults to 1048576

Returns:

SolverResponse object

Return type:

SolverResponse

property details
kill()

Kills the server

ping()

Pings the server

Pings the server to check whether it is up and running

start()

Starts the TCP server

Note: this method starts a local server whose path must have been provided - we cannot start a remote server.

The server starts on a separate thread, so this method does not block the execution of the caller’s programme.

opengen.tcp.solver_error module

class opengen.tcp.solver_error.SolverError(error)

Bases: object

Class for storing solver status in the event of an error.

__init__(error)

Constructs instance of SolverError

Parameters:

error – dictionary containing error attributes

Returns:

New instance of SolverError

property code

Error code

Possible error codes are:

  • 1000: Invalid request: Malformed or invalid JSON

  • 1600: Initial guess has incomplete dimensions

  • 1700: Wrong dimension of Lagrange multipliers

  • 2000: Problem solution failed (solver error)

  • 3003: Parameter vector has wrong length

Returns:

Error code

property message

Returns an appropriate error message matching the error code

Returns:

Error message

Return type:

str

opengen.tcp.solver_response module

class opengen.tcp.solver_response.SolverResponse(d)

Bases: object

Stores a solver response of type SolverStatus or SolverError.

__init__(d)

Constructs instance of SolverResponse

This constructor is not to be used by the end-user

Parameters:

d (dictionary) – dictionary containing either status or error attributes

Returns:

New instance of SolverResponse

get()

Returns response, which is an instance of SolverStatus, if the call was successful, or an instance of SolverError otherwise. It is recommended that you use is_ok to check whether the call has succeeded first

is_ok()

Determines if response is OK.

This method should always be called first when obtaining a server response.

opengen.tcp.solver_status module

class opengen.tcp.solver_status.SolverStatus(status)

Bases: object

Class for storing the status of the solver.

__init__(status)

Constructs instance of SolverStatus

This constructor should not be called by the end-user.

Parameters:

status – dictionary containing solver status attributes

property cost

Cost at the solution

Returns:

Value of cost function at the solution

property exit_status

Solver status

The solver status can be:

  • Converged if the solver has converged

  • NotConvergedIterations if the maximum number of outer or total inner

    iterations was reached

  • NotConvergedOutOfTime if the solver did not have enough time to converge

Returns:

The exit status of the solver

property f1_infeasibility

Returns the infeasibility of the ALM constraints, that is, it returns

\(\mathrm{dist}_C(F_1(u))\)

Returns:

Infeasibility of the constraint F1(u,p) in C

property f2_norm

Infeasibility of PM constraints

Returns:

Euclidean norm of \(\|F_2(u)\|\) at the solution

property lagrange_multipliers

Lagrange multipliers at the solution

Returns:

Vector of Lagrange multipliers

property last_problem_norm_fpr

Returns the infinity norm of the fixed-point residual of the last inner optimization problem

Returns:

inf-norm of the FPR of the last inner problem

property num_inner_iterations

Returns the total number of inner iterations for all inner problems

Returns:

The total number of inner iterations

property num_outer_iterations

Returns the number of outer (ALM/PM) iterations of the algorithm

Returns:

The number of outer iterations

property penalty

Last penalty at the solution :return: Last value of the penalty parameter

property solution

Solution :return: Solution vector :rtype: list of float

property solve_time_ms

Solver time :return: Total execution time in milliseconds

Module contents