Estimate state-value of the transition next state
336138
def model_actions(self):
        
        observations = self.get('rollout:observations')
        model_action = self.model.action(observations)
        return model_action
914316
def get_transitions(self, indexes):
        
        assert indexes.shape[1] == self.state_buffer.shape[1], \
            "Must have the same number of indexes as there are environments"

        frame_batch_shape = (
                [indexes.shape[0], indexes.shape[1]]
                + list(self.state_buffer.shape[2:-1])
                + [self.state_buffer.shape[-1] * self.frame_history]
        )

        past_frame_buffer = np.zeros(frame_batch_shape, dtype=self.state_buffer.dtype)
        future_frame_buffer = np.zeros(frame_batch_shape, dtype=self.state_buffer.dtype)

        for buffer_idx, frame_row in enumerate(indexes):
            for env_idx, frame_idx in enumerate(frame_row):
                past_frame_buffer[buffer_idx, env_idx], future_frame_buffer[buffer_idx, env_idx] = (
                    self.get_frame_with_future(frame_idx, env_idx)
                )

        actions = take_along_axis(self.action_buffer, indexes)
        rewards = take_along_axis(self.reward_buffer, indexes)
        dones = take_along_axis(self.dones_buffer, indexes)

        transition_tensors = {
            'observations': past_frame_buffer,
            'actions': actions,
            'rewards': rewards,
            'observations_next': future_frame_buffer,
            'dones': dones.astype(np.float32),
        }

        for name in self.extra_data:
            transition_tensors[name] = take_along_axis(self.extra_data[name], indexes)

        return transition_tensors
121443
def _do_mstep(self, stats):
        
        # The ``np.where`` calls guard against updating forbidden states
        # or transitions in e.g. a left-right HMM.
        if 's' in self.params:
            startprob_ = self.startprob_prior - 1.0 + stats['start']
            self.startprob_ = np.where(self.startprob_ == 0.0,
                                       self.startprob_, startprob_)
            normalize(self.startprob_)
        if 't' in self.params:
            transmat_ = self.transmat_prior - 1.0 + stats['trans']
            self.transmat_ = np.where(self.transmat_ == 0.0,
                                      self.transmat_, transmat_)
            normalize(self.transmat_, axis=1)
679060
def allowed_transitions():
    """"""
    try:
        sdp_state = SDPState()
        return sdp_state.allowed_target_states[sdp_state.current_state]
    except KeyError:
        LOG.error("Key Error")
        return dict(state="KeyError", reason="KeyError")
174907
def transition_state(state)
      authorize @contribution
      @initiative = @contribution.initiative
      @user = @contribution.user
      state = state.to_sym
      transition = @contribution.transition_by_state(state)
      initial_state = @contribution.state_name
      resource_name = @contribution.class.model_name.human
      if @contribution.send("can_#{transition}?")
        begin
          if @contribution.state_on_gateway != state
            if @contribution.update_state_on_gateway!(state)
              @contribution.send("#{transition}!")
            else
              flash[:alert] = t('flash.actions.update.alert', resource_name: resource_name)
            end
          else
            @contribution.send("#{transition}!")
          end
        rescue
          flash[:alert] = t('flash.actions.update.alert', resource_name: resource_name)
        end
      else
        flash[:alert] = t('flash.actions.update.alert', resource_name: resource_name)
      end
      if flash[:alert].present?
        render 'initiatives/contributions/show'
      else
        if initial_state == :pending
          flash[:notice] = t('flash.actions.create.notice', resource_name: resource_name)
        else
          flash[:notice] = t('flash.actions.update.notice', resource_name: resource_name)
        end
        redirect_to initiative_contribution_path(@contribution.initiative.id, @contribution)
      end
    end
48341
def _get_transitions(self, probs, indexes, tree_idxs, batch_info, forward_steps=1, discount_factor=1.0):
        
        if forward_steps > 1:
            transition_arrays = self.backend.get_transitions_forward_steps(indexes, forward_steps, discount_factor)
        else:
            transition_arrays = self.backend.get_transitions(indexes)

        priority_weight = self.priority_weight.value(batch_info['progress'])

        # Normalize by sum of all probs
        probs = probs / np.array([s.total() for s in self.backend.segment_trees], dtype=float).reshape(1, -1)
        capacity = self.backend.current_size
        weights = (capacity * probs) ** (-priority_weight)
        weights = weights / weights.max(axis=0, keepdims=True)

        transition_arrays['weights'] = weights
        transition_tensors = {k: torch.from_numpy(v) for k, v in transition_arrays.items()}

        transitions = Trajectories(
            num_steps=indexes.shape[0],
            num_envs=indexes.shape[1],
            environment_information=None,
            transition_tensors=transition_tensors,
            rollout_tensors={},
            extra_data={
                'tree_idxs': tree_idxs
            }
        )

        return transitions.to_transitions()
1101675
def _states(self, state, processedstates=[]): #pylint: disable=dangerous-default-value
        """"""
        processedstates.append(state)

        for nextstate in state.epsilon:
            if not nextstate in processedstates:
                self._states(nextstate, processedstates)

        for _, nextstate in state.transitions:
            if not nextstate in processedstates:
                self._states(nextstate, processedstates)

        return processedstates
683683
def get_transitions(self, indexes, history_length=1):
        
        frame_batch_shape = (
                [indexes.shape[0]]
                + list(self.state_buffer.shape[1:-1])
                + [self.state_buffer.shape[-1] * history_length]
        )

        past_frame_buffer = np.zeros(frame_batch_shape, dtype=self.state_buffer.dtype)
        future_frame_buffer = np.zeros(frame_batch_shape, dtype=self.state_buffer.dtype)

        for buffer_idx, frame_idx in enumerate(indexes):
            past_frame_buffer[buffer_idx], future_frame_buffer[buffer_idx] = self.get_frame_with_future(
                frame_idx, history_length
            )

        actions = self.action_buffer[indexes]
        rewards = self.reward_buffer[indexes]
        dones = self.dones_buffer[indexes]

        data_dict = {
            'observations': past_frame_buffer,
            'actions': actions,
            'rewards': rewards,
            'observations_next': future_frame_buffer,
            'dones': dones,
        }

        for name in self.extra_data:
            data_dict[name] = self.extra_data[name][indexes]

        return data_dict
1246650
private void doRunningStateChange(RunningState newState) {
    RunningState oldState = null;
    JobExecutionStateListener stateListener = null;
    this.changeLock.lock();
    try {
      // verify transition
      if (null == this.runningState) {
        Preconditions.checkState(RunningState.PENDING == newState);
      }
      else {
        Preconditions.checkState(EXPECTED_PRE_TRANSITION_STATES.get(newState).contains(this.runningState),
            "unexpected state transition " + this.runningState + " --> " + newState);
      }

      oldState = this.runningState;
      this.runningState = newState;
      if (this.listener.isPresent()) {
        stateListener = this.listener.get();
      }
      this.runningStateChanged.signalAll();
    }
    finally {
      this.changeLock.unlock();
    }
    if (null != stateListener) {
      stateListener.onStatusChange(this, oldState, newState);
    }
  }
605180
def cross_state_value(state):
        
        centres, edges = state
        value = 0
        for edge in edges:
            if "U" in edge:
                if edge["U"] == centres["D"]["D"]:
                    value += 1
                else:
                    value += 2
            elif "D" in edge:
                if edge["D"] != centres["D"]["D"]:
                    value += 3
            else:
                value += 1
        edgeposes = {}
        counts = {f: 0 for f in "LFRB"}
        ngedges = []
        for edge in edges:
            if "U" in edge and edge["U"] == centres["D"]["D"]:
                k = "".join(edge.facings.keys()).replace("U", "")
                edgeposes[k] = edge[k]
                counts[k] += 1
            elif "D" in edge and edge["D"] == centres["D"]["D"]:
                k = "".join(edge.facings.keys()).replace("D", "")
                edgeposes[k] = edge[k]
                counts[k] += 1
            elif "U" in edge or "D" in edge:
                ngedges.append(edge)
            else:
                for k, s in edge:
                    if s != centres["D"]["D"]:
                        edgeposes[k] = s
                        counts[k] += 1
                        break
        for edge in ngedges:
            idx = "LFRB".index(edge[centres["D"].colour])
            for i in [-1, 1]:
                if "LFRB"[(idx+1)%4] not in edgeposes:
                    k = "".join(edge.facings.keys()).replace("LFRB"[idx], "")
                    edgeposes["LFRB"[(idx+1)%4]] = edge[k]
                    counts["LFRB"[(idx+1)%4]] += 1
                    break
            else:
                k = "".join(edge.facings.keys()).replace("LFRB"[idx], "")
                if counts["LFRB"[(idx-1)%4]] > counts["LFRB"[(idx+1)%4]]:
                    edgeposes["LFRB"[(idx-1)%4]] = edge[k]
                else:
                    edgeposes["LFRB"[(idx+1)%4]] = edge[k]
        relative_pos = {f: centres[f][f] for f in "LFRB"}
        if len(edgeposes) == 4:
            for i in range(4):
                edgeposes["L"], edgeposes["F"], edgeposes["R"], edgeposes["B"] = \
                    edgeposes["F"], edgeposes["R"], edgeposes["B"], edgeposes["L"]
                if edgeposes == relative_pos:
                    break
            else:
                value += 5
        else:
            value += 3
        return value
355024
def _generate_state(self, trans):
        
        state = PDAState()
        state.id = self.nextstate()
        state.type = 2
        state.sym = state.id
        state.trans = trans.copy()
        self.toadd.append(state)
        return state.id
505186
def transition_to(state)
      ::Instana.logger.debug("Transitioning to #{state}")
      case state
      when :announced
        # announce successful; set state
        @state = :announced

        # Reset the entity timer
        @entity_last_seen = Time.now

      when :unannounced
        @state = :unannounced
        # Reset our HTTP client
        @httpclient = nil

      else
        ::Instana.logger.debug "Uknown agent state: #{state}"
      end
      ::Instana.collector.reset_timer!
      true
    end
