parameters: (self, inds_con_route, inds_confict_groups, inds_merge_groups, prio,
                       ids_con_tls, inds_cons_conflict, inds_cons_merge,
                       phaseallocations, phaseconflicts, phaseblocks, phasepriorities,
                       ids_fromlane_tls,
                       ids_tolane_tls,
                       are_enabeled)
docstring: Allocates a phase for a particular route though the TLS.
          The route is represented by the connector indexes along the route.
docstring_summary: Allocates a phase for a particular route though the TLS.
          The route is represented by the connector indexes along the route.
docstring_tokens: Allocates a phase for a particular route though the TLS . The route is represented by the connector indexes along the route .
function: def add_timeslot(self, inds_con_route, inds_confict_groups, inds_merge_groups, prio,
                       ids_con_tls, inds_cons_conflict, inds_cons_merge,
                       phaseallocations, phaseconflicts, phaseblocks, phasepriorities,
                       ids_fromlane_tls,
                       ids_tolane_tls,
                       are_enabeled):
          """
          Allocates a phase for a particular route though the TLS.
          The route is represented by the connector indexes along the route.
          """
          print '\nadd_timeslot for', ids_con_tls[inds_con_route]
          # go through all connections used by this route and
          # signalize all conflicts
          # for id_con in ids_con_tls[inds_con_route]:
          #    #inds_con_conflict = inds_cons_conflict[ids_con]
          #    print '  check id_con',id_con
  
          slots, slots_blocked = self.init_slots(inds_con_route,  # all used connections
                                                 ids_con_tls,
                                                 inds_cons_conflict, inds_cons_merge,
                                                 ids_fromlane_tls,
                                                 ids_tolane_tls,
                                                 are_enabeled)
          n_slots = len(np.flatnonzero(slots))
          print '    n_slots', n_slots, 'n_phases', len(phaseconflicts)
  
          if n_slots == 0:
              print '  no  conflicts detected'
              pass
  
          elif len(phaseconflicts) == 0:
              print '  append first phase'
              phaseallocations.append(inds_con_route)
              phaseconflicts.append(slots)
              phaseblocks.append(slots_blocked)
              phasepriorities.append(prio)
          else:
              print '  search phase with minimum signal difference n_phases=', len(phaseallocations)
              n_diff_min = 10**8
              i_phase = 0
              i_phase_min = -1
              for inds_phaseallocation, inds_phaseconflict in zip(phaseallocations, phaseconflicts):
                  # check if slots overlap with allocated connections this phase
                  print '    compare phase', i_phase
                  print '      new  allocations', ids_con_tls[inds_con_route]
                  print '      phaseallocations', ids_con_tls[inds_phaseallocation]
                  print '      new    conflicts', ids_con_tls[slots == 2]
                  print '      phase  conflicts', ids_con_tls[inds_phaseconflict == 2]
                  # print '      allocations',ids_con_tls[inds_phaseallocation]
                  print '      n_diff =', np.sum(np.any((slots == 2) & inds_phaseallocation))
  
                  if not np.any((slots == 2) & inds_phaseallocation):
                      print '    no conflict in this phase go for a merge'
                      i_phase_min = i_phase
                      n_diff_min = -1  # indicate phase merge
  
                  else:
                      print '    there are conflicts with this phase...count'
                      n_diff = np.sum(np.any((slots == 2) & inds_phaseallocation))
                      #n_diff = np.sum(np.abs(slots - inds_phaseconflict)!=0)
                      # print '    ',inds_phaseconflict,n_diff
                      if n_diff < n_diff_min:
                          n_diff_min = n_diff
                          i_phase_min = i_phase
  
                  i_phase += 1
  
              print '    finished comparing phases i_phase_min,n_diff_min', i_phase_min, n_diff_min
  
              if n_diff_min == 0:
                  print '  already a phase with suitable signalling, nothing to do'
                  pass
  
              elif n_diff_min == -1:
                  print '  there are no phase conflicts, so merge'
                  phaseallocations[i_phase_min] = phaseallocations[i_phase_min] | inds_con_route
                  phaseconflicts[i_phase_min] = np.max([slots, phaseconflicts[i_phase_min]], 0)
                  phaseblocks[i_phase_min] = phaseblocks[i_phase_min] | slots_blocked
                  phasepriorities[i_phase_min] = max(prio, phasepriorities[i_phase_min])
  
              # elif n_diff_min>10**7:
              #    # all existing phases are conflicting
  
              else:
                  # get number of cons which are more restrictive signals
                  n_diff_pos = np.sum((slots - phaseconflicts[i_phase_min]) > 0)
  
                  # get number of cons which are less restrictive signals
                  n_diff_neg = np.sum((slots - phaseconflicts[i_phase_min]) < 0)
  
                  print '  n_diff_min', n_diff_min, 'n_diff_pos', n_diff_pos, 'n_diff_neg', n_diff_neg, 'i_phase_min', i_phase_min
                  # print '  inds_phaseconflict_min',ids_con_tls[phaseconflicts[i_phase_min] >0]
                  # print '  inds_phaseconflict',ids_con_tls[slots>0]
                  #inds_diff = np.abs(slots - inds_phaseconflict)!=0
                  # if (n_diff_pos>0) & (n_diff_neg == 0):
                  if (n_diff_pos >= n_diff_neg) & (n_diff_pos <= 2):
                      # only more restrictive
                      print '    put new phase after the phase with minimum difference'
                      phaseallocations.insert(i_phase_min+1, inds_con_route)
                      phaseconflicts.insert(i_phase_min+1, slots)
                      phaseblocks.insert(i_phase_min+1, slots_blocked)
                      phasepriorities.insert(i_phase_min+1, prio)
  
                  # elif (n_diff_pos==0) & (n_diff_neg > 0):
                  if (n_diff_pos < n_diff_neg) & (n_diff_neg <= 2):
                      # only less restrictive
                      print '    put new phase before the phase with minimum difference'
                      phaseallocations.insert(i_phase_min, inds_con_route)
                      phaseconflicts.insert(i_phase_min, slots)
                      phaseblocks.insert(i_phase_min, slots_blocked)
                      phasepriorities.insert(i_phase_min, prio)
  
                  else:
                      # mixed changes
                      print '    append en entirely new phase'
                      phaseconflicts.append(slots)
                      phaseblocks.append(slots_blocked)
                      phasepriorities.append(prio)
