

from typing import Optional, Tuple, Union

import flax
import flax.linen as nn
import jax
import jax.numpy as jnp
from flax.core.frozen_dict import FrozenDict

from ..configuration_utils import ConfigMixin, flax_register_to_config
from ..utils import BaseOutput
from .embeddings_flax import FlaxTimestepEmbedding, FlaxTimesteps
from .modeling_flax_utils import FlaxModelMixin
from .unet_ad_blocks_flax import (
    FlaxCrossAttnDownBlockaD,
    FlaxDownBlockaD,
    FlaxUNetMidBlockaDCrossAttn,
)






@flax.struct.dataclass
class 	UpperCAmelCase__		(	A__	):






  """simple docstring"""


  a									= 42
  a									= 42






class 	UpperCAmelCase__		(	nn.Module	):






  """simple docstring"""


  a									= 42
  a									= (16, 32, 96, 2_56)
  a									= jnp.floataa






  def 				lowercase_		(							self						:		Tuple )		->							Optional[Any]:
       SCREAMING_SNAKE_CASE__   =   nn.Conv(
           self.block_out_channels[0]     ,       kernel_size=(3, 3)     ,       padding=((1, 1), (1, 1))     ,       dtype=self.dtype     ,       )

       SCREAMING_SNAKE_CASE__   =   []
       for i in range(len(self.block_out_channels ) - 1 ):
            SCREAMING_SNAKE_CASE__   =   self.block_out_channels[i]
            SCREAMING_SNAKE_CASE__   =   self.block_out_channels[i + 1]
            SCREAMING_SNAKE_CASE__   =   nn.Conv(
                __lowerCamelCase     ,       kernel_size=(3, 3)     ,       padding=((1, 1), (1, 1))     ,       dtype=self.dtype     ,       )
            blocks.append(__lowerCamelCase )
            SCREAMING_SNAKE_CASE__   =   nn.Conv(
                __lowerCamelCase     ,       kernel_size=(3, 3)     ,       strides=(2, 2)     ,       padding=((1, 1), (1, 1))     ,       dtype=self.dtype     ,       )
            blocks.append(__lowerCamelCase )
       SCREAMING_SNAKE_CASE__   =   blocks

       SCREAMING_SNAKE_CASE__   =   nn.Conv(
           self.conditioning_embedding_channels     ,       kernel_size=(3, 3)     ,       padding=((1, 1), (1, 1))     ,       kernel_init=nn.initializers.zeros_init()     ,       bias_init=nn.initializers.zeros_init()     ,       dtype=self.dtype     ,       )







  def __call__(							self						:		Any     ,       __lowerCamelCase						:		Optional[Any] )		->							List[str]:
       SCREAMING_SNAKE_CASE__   =   self.conv_in(__lowerCamelCase )
       SCREAMING_SNAKE_CASE__   =   nn.silu(__lowerCamelCase )

       for block in self.blocks:
            SCREAMING_SNAKE_CASE__   =   block(__lowerCamelCase )
            SCREAMING_SNAKE_CASE__   =   nn.silu(__lowerCamelCase )

       SCREAMING_SNAKE_CASE__   =   self.conv_out(__lowerCamelCase )

       return embedding






