curobo.opt.opt_base module

Base module for Optimization.

class curobo.opt.opt_base.OptimizerConfig(d_action, action_lows, action_highs, action_horizon, horizon, n_iters, cold_start_n_iters, rollout_fn, tensor_args, use_cuda_graph, store_debug, debug_info, n_problems, num_particles, sync_cuda_time, use_coo_sparse)

Bases: object

Configuration for an Optimizer.

Parameters:
  • d_action (int) –

  • action_lows (List[float]) –

  • action_highs (List[float]) –

  • action_horizon (int) –

  • horizon (int) –

  • n_iters (int) –

  • cold_start_n_iters (int | None) –

  • rollout_fn (RolloutBase) –

  • tensor_args (TensorDeviceType) –

  • use_cuda_graph (bool) –

  • store_debug (bool) –

  • debug_info (Any) –

  • n_problems (int) –

  • num_particles (int | None) –

  • sync_cuda_time (bool) –

  • use_coo_sparse (bool) –

d_action: int

Number of optimization variables per timestep.

action_lows: List[float]

Lower bound for optimization variables.

action_highs: List[float]

Higher bound for optimization variables

action_horizon: int
horizon: int

Number of timesteps in optimization state, total variables = d_action * horizon

n_iters: int

Number of iterations to run optimization

cold_start_n_iters: int | None

Number of iterations to run optimization during the first instance. Setting to None will use n_iters. This parameter is useful in MPC like settings where we need to run many iterations during initialization (cold start) and then run only few iterations (warm start).

rollout_fn: RolloutBase

Rollout function to use for computing cost, given optimization variables.

tensor_args: TensorDeviceType

Tensor device to use for optimization.

use_cuda_graph: bool

Capture optimization iteration in a cuda graph and replay graph instead of eager execution. Enabling this can make optimization 10x faster. But changing control flow, tensor shapes, or problem type is not allowed.

store_debug: bool

Record debugging data such as optimization variables, and cost at every iteration. Enabling this will disable cuda graph.

debug_info: Any

Use this to record additional attributes from rollouts.

n_problems: int

Number of parallel problems to optimize.

num_particles: int | None

Number of particles to use per problem. Common optimization solvers use many particles to optimize a single problem. E.g., MPPI rolls out many parallel samples and computes a weighted mean. In cuRobo, Quasi-Newton solvers use particles to run many line search magnitudes. Total optimization batch size = n_problems * num_particles.

sync_cuda_time: bool

Synchronize device before computing solver time.

use_coo_sparse: bool

Matmul with a Sparse tensor is used to create particles for each problem index to save memory and compute. Some versions of pytorch do not support coo sparse, specifically during torch profile runs. Set this to False to use a standard tensor.

static create_data_dict(data_dict, rollout_fn, tensor_args=TensorDeviceType(device=device(type='cuda', index=0), dtype=torch.float32), child_dict=None)

Helper function to create dictionary from optimizer parameters and rollout class.

Parameters:
  • data_dict (Dict) – optimizer parameters dictionary.

  • rollout_fn (RolloutBase) – rollout function.

  • tensor_args (TensorDeviceType) – tensor cuda device.

  • child_dict (Dict | None) – new dictionary where parameters will be stored.

Returns:

Dictionary with parameters to create a OptimizerConfig

class curobo.opt.opt_base.Optimizer(config=None)

Bases: OptimizerConfig

Base optimization solver class

Parameters:

config (Optional[OptimizerConfig]) – Initialized with parameters from a dataclass.

optimize(opt_tensor, shift_steps=0, n_iters=None)

Find a solution through optimization given the initial values for variables.

Parameters:
  • opt_tensor (Tensor) – Initial value of optimization variables. Shape: [n_problems, action_horizon, d_action]

  • shift_steps – Shift variables along action_horizon. Useful in mpc warm-start setting.

  • n_iters – Override number of iterations to run optimization.

Returns:

Optimized values returned as a tensor of shape [n_problems, action_horizon, d_action].

Return type:

Tensor

update_params(goal)

Update parameters in the curobo.rollout.rollout_base.RolloutBase instance.

Parameters:

goal (Goal) – parameters to update rollout instance.

reset()

Reset optimizer.

update_nproblems(n_problems)

Update the number of problems that need to be optimized.

Parameters:

n_problems (int) – number of problems.

get_nproblem_tensor(x)

This function takes an input tensor of shape (n_problem,….) and converts it into (n_particles,…).

reset_seed()

Reset seeds.

reset_cuda_graph()

Reset CUDA Graphs. This does not work, workaround is to create a new instance.

reset_shape()

Reset any flags in rollout class. Useful to reinitialize tensors for a new shape.

get_all_rollout_instances()

Get all instances of Rollout class in the optimizer.

Return type:

List[RolloutBase]

abstract _optimize(opt_tensor, shift_steps=0, n_iters=None)

Implement this function in a derived class containing the solver.

Parameters:
  • opt_tensor (Tensor) – Initial value of optimization variables. Shape: [n_problems, action_horizon, d_action]

  • shift_steps – Shift variables along action_horizon. Useful in mpc warm-start setting.

  • n_iters – Override number of iterations to run optimization.

Returns:

Optimized variables in tensor shape [action_horizon, d_action].

Return type:

Tensor

abstract _shift(shift_steps=0)

Shift the variables in the solver to hotstart the next timestep.

Parameters:

shift_steps – Number of timesteps to shift.

_update_problem_kernel(n_problems, num_particles)

Update matrix used to map problem index to number of particles.

Parameters:
  • n_problems (int) – Number of optimization problems.

  • num_particles (int) – Number of particles per problem.