function_tokens: def add_timeslot ( self , inds_con_route , inds_confict_groups , inds_merge_groups , prio , ids_con_tls , inds_cons_conflict , inds_cons_merge , phaseallocations , phaseconflicts , phaseblocks , phasepriorities , ids_fromlane_tls , ids_tolane_tls , are_enabeled ) : print '\nadd_timeslot for' , ids_con_tls [ inds_con_route ] # go through all connections used by this route and # signalize all conflicts # for id_con in ids_con_tls[inds_con_route]: #    #inds_con_conflict = inds_cons_conflict[ids_con] #    print '  check id_con',id_con slots , slots_blocked = self . init_slots ( inds_con_route , # all used connections ids_con_tls , inds_cons_conflict , inds_cons_merge , ids_fromlane_tls , ids_tolane_tls , are_enabeled ) n_slots = len ( np . flatnonzero ( slots ) ) print '    n_slots' , n_slots , 'n_phases' , len ( phaseconflicts ) if n_slots == 0 : print '  no  conflicts detected' pass elif len ( phaseconflicts ) == 0 : print '  append first phase' phaseallocations . append ( inds_con_route ) phaseconflicts . append ( slots ) phaseblocks . append ( slots_blocked ) phasepriorities . append ( prio ) else : print '  search phase with minimum signal difference n_phases=' , len ( phaseallocations ) n_diff_min = 10 ** 8 i_phase = 0 i_phase_min = - 1 for inds_phaseallocation , inds_phaseconflict in zip ( phaseallocations , phaseconflicts ) : # check if slots overlap with allocated connections this phase print '    compare phase' , i_phase print '      new  allocations' , ids_con_tls [ inds_con_route ] print '      phaseallocations' , ids_con_tls [ inds_phaseallocation ] print '      new    conflicts' , ids_con_tls [ slots == 2 ] print '      phase  conflicts' , ids_con_tls [ inds_phaseconflict == 2 ] # print '      allocations',ids_con_tls[inds_phaseallocation] print '      n_diff =' , np . sum ( np . any ( ( slots == 2 ) & inds_phaseallocation ) ) if not np . any ( ( slots == 2 ) & inds_phaseallocation ) : print '    no conflict in this phase go for a merge' i_phase_min = i_phase n_diff_min = - 1 # indicate phase merge else : print '    there are conflicts with this phase...count' n_diff = np . sum ( np . any ( ( slots == 2 ) & inds_phaseallocation ) ) #n_diff = np.sum(np.abs(slots - inds_phaseconflict)!=0) # print '    ',inds_phaseconflict,n_diff if n_diff < n_diff_min : n_diff_min = n_diff i_phase_min = i_phase i_phase += 1 print '    finished comparing phases i_phase_min,n_diff_min' , i_phase_min , n_diff_min if n_diff_min == 0 : print '  already a phase with suitable signalling, nothing to do' pass elif n_diff_min == - 1 : print '  there are no phase conflicts, so merge' phaseallocations [ i_phase_min ] = phaseallocations [ i_phase_min ] | inds_con_route phaseconflicts [ i_phase_min ] = np . max ( [ slots , phaseconflicts [ i_phase_min ] ] , 0 ) phaseblocks [ i_phase_min ] = phaseblocks [ i_phase_min ] | slots_blocked phasepriorities [ i_phase_min ] = max ( prio , phasepriorities [ i_phase_min ] ) # elif n_diff_min>10**7: #    # all existing phases are conflicting else : # get number of cons which are more restrictive signals n_diff_pos = np . sum ( ( slots - phaseconflicts [ i_phase_min ] ) > 0 ) # get number of cons which are less restrictive signals n_diff_neg = np . sum ( ( slots - phaseconflicts [ i_phase_min ] ) < 0 ) print '  n_diff_min' , n_diff_min , 'n_diff_pos' , n_diff_pos , 'n_diff_neg' , n_diff_neg , 'i_phase_min' , i_phase_min # print '  inds_phaseconflict_min',ids_con_tls[phaseconflicts[i_phase_min] >0] # print '  inds_phaseconflict',ids_con_tls[slots>0] #inds_diff = np.abs(slots - inds_phaseconflict)!=0 # if (n_diff_pos>0) & (n_diff_neg == 0): if ( n_diff_pos >= n_diff_neg ) & ( n_diff_pos <= 2 ) : # only more restrictive print '    put new phase after the phase with minimum difference' phaseallocations . insert ( i_phase_min + 1 , inds_con_route ) phaseconflicts . insert ( i_phase_min + 1 , slots ) phaseblocks . insert ( i_phase_min + 1 , slots_blocked ) phasepriorities . insert ( i_phase_min + 1 , prio ) # elif (n_diff_pos==0) & (n_diff_neg > 0): if ( n_diff_pos < n_diff_neg ) & ( n_diff_neg <= 2 ) : # only less restrictive print '    put new phase before the phase with minimum difference' phaseallocations . insert ( i_phase_min , inds_con_route ) phaseconflicts . insert ( i_phase_min , slots ) phaseblocks . insert ( i_phase_min , slots_blocked ) phasepriorities . insert ( i_phase_min , prio ) else : # mixed changes print '    append en entirely new phase' phaseconflicts . append ( slots ) phaseblocks . append ( slots_blocked ) phasepriorities . append ( prio )