@flax_register_to_config
class 	UpperCAmelCase__		(	nn.Module						,					A__						,					A__	):






  """simple docstring"""


  a									= 32
  a									= 4
  a									= (
      "CrossAttnDownBlock2D",
      "CrossAttnDownBlock2D",
      "CrossAttnDownBlock2D",
      "DownBlock2D",
  )
  a									= False
  a									= (3_20, 6_40, 12_80, 12_80)
  a									= 2
  a									= 8
  a									= None
  a									= 12_80
  a									= 0.0
  a									= False
  a									= jnp.floataa
  a									= True
  a									= 0
  a									= "rgb"
  a									= (16, 32, 96, 2_56)






  def 				lowercase_		(							self						:		Any     ,       __lowerCamelCase						:		jax.random.KeyArray )		->							FrozenDict:
       # init input tensors
       SCREAMING_SNAKE_CASE__   =   (1, self.in_channels, self.sample_size, self.sample_size)
       SCREAMING_SNAKE_CASE__   =   jnp.zeros(__lowerCamelCase     ,       dtype=jnp.floataa )
       SCREAMING_SNAKE_CASE__   =   jnp.ones((1,)     ,       dtype=jnp.intaa )
       SCREAMING_SNAKE_CASE__   =   jnp.zeros((1, 1, self.cross_attention_dim)     ,       dtype=jnp.floataa )
       SCREAMING_SNAKE_CASE__   =   (1, 3, self.sample_size * 8, self.sample_size * 8)
       SCREAMING_SNAKE_CASE__   =   jnp.zeros(__lowerCamelCase     ,       dtype=jnp.floataa )

       SCREAMING_SNAKE_CASE__,SCREAMING_SNAKE_CASE__   =   jax.random.split(__lowerCamelCase )
       SCREAMING_SNAKE_CASE__   =   {'''params''': params_rng, '''dropout''': dropout_rng}

       return self.init(__lowerCamelCase     ,       __lowerCamelCase     ,       __lowerCamelCase     ,       __lowerCamelCase     ,       __lowerCamelCase )["params"]






  def 				lowercase_		(							self						:		Any )		->							List[Any]:
       SCREAMING_SNAKE_CASE__   =   self.block_out_channels
       SCREAMING_SNAKE_CASE__   =   block_out_channels[0] * 4

       # If `num_attention_heads` is not defined (which is the case for most models)
       # it will default to `attention_head_dim`. This looks weird upon first reading it and it is.
       # The reason for this behavior is to correct for incorrectly named variables that were introduced
       # when this library was created. The incorrect naming was only discovered much later in https://github.com/huggingface/diffusers/issues/2011#issuecomment-1547958131
       # Changing `attention_head_dim` to `num_attention_heads` for 40,000+ configurations is too backwards breaking
       # which is why we correct for the naming here.
       SCREAMING_SNAKE_CASE__   =   self.num_attention_heads or self.attention_head_dim

       # input
       SCREAMING_SNAKE_CASE__   =   nn.Conv(
           block_out_channels[0]     ,       kernel_size=(3, 3)     ,       strides=(1, 1)     ,       padding=((1, 1), (1, 1))     ,       dtype=self.dtype     ,       )

       # time
       SCREAMING_SNAKE_CASE__   =   FlaxTimesteps(
           block_out_channels[0]     ,       flip_sin_to_cos=self.flip_sin_to_cos     ,       freq_shift=self.config.freq_shift )
       SCREAMING_SNAKE_CASE__   =   FlaxTimestepEmbedding(__lowerCamelCase     ,       dtype=self.dtype )

       SCREAMING_SNAKE_CASE__   =   FlaxControlNetConditioningEmbedding(
           conditioning_embedding_channels=block_out_channels[0]     ,       block_out_channels=self.conditioning_embedding_out_channels     ,       )

       SCREAMING_SNAKE_CASE__   =   self.only_cross_attention
       if isinstance(__lowerCamelCase     ,       __lowerCamelCase ):
            SCREAMING_SNAKE_CASE__   =   (only_cross_attention,) * len(self.down_block_types )

       if isinstance(__lowerCamelCase     ,       __lowerCamelCase ):
            SCREAMING_SNAKE_CASE__   =   (num_attention_heads,) * len(self.down_block_types )

       # down
       SCREAMING_SNAKE_CASE__   =   []
       SCREAMING_SNAKE_CASE__   =   []

       SCREAMING_SNAKE_CASE__   =   block_out_channels[0]

       SCREAMING_SNAKE_CASE__   =   nn.Conv(
           __lowerCamelCase     ,       kernel_size=(1, 1)     ,       padding='''VALID'''     ,       kernel_init=nn.initializers.zeros_init()     ,       bias_init=nn.initializers.zeros_init()     ,       dtype=self.dtype     ,       )
       controlnet_down_blocks.append(__lowerCamelCase )

       for i, down_block_type in enumerate(self.down_block_types ):
            SCREAMING_SNAKE_CASE__   =   output_channel
            SCREAMING_SNAKE_CASE__   =   block_out_channels[i]
            SCREAMING_SNAKE_CASE__   =   i == len(__lowerCamelCase ) - 1

            if down_block_type == "CrossAttnDownBlock2D":
                 SCREAMING_SNAKE_CASE__   =   FlaxCrossAttnDownBlockaD(
                     in_channels=__lowerCamelCase     ,       out_channels=__lowerCamelCase     ,       dropout=self.dropout     ,       num_layers=self.layers_per_block     ,       num_attention_heads=num_attention_heads[i]     ,       add_downsample=not is_final_block     ,       use_linear_projection=self.use_linear_projection     ,       only_cross_attention=only_cross_attention[i]     ,       dtype=self.dtype     ,       )
            else:
                 SCREAMING_SNAKE_CASE__   =   FlaxDownBlockaD(
                     in_channels=__lowerCamelCase     ,       out_channels=__lowerCamelCase     ,       dropout=self.dropout     ,       num_layers=self.layers_per_block     ,       add_downsample=not is_final_block     ,       dtype=self.dtype     ,       )

            down_blocks.append(__lowerCamelCase )

            for _ in range(self.layers_per_block ):
                 SCREAMING_SNAKE_CASE__   =   nn.Conv(
                     __lowerCamelCase     ,       kernel_size=(1, 1)     ,       padding='''VALID'''     ,       kernel_init=nn.initializers.zeros_init()     ,       bias_init=nn.initializers.zeros_init()     ,       dtype=self.dtype     ,       )
                 controlnet_down_blocks.append(__lowerCamelCase )

            if not is_final_block:
                 SCREAMING_SNAKE_CASE__   =   nn.Conv(
                     __lowerCamelCase     ,       kernel_size=(1, 1)     ,       padding='''VALID'''     ,       kernel_init=nn.initializers.zeros_init()     ,       bias_init=nn.initializers.zeros_init()     ,       dtype=self.dtype     ,       )
                 controlnet_down_blocks.append(__lowerCamelCase )

       SCREAMING_SNAKE_CASE__   =   down_blocks
       SCREAMING_SNAKE_CASE__   =   controlnet_down_blocks

       # mid
       SCREAMING_SNAKE_CASE__   =   block_out_channels[-1]
       SCREAMING_SNAKE_CASE__   =   FlaxUNetMidBlockaDCrossAttn(
           in_channels=__lowerCamelCase     ,       dropout=self.dropout     ,       num_attention_heads=num_attention_heads[-1]     ,       use_linear_projection=self.use_linear_projection     ,       dtype=self.dtype     ,       )

       SCREAMING_SNAKE_CASE__   =   nn.Conv(
           __lowerCamelCase     ,       kernel_size=(1, 1)     ,       padding='''VALID'''     ,       kernel_init=nn.initializers.zeros_init()     ,       bias_init=nn.initializers.zeros_init()     ,       dtype=self.dtype     ,       )







  def __call__(							self						:		List[str]     ,       __lowerCamelCase						:		List[str]     ,       __lowerCamelCase						:		Any     ,       __lowerCamelCase						:		Dict     ,       __lowerCamelCase						:		int     ,       __lowerCamelCase						:		float = 1.0     ,       __lowerCamelCase						:		bool = True     ,       __lowerCamelCase						:		bool = False     ,       )		->							Union[FlaxControlNetOutput, Tuple]:
       SCREAMING_SNAKE_CASE__   =   self.controlnet_conditioning_channel_order
       if channel_order == "bgr":
            SCREAMING_SNAKE_CASE__   =   jnp.flip(__lowerCamelCase     ,       axis=1 )

       # 1. time
       if not isinstance(__lowerCamelCase     ,       jnp.ndarray ):
            SCREAMING_SNAKE_CASE__   =   jnp.array([timesteps]     ,       dtype=jnp.intaa )
       elif isinstance(__lowerCamelCase     ,       jnp.ndarray ) and len(timesteps.shape ) == 0:
            SCREAMING_SNAKE_CASE__   =   timesteps.astype(dtype=jnp.floataa )
            SCREAMING_SNAKE_CASE__   =   jnp.expand_dims(__lowerCamelCase     ,       0 )

       SCREAMING_SNAKE_CASE__   =   self.time_proj(__lowerCamelCase )
       SCREAMING_SNAKE_CASE__   =   self.time_embedding(__lowerCamelCase )

       # 2. pre-process
       SCREAMING_SNAKE_CASE__   =   jnp.transpose(__lowerCamelCase     ,       (0, 2, 3, 1) )
       SCREAMING_SNAKE_CASE__   =   self.conv_in(__lowerCamelCase )

       SCREAMING_SNAKE_CASE__   =   jnp.transpose(__lowerCamelCase     ,       (0, 2, 3, 1) )
       SCREAMING_SNAKE_CASE__   =   self.controlnet_cond_embedding(__lowerCamelCase )
       sample += controlnet_cond

       # 3. down
       SCREAMING_SNAKE_CASE__   =   (sample,)
       for down_block in self.down_blocks:
            if isinstance(__lowerCamelCase     ,       __lowerCamelCase ):
                 SCREAMING_SNAKE_CASE__,SCREAMING_SNAKE_CASE__   =   down_block(__lowerCamelCase     ,       __lowerCamelCase     ,       __lowerCamelCase     ,       deterministic=not train )
            else:
                 SCREAMING_SNAKE_CASE__,SCREAMING_SNAKE_CASE__   =   down_block(__lowerCamelCase     ,       __lowerCamelCase     ,       deterministic=not train )
            down_block_res_samples += res_samples

       # 4. mid
       SCREAMING_SNAKE_CASE__   =   self.mid_block(__lowerCamelCase     ,       __lowerCamelCase     ,       __lowerCamelCase     ,       deterministic=not train )

       # 5. contronet blocks
       SCREAMING_SNAKE_CASE__   =   ()
       for down_block_res_sample, controlnet_block in zip(__lowerCamelCase     ,       self.controlnet_down_blocks ):
            SCREAMING_SNAKE_CASE__   =   controlnet_block(__lowerCamelCase )
            controlnet_down_block_res_samples += (down_block_res_sample,)

       SCREAMING_SNAKE_CASE__   =   controlnet_down_block_res_samples

       SCREAMING_SNAKE_CASE__   =   self.controlnet_mid_block(__lowerCamelCase )

       # 6. scaling
       SCREAMING_SNAKE_CASE__   =   [sample * conditioning_scale for sample in down_block_res_samples]
       mid_block_res_sample *= conditioning_scale

       if not return_dict:
            return (down_block_res_samples, mid_block_res_sample)

       return FlaxControlNetOutput(
           down_block_res_samples=__lowerCamelCase     ,       mid_block_res_sample=__lowerCamelCase )





import argparse
import copy
def     UpperCAmelCase_       (						_A    ):





     '''simple docstring'''




     SCREAMING_SNAKE_CASE__   =   {}

     with open(_A    ) as f:
          for line in f:
               if line.split()[0] not in dict_of_neighbours:
                    SCREAMING_SNAKE_CASE__   =   []
                    _list.append([line.split()[1], line.split()[2]]    )
                    SCREAMING_SNAKE_CASE__   =   _list
               else:
                    dict_of_neighbours[line.split()[0]].append(
                        [line.split()[1], line.split()[2]]    )
               if line.split()[1] not in dict_of_neighbours:
                    SCREAMING_SNAKE_CASE__   =   []
                    _list.append([line.split()[0], line.split()[2]]    )
                    SCREAMING_SNAKE_CASE__   =   _list
               else:
                    dict_of_neighbours[line.split()[1]].append(
                        [line.split()[0], line.split()[2]]    )

     return dict_of_neighbours
def     UpperCAmelCase_       (						_A				,		_A    ):





     '''simple docstring'''




     with open(_A    ) as f:
          SCREAMING_SNAKE_CASE__   =   f.read(1    )
     SCREAMING_SNAKE_CASE__   =   start_node

     SCREAMING_SNAKE_CASE__   =   []

     SCREAMING_SNAKE_CASE__   =   start_node

     SCREAMING_SNAKE_CASE__   =   0
     while visiting not in first_solution:
          SCREAMING_SNAKE_CASE__   =   1_00_00
          for k in dict_of_neighbours[visiting]:
               if int(k[1]    ) < int(_A    ) and k[0] not in first_solution:
                    SCREAMING_SNAKE_CASE__   =   k[1]
                    SCREAMING_SNAKE_CASE__   =   k[0]

          first_solution.append(_A    )
          SCREAMING_SNAKE_CASE__   =   distance_of_first_solution + int(_A    )
          SCREAMING_SNAKE_CASE__   =   best_node

     first_solution.append(_A    )

     SCREAMING_SNAKE_CASE__   =   0
     for k in dict_of_neighbours[first_solution[-2]]:
          if k[0] == start_node:
               break
          position += 1

     SCREAMING_SNAKE_CASE__   =   (
         distance_of_first_solution
         + int(dict_of_neighbours[first_solution[-2]][position][1]    )
         - 1_00_00
     )
     return first_solution, distance_of_first_solution
