artist.optim.aim_point_optimizer ================================ .. py:module:: artist.optim.aim_point_optimizer Attributes ---------- .. autoapisummary:: artist.optim.aim_point_optimizer.log Classes ------- .. autoapisummary:: artist.optim.aim_point_optimizer.AimPointOptimizer Module Contents --------------- .. py:data:: log A logger for the aim point optimizer. .. py:class:: AimPointOptimizer(ddp_setup: artist.util.env.DdpSetup, scenario: artist.scenario.scenario.Scenario, optimization_configuration: dict[str, Any], incident_ray_direction: torch.Tensor, target_area_index: int, ground_truth: torch.Tensor, dni: float, bitmap_resolution: torch.Tensor = torch.tensor([256, 256]), epsilon: float = 1e-12, device: torch.device | None = None) Initialize the aim points optimizer. Parameters ---------- ddp_setup : DdpSetup Information about the distributed environment, process groups, devices, ranks, world size, and heliostat-group-to-ranks mapping. scenario : Scenario The scenario. optimization_configuration : dict[str, Any] Parameters for the optimizer, learning rate scheduler, regularizers, and early stopping. incident_ray_direction : torch.Tensor Incident ray direction during the optimization. Shape is ``[4]``. target_area_index : int Index of the target used for the optimization. ground_truth : torch.Tensor Desired focal spot or distribution. Shape is ``[4]`` or ``[bitmap_resolution_e, bitmap_resolution_u]``. dni : float Direct normal irradiance in W/m^2. bitmap_resolution : torch.Tensor Resolution of all bitmaps during optimization (default is ``torch.tensor([256,256])``). Shape is ``[2]``. epsilon : float A small value to avoid division by zero (default is 1e-12). device : torch.device | None The device on which to perform computations or load tensors and models (default is None). If None, ``ARTIST`` will automatically select the most appropriate device (CUDA or CPU) based on availability and OS. .. py:attribute:: ddp_setup .. py:attribute:: scenario .. py:attribute:: optimizer_dict .. py:attribute:: scheduler_dict .. py:attribute:: constraint_dict .. py:attribute:: incident_ray_direction .. py:attribute:: target_area_index .. py:attribute:: ground_truth .. py:attribute:: dni .. py:attribute:: bitmap_resolution .. py:attribute:: epsilon :value: 1e-12 .. py:method:: _initialize_group_parameters(device: torch.device) -> tuple[list[torch.nn.Parameter], list[torch.Tensor], list[torch.Tensor], list[torch.Tensor], list[torch.Tensor], list[torch.Tensor], torch.Tensor] Pre-align all heliostat groups and set up their reparameterized motor positions. Each group is aligned once to the given incident ray direction and target to obtain the initial motor positions. The optimizable parameter is a zero-initialized reparameterization of the motor positions (see :meth:`optimize`). Parameters ---------- device : torch.device The device on which to perform computations or load tensors and models. Returns ------- list[torch.nn.Parameter] The optimizable reparameterized motor position parameters per group. list[torch.Tensor] The reparameterization scales per group. list[torch.Tensor] The initial motor positions per group. list[torch.Tensor] The active heliostats masks per group. list[torch.Tensor] The target area indices per group. list[torch.Tensor] The incident ray directions per group. torch.Tensor Offsets mapping group-local heliostat indices to global-flat index positions. .. py:method:: _setup_optimizer_scheduler_early_stopping(optimizable_parameters_all_groups: list[torch.nn.Parameter]) -> tuple[torch.optim.Optimizer, torch.optim.lr_scheduler.LRScheduler, artist.optim.training.EarlyStopping] Create the optimizer, learning rate scheduler, and early stopping. Parameters ---------- optimizable_parameters_all_groups : list[torch.nn.Parameter] The optimizable reparameterized motor position parameters per group. Returns ------- torch.optim.Optimizer The Adam optimizer over all group parameter tensors. LRScheduler The learning rate scheduler. training.EarlyStopping The early stopping monitor. .. py:method:: _get_target_plane_dimensions(device: torch.device) -> torch.Tensor Determine the target plane dimensions for the optimization target. Handles both planar and cylindrical target areas. Parameters ---------- device : torch.device The device on which to perform computations or load tensors and models. Returns ------- torch.Tensor The width and height of the target plane. Shape is ``[2]``. .. py:method:: _align_all_groups(optimizer: torch.optim.Optimizer, initial_motor_positions_all_groups: list[torch.Tensor], scales_all_groups: list[torch.Tensor], active_heliostats_masks_all_groups: list[torch.Tensor], device: torch.device) -> None Align all heliostat groups on this rank from the reparameterized motor positions. The true motor positions are reconstructed from the ``tanh``-reparameterized parameters. Parameters ---------- optimizer : torch.optim.Optimizer The optimizer holding the reparameterized motor position parameters. initial_motor_positions_all_groups : list[torch.Tensor] The initial motor positions per group. scales_all_groups : list[torch.Tensor] The reparameterization scales per group. active_heliostats_masks_all_groups : list[torch.Tensor] The active heliostats masks per group. device : torch.device The device on which to perform computations or load tensors and models. .. py:method:: _trace_and_accumulate_flux(active_heliostats_masks_all_groups: list[torch.Tensor], target_area_indices_all_groups: list[torch.Tensor], incident_ray_directions_all_groups: list[torch.Tensor], group_offsets: torch.Tensor, device: torch.device) -> tuple[torch.Tensor, torch.Tensor, torch.Tensor, torch.Tensor] Ray trace all heliostat groups on this rank and accumulate the flux on the target. Parameters ---------- active_heliostats_masks_all_groups : list[torch.Tensor] The active heliostats masks per group. target_area_indices_all_groups : list[torch.Tensor] The target area indices per group. incident_ray_directions_all_groups : list[torch.Tensor] The incident ray directions per group. group_offsets : torch.Tensor Offsets mapping group-local heliostat indices to global-flat index positions. device : torch.device The device on which to perform computations or load tensors and models. Returns ------- torch.Tensor The accumulated flux distribution on the target. torch.Tensor The intercept factors per heliostat. torch.Tensor The on-target factors per heliostat. torch.Tensor The blocking factors per heliostat. .. py:method:: _compute_kl_constraints(flux_loss: torch.Tensor, total_flux: torch.Tensor, intercept_factors: torch.Tensor, epoch: int, flux_integral_reference: torch.Tensor | float, intercept_factors_reference: torch.Tensor | float, lambda_flux_integral: torch.Tensor | float, lambda_intercept: torch.Tensor | float, lambda_local_flux: torch.Tensor | float, rho_flux_integral: float, rho_intercept: float, rho_local_flux: float, max_flux_density_per_pixel: torch.Tensor) -> tuple[torch.Tensor, torch.Tensor, torch.Tensor, torch.Tensor, torch.Tensor | float, torch.Tensor | float, torch.Tensor | float, torch.Tensor | float, torch.Tensor | float] Compute the Augmented-Lagrangian constraints and the total loss for a KL-divergence loss. Three constraints are added: one that maximizes the flux integral, one that maximizes the intercept factor, and one that constrains the local maximum intensity. The Augmented-Lagrangian multipliers are updated and returned so that their state persists across epochs. Parameters ---------- flux_loss : torch.Tensor The flux loss between predicted and target flux. total_flux : torch.Tensor The accumulated flux distribution on the target. intercept_factors : torch.Tensor The intercept factors per heliostat. epoch : int The current epoch. References are captured in the first epoch. flux_integral_reference : torch.Tensor | float The reference flux integral captured in the first epoch. intercept_factors_reference : torch.Tensor | float The reference intercept factors captured in the first epoch. lambda_flux_integral : torch.Tensor | float The current flux integral multiplier. lambda_intercept : torch.Tensor | float The current intercept multiplier. lambda_local_flux : torch.Tensor | float The current local flux multiplier. rho_flux_integral : float The penalty parameter of the flux integral constraint. rho_intercept : float The penalty parameter of the intercept constraint. rho_local_flux : float The penalty parameter of the local flux constraint. max_flux_density_per_pixel : torch.Tensor The maximum allowed flux density per pixel. Returns ------- torch.Tensor The total loss. torch.Tensor The flux integral constraint. torch.Tensor The intercept factor constraint. torch.Tensor The local flux constraint. torch.Tensor | float The (possibly updated) reference flux integral. torch.Tensor | float The (possibly updated) reference intercept factors. torch.Tensor | float The updated flux integral multiplier. torch.Tensor | float The updated intercept multiplier. torch.Tensor | float The updated local flux multiplier. .. py:method:: _synchronize_distributed_gradients(optimizer: torch.optim.Optimizer) -> None Reduce and average gradients across all ranks in the global process group. This is a no-op when not running in distributed mode. Parameters ---------- optimizer : torch.optim.Optimizer The optimizer whose parameter gradients are synchronized. .. py:method:: _broadcast_motor_positions() -> None Broadcast the final motor positions of each heliostat group from its source rank. This is a no-op when not running in distributed mode. .. py:method:: optimize(loss_definition: artist.optim.loss.Loss, device: torch.device | None = None) -> tuple[torch.Tensor, dict[str, list], torch.Tensor, torch.Tensor, torch.Tensor] Optimize the motor positions for optimal aim points. The motor positions are optimized through a reparameterization to ensure stable training across different heliostats with widely varying initial motor positions and ranges. Motor positions can range from 0 to up to ~80000. Instead of directly optimizing the absolute motor positions, which can differ in magnitudes, an unconstrained parameter is optimized. Directly optimizing the absolute motor positions would have very different effects depending on the scale of the motors. For small initial motor positions (e.g. ~100), a gradient update of size 10 may cause a ~10% relative change, drastically altering the motor positions of this heliostat. For large initial motor positions (e.g. ~50000), the same optimizer step would correspond to only a 0.02% relative change in motor positions, effectively freezing the optimization of this heliostat. This mismatch makes it impossible to choose a single learning rate that works robustly across all heliostats. Reparameterizing the motor positions to be optimized defines the optimizable parameter as: .. math:: \text{motor\_positions\_optimized} = \tanh( \text{torch.nn.Parameter(optimizable\_parameter)} ) The true motor positions can be reconstructed by: .. math:: \text{motor\_positions} = \text{initial\_motor\_positions} + \text{motor\_positions\_normalized} \cdot \text{scale} where scale defines the range (e.g. up to ~80000) for adjustments. By optimizing reparameterized instead of raw motor positions, every heliostat sees updates of comparable relative magnitude, regardless of the absolute size of its motors positions. Parameters ---------- loss_definition : Loss The definition of the loss function and pre-processing of the prediction. device : torch.device | None The device on which to perform computations or load tensors and models (default is None). If None, ``ARTIST`` will automatically select the most appropriate device (CUDA or CPU) based on availability and OS. Returns ------- torch.Tensor Final loss of the aim point optimization. dict[str, list] Loss history over epochs, with keys ``"total_loss"``, ``"flux_loss"``, ``"local_flux_constraint"``, ``"intercept_constraint"``, ``"flux_integral_constraint"``, and ``"flux_integral"``. Each value is a list of per-epoch scalar floats. torch.Tensor Final intercept factors for each heliostat. torch.Tensor Final fraction of rays hitting the target, neglecting blocking effects, for each heliostat. torch.Tensor Final fraction of rays not being blocked, for each heliostat.