# coding=utf-8
# Copyright 2021 DeepMind Technologies Limited.
#
# Licensed under the Apache License, Version 2.0 (the "License");
# you may not use this file except in compliance with the License.
# You may obtain a copy of the License at
#
#     http://www.apache.org/licenses/LICENSE-2.0
#
# Unless required by applicable law or agreed to in writing, software
# distributed under the License is distributed on an "AS IS" BASIS,
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
# See the License for the specific language governing permissions and
# limitations under the License.

# Lint as: python3
"""Facilities to construct SDP Verification problem instances."""

from typing import Callable, Dict, Mapping, Optional, Sequence, Tuple, Union

import jax.numpy as jnp
from jax_verify.extensions.sdp_verify import utils
from jax_verify.src import bound_propagation
from jax_verify.src import synthetic_primitives
import numpy as np


Bound = bound_propagation.Bound
Tensor = bound_propagation.Tensor
Index = bound_propagation.Index
Primitive = bound_propagation.Primitive
TransformContext = bound_propagation.TransformContext
SdpDualVerifInstance = utils.SdpDualVerifInstance


class SdpReluProblem:
  """SDP problem to optimise over a ReLU-based network."""

  def __init__(
      self,
      boundprop_transform: bound_propagation.BoundTransform,
      spec_fn: Callable[..., Tensor],
      *input_bounds: Bound,
  ):
    """Initialises a ReLU-based network SDP problem.

    Args:
      boundprop_transform: Transform to supply concrete bounds.
      spec_fn: Network to verify.
      *input_bounds: Concrete bounds on the network inputs.
    """
    self._output_node, self._env = bound_propagation.bound_propagation(
        bound_propagation.ForwardPropagationAlgorithm(
            _SdpTransform(boundprop_transform)),
        spec_fn, *input_bounds)

  def build_sdp_verification_instance(self) -> SdpDualVerifInstance: