def state_to_index(grid_cells, state_bounds, state):
  """Transforms the state into the index of the nearest grid.

  Args:
      grid_cells (tuple of ints): where the ith value is the number of
          grid_cells for ith dimension of state
      state_bounds (list of tuples):  where ith tuple contains the min and
          max value in that order of ith dimension
      state (np.ndarray): state to discretize

  Returns:
      state discretized into appropriate grid_cells
  """

  index = []
  for i in range(len(state)):
    lower_bound = state_bounds[i][0]
    upper_bound = state_bounds[i][1]
    if state[i] <= lower_bound:
      index.append(0)
    elif state[i] >= upper_bound:
      index.append(grid_cells[i] - 1)
    else:
      index.append(
          int(((state[i] - lower_bound) * grid_cells[i]) //
              (upper_bound-lower_bound))
      )
  return tuple(index)