def     UpperCAmelCase_       (						_A				,		_A    ):





     '''simple docstring'''




     SCREAMING_SNAKE_CASE__   =   []

     for n in solution[1:-1]:
          SCREAMING_SNAKE_CASE__   =   solution.index(_A    )
          for kn in solution[1:-1]:
               SCREAMING_SNAKE_CASE__   =   solution.index(_A    )
               if n == kn:
                    continue

               SCREAMING_SNAKE_CASE__   =   copy.deepcopy(_A    )
               SCREAMING_SNAKE_CASE__   =   kn
               SCREAMING_SNAKE_CASE__   =   n

               SCREAMING_SNAKE_CASE__   =   0

               for k in _tmp[:-1]:
                    SCREAMING_SNAKE_CASE__   =   _tmp[_tmp.index(_A    ) + 1]
                    for i in dict_of_neighbours[k]:
                         if i[0] == next_node:
                              SCREAMING_SNAKE_CASE__   =   distance + int(i[1]    )
               _tmp.append(_A    )

               if _tmp not in neighborhood_of_solution:
                    neighborhood_of_solution.append(_tmp    )

     SCREAMING_SNAKE_CASE__   =   len(neighborhood_of_solution[0]    ) - 1

     neighborhood_of_solution.sort(key=lambda _A    : x[index_of_last_item_in_the_list]    )
     return neighborhood_of_solution