146505
def add_state(self, new_data, method='inc'):
        """"""
        self.sfx.append(0)
        self.rsfx.append([])
        self.trn.append([])
        self.lrs.append(0)

        # Experiment with pointer-based
        self.f_array.add(new_data)

        self.n_states += 1
        i = self.n_states - 1

        # assign new transition from state i-1 to i
        self.trn[i - 1].append(i)
        k = self.sfx[i - 1]
        pi_1 = i - 1

        # iteratively backtrack suffixes from state i-1
        if method == 'inc':
            suffix_candidate = 0
        elif method == 'complete':
            suffix_candidate = []
        else:
            suffix_candidate = 0

        while k is not None:

            if self.params['dfunc'] == 'other':
                # dvec = self.dfunc_handle([new_data],
                #                          self.f_array[self.trn[k]])[0]
                dvec = dist.cdist([new_data],
                                  self.f_array[self.trn[k]],
                                  metric=self.params['dfunc_handle'])[0]
            else:
                dvec = dist.cdist([new_data],
                                  self.f_array[self.trn[k]],
                                  metric=self.params['dfunc'])[0]

            I = np.where(dvec < self.params['threshold'])[0]
            if len(I) == 0:  # if no transition from suffix
                self.trn[k].append(i)  # Add new forward link to unvisited state
                pi_1 = k
                if method != 'complete':
                    k = self.sfx[k]
            else:
                if method == 'inc':
                    if I.shape[0] == 1:
                        suffix_candidate = self.trn[k][I[0]]
                    else:
                        suffix_candidate = self.trn[k][I[np.argmin(dvec[I])]]
                    break
                elif method == 'complete':
                    suffix_candidate.append((self.trn[k][I[np.argmin(dvec[I])]],
                                             np.min(dvec)))
                else:
                    suffix_candidate = self.trn[k][I[np.argmin(dvec[I])]]
                    break

            if method == 'complete':
                k = self.sfx[k]

        if method == 'complete':
            if not suffix_candidate:
                self.sfx[i] = 0
                self.lrs[i] = 0
                self.latent.append([i])
                self.data.append(len(self.latent) - 1)
            else:
                sorted_suffix_candidates = sorted(suffix_candidate,
                                                  key=lambda suffix: suffix[1])
                self.sfx[i] = sorted_suffix_candidates[0][0]
                self.lrs[i] = self._len_common_suffix(pi_1, self.sfx[i] - 1) + 1
                self.latent[self.data[self.sfx[i]]].append(i)
                self.data.append(self.data[self.sfx[i]])
        else:
            if k is None:
                self.sfx[i] = 0
                self.lrs[i] = 0
                self.latent.append([i])
                self.data.append(len(self.latent) - 1)
            else:
                self.sfx[i] = suffix_candidate
                self.lrs[i] = self._len_common_suffix(pi_1, self.sfx[i] - 1) + 1
                self.latent[self.data[self.sfx[i]]].append(i)
                self.data.append(self.data[self.sfx[i]])

        # Temporary adjustment
        k = self._find_better(i, self.data[i - self.lrs[i]])
        if k is not None:
            self.lrs[i] += 1
            self.sfx[i] = k

        self.rsfx[self.sfx[i]].append(i)

        if self.lrs[i] > self.max_lrs[i - 1]:
            self.max_lrs.append(self.lrs[i])
        else:
            self.max_lrs.append(self.max_lrs[i - 1])

        self.avg_lrs.append(self.avg_lrs[i - 1] * ((i - 1.0) / (self.n_states - 1.0)) +
                            self.lrs[i] * (1.0 / (self.n_states - 1.0)))
719156
function(newState, newValues) {
      if (state == null && newState != null) {
        state = newState;
        values = newValues;
        if (deferred.length)
          defer(function() {
            for (var i = 0; i < deferred.length; i++)
            deferred[i]();
          });
      }
      return state;
    }
1776
def store_transition(self, frame, action, reward, done, extra_info=None):
        
        self.current_idx = (self.current_idx + 1) % self.buffer_capacity

        if self.frame_stack_compensation:
            # Compensate for frame stack built into the environment
            idx_range = np.arange(-frame.shape[-1] // self.frame_history, 0)
            frame = np.take(frame, indices=idx_range, axis=-1)

        self.state_buffer[self.current_idx] = frame

        self.action_buffer[self.current_idx] = action
        self.reward_buffer[self.current_idx] = reward
        self.dones_buffer[self.current_idx] = done

        if extra_info is not None:
            for name in extra_info:
                if name not in self.extra_data:
                    assert self.current_size == 0, f"New data {name} encountered in the middle of the training"
                    array = extra_info[name]

                    self.extra_data[name] = np.zeros([self.buffer_capacity] + list(array.shape), dtype=array.dtype)

                self.extra_data[name][self.current_idx] = extra_info[name]

        if self.current_size < self.buffer_capacity:
            self.current_size += 1

        return self.current_idx
273146
def extract_now_state(self):
        '''
        
        '''
        x, y = self.__agent_pos
        state_arr = np.zeros(self.__map_arr.shape)
        state_arr[x, y] = 1
        return np.expand_dims(state_arr, axis=0)
108367
def get_constant_state(self):
    
    ret = self.constant_states[self.next_constant_state]
    self.next_constant_state += 1
    return ret