static int NumOfMoneyExchange(int amout, int countOfChange) { if (amout < 0 || countOfChange == 0) { return(0); } else if (amout == 0) { return(1); } else { StateStruct state = new StateStruct(amout, countOfChange); if (dic.ContainsKey(state)) { return(dic[state]); } else { int countMinusOne = countOfChange - 1; int num = NumOfMoneyExchange(amout, countMinusOne) + NumOfMoneyExchange(amout - First_Denomination(countOfChange), countOfChange); dic[state] = num;//存储状态 return(num); } } }
void restartMinindexStates() { int goalx = Mathf.RoundToInt(goal.transform.position.x); int goaly = Mathf.RoundToInt(goal.transform.position.z); minIndexStates = new StateStruct[rows * columns]; for (int i = 0; i < rows; i++) { for (int j = 0; j < columns; j++) { StateStruct state = new StateStruct(); state.x = j; state.y = i; if (j == goalx && i == goaly) { state.f = STARTSTATE; state.g = STARTSTATE; } else { state.f = NEEDSUPDATE; state.g = NEEDSUPDATE; } minIndexStates[i * columns + j] = state; } } for (int i = 0; i < obstacles.Length; ++i) { int obstaclex = Mathf.RoundToInt(obstacles[i].transform.position.x); int obstacley = Mathf.RoundToInt(obstacles[i].transform.position.z); insertValuesInMap(obstaclex, obstacley, OBSTACLESTATE, OBSTACLESTATE, 20.0f, false); StateStruct state = minIndexStates[obstacley * columns + obstaclex]; state.f = OBSTACLESTATE; state.g = OBSTACLESTATE; state.predx = -1; state.predy = -1; minIndexStates[obstacley * columns + obstaclex] = state; } }
public void AddState( States state, float time = 0, Action onEnterFunction = null, Action onUpdateFunction = null, Action onExitFunction = null ) { if(activeStates.Contains(state)) { if(!activeStatesDict.ContainsKey(state)) Debug.Log("WT: State " + state + " existed in List but was not in Dict"); else { if(activeStatesDict[state].timer == null || activeStatesDict[state].timer.TimeRemaining > time) return; else { activeStatesDict[state].timer.ResetTimer(time); return; } } } else { if(activeStatesDict.ContainsKey(state)) if(!activeStates.Contains(state)) Debug.Log("WT: State " + state + " existed in Dict but was not in List"); activeStates.Add(state); StateStruct temp = new StateStruct(state, onEnterFunction, onUpdateFunction, onExitFunction, time, this); activeStatesDict.Add(state, temp); } }
public HardwareState(StateStruct state) { this.enabledF1 = byteToBool(state.enabled_f1); this.enabledF2 = byteToBool(state.enabled_f2); this.enabledF3 = byteToBool(state.enabled_f3); this.enabledF4 = byteToBool(state.enabled_f4); this.enabledF5 = byteToBool(state.enabled_f5); this.enabledF6 = byteToBool(state.enabled_f6); this.enabledF10 = byteToBool(state.enabled_f10); this.ledLight = byteToBool(state.led_light); int tmp = state.search_env & 0x0F; switch (tmp) { case 0: currentState = SearchState.Idle; break; case 1: currentState = SearchState.Searching; break; case 2: currentState = SearchState.Generating; break; default: throw new ArgumentException(); } this.periodF1 = state.period_f1; this.periodF10 = state.period_f10; }
bool statesAreConsistentMinIndexCPU() { List <StateStruct> statesToCheck = new List <StateStruct>(); for (int i = 0; i < rows; i++) { for (int j = 0; j < columns; j++) { StateStruct state = minIndexStates[i * columns + j]; if (state.f != NEEDSUPDATE && state.f != OBSTACLESTATE) { statesToCheck.Add(state); } } } for (int i = 0; i < statesToCheck.Count; i++) { StateStruct state = statesToCheck[i]; if (!stateIsConsistent(state)) { return(false); } } return(true); }
private int computeCostsMinIndexCPU() { int minIndex = 0; List <StateStruct> statesToUpdate = new List <StateStruct>(); for (int i = 0; i < rows; i++) { for (int j = 0; j < columns; j++) { int index = i * columns + j; List <int> neighbors = neighborsForPosition(j, i); bool hasNieghborToUpdate = false; for (int s = 0; s < neighbors.Count; s++) { if (minIndexStates[neighbors[s]].f > -1 && minIndexStates[neighbors[s]].f != OBSTACLESTATE) { hasNieghborToUpdate = true; } } if (minIndexStates[index].f < 0 && hasNieghborToUpdate) { statesToUpdate.Add(minIndexStates[index]); } } } float minCost = Mathf.Infinity; for (int i = 0; i < statesToUpdate.Count; i++) { StateStruct state = statesToUpdate[i]; List <int> neighborsToUpdateIndex = neighborsForPosition(state.x, state.y); int updateIndex = state.y * columns + state.x; for (int s = 0; s < neighborsToUpdateIndex.Count; s++) { StateStruct neighbor = minIndexStates[neighborsToUpdateIndex[s]]; Vector2 vec1 = new Vector2((float)state.x, (float)state.y); Vector2 vec2 = new Vector2((float)neighbor.x, (float)neighbor.y); Vector2 startVec = new Vector2(start.transform.position.x, start.transform.position.z); float newCost = neighbor.g + Vector2.Distance(vec1, vec2) + Vector2.Distance(vec1, startVec); if (neighbor.f != NEEDSUPDATE && (newCost < state.f || state.f < 0)) { computedCosts[updateIndex] = newCost; state.f = newCost; state.g = neighbor.g + Vector2.Distance(vec1, vec2); state.predx = neighbor.x; state.predy = neighbor.y; minIndexStates[updateIndex] = state; if (newCost <= minCost) { minCost = newCost; minIndex = updateIndex; } } } } return(minIndex); }
public static StateStruct CreateStateStruct(int id, ref FileStruct.TSDialogState tsDialogState) { StateStruct ss = new StateStruct(); ss.id = id; ss.srStateIndex = tsDialogState.srStateIndex; ss.sTrigIndex = tsDialogState.sTrigIndex; ss.transIndex = tsDialogState.transIndex; ss.transCount = tsDialogState.transCount; return(ss); }
bool stateIsNotAnObstacle(StateStruct state) { foreach (GameObject obstacle in obstacles) { if (Mathf.RoundToInt(obstacle.transform.position.x) == state.x && Mathf.RoundToInt(obstacle.transform.position.z) == state.y) { return(false); } } return(true); }
// The beef: return an optimal action when we know the state public Action SelectAction(State _state) { // Find corresponding state struct StateStruct found = null; foreach (StateStruct statestr in states) { if (def.IsSameState(statestr.state, _state)) { found = statestr; break; } } // If not found, we have a new state, let's add that if (found == null) { found = new StateStruct(_state, def); states.Add(found); } // Then select either the best action (exploit) or some other (explore) ActionStruct selected = null; if (rnd.NextDouble() < 0.1) { // Select random int inx = rnd.Next(found.actions.Count()); selected = found.actions.ElementAt(inx); } else { // Select the best selected = found.actions.ElementAt(0); foreach (ActionStruct actstr in found.actions) { if (actstr.expectation > selected.expectation) { //if (actstr.expectation > selected.expectation || (actstr.expectation == selected.expectation && rnd.NextDouble() < 0.2)) // add some randomness if there are several similar options selected = actstr; } } } // Remember last selected actions (4 of them) if (last_actions.Count >= 4) { last_actions.RemoveAt(0); } last_actions.Add(selected); return(selected.action); }
bool stateIsConsistent(StateStruct state) { List <StateStruct> neighbors = getStateNeighbors(state); for (int i = 0; i < neighbors.Count; i++) { StateStruct neighbor = neighbors[i]; if (neighbor.f < minIndexStates[state.predy * columns + state.predx].f) { return(false); } } return(true); }
private static bool TryMergeStructFields(StateStruct ancStruct, StateStruct descStruct) { foreach (var ancField in ancStruct.Fields) { TicNode descFieldNode = descStruct.GetFieldOrNull(ancField.Key); if (descFieldNode == null) { return(false); } // i m not sure why - but it is very important to set descFieldNode as main merge node... SolvingFunctions.MergeInplace(descFieldNode, ancField.Value); } return(true); }
private void processIncomingPacket() { while (receivedPackets.Count > 0) { AbstractInPacket pack = receivedPackets.Dequeue(); if (pack.Command == 0x01) { currentState = ((Packet_01)pack).state; } else { pack.Process(); } } }
/******************************** ****** MOVEMENT HANDLING ******** *********************************/ void handleAgentMovement(Agent agent) { if (plannerType == PlannerType.Optimal) { StateStruct state = stateAtPosition(Mathf.RoundToInt(agent.gameObject.transform.position.x), Mathf.RoundToInt(agent.gameObject.transform.position.z)); List <StateStruct> neighbors = getStateNeighbors(state); foreach (StateStruct neighbor in neighbors) { if (stateIsNotAnObstacle(neighbor) && !stateEqualsGoal(neighbor)) { insertValuesInMap(neighbor.x, neighbor.y, NEEDSUPDATE, NEEDSUPDATE, 1.0f, false); } } } runKernel(); }
public void SingleStrictStructMember() { // 0 2 1 //y:int = a . name var graph = new GraphBuilder(); graph.SetVar("a", 0); graph.SetFieldAccess(0, 2, "name"); graph.SetVarType("y", StatePrimitive.I32); graph.SetDef("y", 2); var result = graph.Solve(); result.AssertNoGenerics(); result.AssertNamed(StateStruct.WithField("name", StatePrimitive.I32), "a"); result.AssertNamed(StatePrimitive.I32, "y"); }
void updateNeighborsToState(StateStruct state) { List <StateStruct> neighbors = getStateNeighbors(state); List <StateStruct> listToUpdate = new List <StateStruct>(); foreach (StateStruct neighbor in neighbors) { if (neighbor.predx == state.x && neighbor.predy == state.y && !stateEqualsGoal(neighbor)) { listToUpdate.Add(neighbor); } } foreach (StateStruct updateNeighbor in listToUpdate) { insertValuesInMap(updateNeighbor.x, updateNeighbor.y, NEEDSUPDATE, NEEDSUPDATE, 1.0f, true); updateNeighborsToState(updateNeighbor); } }
void handleObstacleMovement(Vector3 previousState, Vector3 newState) { int newx = Mathf.RoundToInt(newState.x); int newy = Mathf.RoundToInt(newState.z); int prevx = Mathf.RoundToInt(previousState.x); int prevy = Mathf.RoundToInt(previousState.z); insertValuesInMap(newx, newy, OBSTACLESTATE, OBSTACLESTATE, 20.0f, false); insertValuesInMap(prevx, prevy, NEEDSUPDATE, NEEDSUPDATE, 1.0f, true); if (plannerType == PlannerType.MinIndex) { StateStruct nState = stateAtPosition(newx, newy); List <StateStruct> neighbors = getStateNeighbors(nState); foreach (StateStruct neighbor in neighbors) { if (stateIsNotAnObstacle(neighbor)) { if (neighbor.predx == newx && neighbor.predy == newy && !stateEqualsGoal(neighbor)) { insertValuesInMap(neighbor.x, neighbor.y, NEEDSUPDATE, NEEDSUPDATE, 1.0f, true); } } } } else { foreach (Agent agent in agents) { foreach (Vector2 pathState in agent.path) { if (pathState.x == newState.x && pathState.y == newState.y) { StateStruct stateToUpdate = stateAtPosition(newx, newy); updateNeighborsToState(stateToUpdate); break; } } } } runKernel(); }
private List <StateStruct> getStateNeighbors(StateStruct state) { List <StateStruct> neighbors = new List <StateStruct>(); if (state.x < columns - 1) { neighbors.Add(stateAtPosition(state.x + 1, state.y)); } if (state.x > 0) { neighbors.Add(stateAtPosition(state.x - 1, state.y)); } if (state.y < rows - 1) { neighbors.Add(stateAtPosition(state.x, state.y + 1)); } if (state.y > 0) { neighbors.Add(stateAtPosition(state.x, state.y - 1)); } if (state.x < columns - 1 && state.y < rows - 1) { neighbors.Add(stateAtPosition(state.x + 1, state.y + 1)); } if (state.x < columns - 1 && state.y > 0) { neighbors.Add(stateAtPosition(state.x + 1, state.y - 1)); } if (state.x > 0 && state.y < rows - 1) { neighbors.Add(stateAtPosition(state.x - 1, state.y + 1)); } if (state.x > 0 && state.y > 0) { neighbors.Add(stateAtPosition(state.x - 1, state.y - 1)); } return(neighbors); }
void updateSuccessorMinIndexCPU(StateStruct state) { List <StateStruct> listToUpdate = new List <StateStruct>(); List <int> neighbors = neighborsForPosition(state.x, state.y); for (int i = 0; i < neighbors.Count; i++) { int neighborIndex = neighbors[i]; StateStruct neighbor = minIndexStates[neighborIndex]; if (neighbor.predx == state.x && neighbor.predy == state.y && neighbor.f != OBSTACLESTATE) { listToUpdate.Add(neighbor); } } for (int i = 0; i < listToUpdate.Count; i++) { StateStruct updateNeighbor = listToUpdate[i]; updateNeighbor.f = NEEDSUPDATE; updateNeighbor.g = NEEDSUPDATE; minIndexStates[updateNeighbor.y * columns + updateNeighbor.x] = updateNeighbor; computedCosts[updateNeighbor.y * columns + updateNeighbor.x] = NEEDSUPDATE; updateSuccessorMinIndexCPU(updateNeighbor); } }
// Add the callback functions for a new state to our callbacks dictioanry public void SetCallbacks(int i, UDelegateFn update, System.Action coroutine, UDelegateFn begin, UDelegateFn end) { callbacks[i] = new StateStruct(update, coroutine, begin, end); }
bool stateIsNotAnObstacle(StateStruct state) { foreach(GameObject obstacle in obstacles) { if (Mathf.RoundToInt(obstacle.transform.position.x) == state.x && Mathf.RoundToInt(obstacle.transform.position.z) == state.y) { return false; } } return true; }
void OnDrawGizmos() { if (stateCostValues != null && showStates) { for (int i = 0; i < rows; i++) { for (int j = 0; j < columns; j++) { StateStruct state = stateAtPosition(j, i); if (state.f != OBSTACLESTATE) { Vector3 nodePosition = new Vector3((float)j, 0.5f, (float)i); Vector3 predPosition = new Vector3((float)state.predx, 0.5f, (float)state.predy); if (state.g != NEEDSUPDATE && state.f != NEEDSUPDATE) { Vector3 direction = nodePosition - predPosition; Gizmos.color = Color.red; Gizmos.DrawSphere(nodePosition, 0.25f); DrawArrow.ForGizmo(predPosition, direction, Color.black); } } } } } if (agents != null && agents.Count > 0) { foreach (Agent agent in agents) { if (agent.path != null) { foreach (Vector2 state in agent.path) { Gizmos.color = Color.blue; Gizmos.DrawSphere(new Vector3(state.x, 0.5f, state.y), 0.25f); } } } } Gizmos.color = Color.yellow; if (selectedGameObject != null) { if (selectedGameObject.collider is BoxCollider) { Gizmos.DrawWireCube(selectedGameObject.transform.position, (selectedGameObject.collider as BoxCollider).size); } else if (selectedGameObject.collider is SphereCollider) { Gizmos.DrawWireSphere(selectedGameObject.transform.position, (selectedGameObject.collider as SphereCollider).radius); } } Gizmos.DrawWireCube(new Vector3(columns / 2, 1.0f, rows / 2), new Vector3(columns, 0.0f, rows)); if (showHMap && stateHValues != null) { for (int i = 0; i < rows; i++) { for (int j = 0; j < columns; j++) { float val = stateHValues[i * columns + j]; float blue = 1 * val; float green = 1 - blue; Gizmos.color = new Color(0.0f, green, blue, 0.5f); Gizmos.DrawCube(new Vector3(j, 1.0f, i), new Vector3(1.0f, 0.0f, 1.0f)); } } } if (showCostMap && stateCostValues != null) { for (int i = 0; i < rows; i++) { for (int j = 0; j < columns; j++) { float val = stateCostValues[i * columns + j]; float blue = 1 * val; float green = 1 - blue; Gizmos.color = new Color(0.0f, green, blue, 0.5f); Gizmos.DrawCube(new Vector3(j, 1.0f, i), new Vector3(1.0f, 0.0f, 1.0f)); } } } if (showGMap && stateGValues != null) { for (int i = 0; i < rows; i++) { for (int j = 0; j < columns; j++) { float val = stateGValues[i * columns + j]; if (val == OBSTACLESTATE) { Gizmos.color = Color.black; } else { Gizmos.color = new Color(1 - val, 1 - val, 1 - val, 0.5f); } Gizmos.DrawCube(new Vector3(j, 1.0f, i), new Vector3(1.0f, 0.0f, 1.0f)); } } } if (showFMap && stateGrayScaleValues != null) { for (int i = 0; i < rows; i++) { for (int j = 0; j < columns; j++) { float val = stateGrayScaleValues[i * columns + j]; if (val == OBSTACLESTATE) { Gizmos.color = Color.black; } else { Gizmos.color = new Color(1 - val, 1 - val, 1 - val, 0.5f); } Gizmos.DrawCube(new Vector3(j, 1.0f, i), new Vector3(1.0f, 0.0f, 1.0f)); } } } }
bool stateIsConsistent(StateStruct state) { List<StateStruct> neighbors = getStateNeighbors(state); for (int i = 0; i < neighbors.Count; i++) { StateStruct neighbor = neighbors[i]; if (neighbor.f < minIndexStates[state.predy*columns+state.predx].f) { return false; } } return true; }
void updateSuccessorMinIndexCPU(StateStruct state) { List<StateStruct> listToUpdate = new List<StateStruct>(); List<int> neighbors = neighborsForPosition(state.x, state.y); for (int i = 0; i < neighbors.Count; i++) { int neighborIndex = neighbors[i]; StateStruct neighbor = minIndexStates[neighborIndex]; if (neighbor.predx == state.x && neighbor.predy == state.y && neighbor.f != OBSTACLESTATE) { listToUpdate.Add(neighbor); } } for (int i = 0; i < listToUpdate.Count; i++) { StateStruct updateNeighbor = listToUpdate[i]; updateNeighbor.f = NEEDSUPDATE; updateNeighbor.g = NEEDSUPDATE; minIndexStates[updateNeighbor.y*columns+updateNeighbor.x] = updateNeighbor; computedCosts[updateNeighbor.y*columns+updateNeighbor.x] = NEEDSUPDATE; updateSuccessorMinIndexCPU(updateNeighbor); } }
/// <summary> /// Transform constrains to struct state /// </summary> public static StateStruct TransformToStructOrNull(ConstrainsState descendant, StateStruct ancStruct) { if (descendant.NoConstrains) { return(ancStruct); } if (descendant.HasDescendant && descendant.Descedant is StateStruct structDesc) { //descendant is struct. if (structDesc.IsSolved) { return(structDesc); //if it is solved - return it } // For perfomance bool allFieldsAreSolved = true; var newFields = new Dictionary <string, TicNode>(structDesc.MembersCount); foreach (var field in structDesc.Fields) { var nrField = field.Value.GetNonReference(); allFieldsAreSolved = allFieldsAreSolved && nrField.IsSolved; newFields.Add(field.Key, nrField); } return(new StateStruct(newFields)); } return(null); }
private List<StateStruct> getStateNeighbors(StateStruct state) { List<StateStruct> neighbors = new List<StateStruct>(); if (state.x < columns-1) { neighbors.Add(stateAtPosition(state.x+1, state.y)); } if (state.x > 0) { neighbors.Add(stateAtPosition(state.x-1, state.y)); } if (state.y < rows-1) { neighbors.Add(stateAtPosition(state.x, state.y+1)); } if (state.y > 0) { neighbors.Add(stateAtPosition(state.x, state.y-1)); } if (state.x < columns-1 && state.y < rows-1) { neighbors.Add(stateAtPosition(state.x+1, state.y+1)); } if (state.x < columns-1 && state.y > 0) { neighbors.Add(stateAtPosition(state.x+1, state.y-1)); } if (state.x > 0 && state.y < rows-1) { neighbors.Add(stateAtPosition(state.x-1, state.y+1)); } if (state.x > 0 && state.y > 0) { neighbors.Add(stateAtPosition(state.x-1, state.y-1)); } return neighbors; }
bool stateEqualsGoal(StateStruct state) { return (state.x == goal.transform.position.x && state.y == goal.transform.position.z); }
void restartMinindexStates() { int goalx = Mathf.RoundToInt(goal.transform.position.x); int goaly = Mathf.RoundToInt(goal.transform.position.z); minIndexStates = new StateStruct[rows*columns]; for (int i = 0; i < rows; i++) { for (int j = 0; j < columns; j++) { StateStruct state = new StateStruct(); state.x = j; state.y = i; if (j == goalx && i == goaly) { state.f = STARTSTATE; state.g = STARTSTATE; } else { state.f = NEEDSUPDATE; state.g = NEEDSUPDATE; } minIndexStates[i*columns+j] = state; } } for(int i = 0; i < obstacles.Length; ++i) { int obstaclex = Mathf.RoundToInt(obstacles[i].transform.position.x); int obstacley = Mathf.RoundToInt(obstacles[i].transform.position.z); insertValuesInMap(obstaclex, obstacley, OBSTACLESTATE, OBSTACLESTATE, 20.0f, false); StateStruct state = minIndexStates[obstacley*columns+obstaclex]; state.f = OBSTACLESTATE; state.g = OBSTACLESTATE; state.predx = -1; state.predy = -1; minIndexStates[obstacley*columns+obstaclex] = state; } }
bool stateEqualsGoal(StateStruct state) { return(state.x == goal.transform.position.x && state.y == goal.transform.position.z); }
void updateNeighborsToState(StateStruct state) { List<StateStruct> neighbors = getStateNeighbors(state); List<StateStruct> listToUpdate = new List<StateStruct>(); foreach (StateStruct neighbor in neighbors) { if (neighbor.predx == state.x && neighbor.predy == state.y && !stateEqualsGoal(neighbor)) { listToUpdate.Add(neighbor); } } foreach (StateStruct updateNeighbor in listToUpdate) { insertValuesInMap(updateNeighbor.x, updateNeighbor.y, NEEDSUPDATE, NEEDSUPDATE, 1.0f, true); updateNeighborsToState(updateNeighbor); } }