def     UpperCAmelCase_       (						_A				,		_A				,		_A				,		_A				,		_A    ):





     '''simple docstring'''




     SCREAMING_SNAKE_CASE__   =   1
     SCREAMING_SNAKE_CASE__   =   first_solution
     SCREAMING_SNAKE_CASE__   =   []
     SCREAMING_SNAKE_CASE__   =   distance_of_first_solution
     SCREAMING_SNAKE_CASE__   =   solution

     while count <= iters:
          SCREAMING_SNAKE_CASE__   =   find_neighborhood(_A				,		_A    )
          SCREAMING_SNAKE_CASE__   =   0
          SCREAMING_SNAKE_CASE__   =   neighborhood[index_of_best_solution]
          SCREAMING_SNAKE_CASE__   =   len(_A    ) - 1

          SCREAMING_SNAKE_CASE__   =   False
          while not found:
               SCREAMING_SNAKE_CASE__   =   0
               while i < len(_A    ):
                    if best_solution[i] != solution[i]:
                         SCREAMING_SNAKE_CASE__   =   best_solution[i]
                         SCREAMING_SNAKE_CASE__   =   solution[i]
                         break
                    SCREAMING_SNAKE_CASE__   =   i + 1

               if [first_exchange_node, second_exchange_node] not in tabu_list and [
                   second_exchange_node,
                   first_exchange_node,
               ] not in tabu_list:
                    tabu_list.append([first_exchange_node, second_exchange_node]    )
                    SCREAMING_SNAKE_CASE__   =   True
                    SCREAMING_SNAKE_CASE__   =   best_solution[:-1]
                    SCREAMING_SNAKE_CASE__   =   neighborhood[index_of_best_solution][best_cost_index]
                    if cost < best_cost:
                         SCREAMING_SNAKE_CASE__   =   cost
                         SCREAMING_SNAKE_CASE__   =   solution
               else:
                    SCREAMING_SNAKE_CASE__   =   index_of_best_solution + 1
                    SCREAMING_SNAKE_CASE__   =   neighborhood[index_of_best_solution]

          if len(_A    ) >= size:
               tabu_list.pop(0    )

          SCREAMING_SNAKE_CASE__   =   count + 1

     return best_solution_ever, best_cost
def     UpperCAmelCase_       (						_A=None    ):





     '''simple docstring'''




     SCREAMING_SNAKE_CASE__   =   generate_neighbours(args.File    )

     SCREAMING_SNAKE_CASE__,SCREAMING_SNAKE_CASE__   =   generate_first_solution(
         args.File				,		_A    )

     SCREAMING_SNAKE_CASE__,SCREAMING_SNAKE_CASE__   =   tabu_search(
         _A				,		_A				,		_A				,		args.Iterations				,		args.Size				,		)

     print(F'''Best solution: {best_sol}, with total distance: {best_cost}.'''    )


if __name__ == "__main__":
       _SCREAMING_SNAKE_CASE					:  Tuple             =      argparse.ArgumentParser(description='''Tabu Search''')
       parser.add_argument(
           '''-f''',
           '''--File''',
           type=str,
           help='''Path to the file containing the data''',
           required=True,
       )
       parser.add_argument(
           '''-i''',
           '''--Iterations''',
           type=int,
           help='''How many iterations the algorithm should perform''',
           required=True,
       )
       parser.add_argument(
           '''-s''', '''--Size''', type=int, help='''Size of the tabu list''', required=True
       )

       # Pass the arguments to main method
       main(parser.parse_args())


