Replaced costs array with just a single cost value

This commit is contained in:
Michael Votaw
2025-02-11 19:39:03 -08:00
committed by teejusb
parent cbfd09658f
commit 61ee3bc329
5 changed files with 61 additions and 88 deletions
+21 -35
View File
@@ -20,17 +20,13 @@ bool isEmpty(const std::vector<T> & vec, int columnCount) {
} }
float* StepParityCost::getActionCost(State * initialState, State * resultState, std::vector<Row>& rows, int rowIndex) float StepParityCost::getActionCost(State * initialState, State * resultState, std::vector<Row>& rows, int rowIndex)
{ {
Row &row = rows[rowIndex]; Row &row = rows[rowIndex];
int columnCount = row.columnCount; int columnCount = row.columnCount;
float elapsedTime = resultState->second - initialState->second; float elapsedTime = resultState->second - initialState->second;
float* costs = new float[NUM_Cost]; float cost = 0;
for(int i = 0; i < NUM_Cost; i++)
{
costs[i] = 0;
}
std::vector<StepParity::Foot> combinedColumns(columnCount, NONE); std::vector<StepParity::Foot> combinedColumns(columnCount, NONE);
@@ -63,11 +59,6 @@ float* StepParityCost::getActionCost(State * initialState, State * resultState,
} }
} }
costs[COST_MINE] += calcMineCost( initialState, resultState, row, combinedColumns, columnCount);
costs[COST_HOLDSWITCH] += calcHoldSwitchCost( initialState, resultState, row, combinedColumns, columnCount);
costs[COST_BRACKETTAP] += calcBracketTapCost( initialState, resultState, row, leftHeel, leftToe, rightHeel, rightToe, elapsedTime, columnCount);
// costs[COST_OTHER] += calcMovingFootWhileOtherIsntOnPadCost( initialState, resultState, columnCount);
bool movedLeft = bool movedLeft =
resultState->didTheFootMove[LEFT_HEEL] || resultState->didTheFootMove[LEFT_HEEL] ||
resultState->didTheFootMove[LEFT_TOE]; resultState->didTheFootMove[LEFT_TOE];
@@ -87,26 +78,23 @@ float* StepParityCost::getActionCost(State * initialState, State * resultState,
(initialState->didTheFootMove[RIGHT_TOE] && (initialState->didTheFootMove[RIGHT_TOE] &&
!initialState->isTheFootHolding[RIGHT_TOE])); !initialState->isTheFootHolding[RIGHT_TOE]));
// jacks don't matter if you did a jump before
bool jackedLeft = didJackLeft(initialState, resultState, leftHeel, leftToe, movedLeft, didJump, columnCount); bool jackedLeft = didJackLeft(initialState, resultState, leftHeel, leftToe, movedLeft, didJump, columnCount);
bool jackedRight = didJackRight(initialState, resultState, rightHeel, rightToe, movedRight, didJump, columnCount); bool jackedRight = didJackRight(initialState, resultState, rightHeel, rightToe, movedRight, didJump, columnCount);
// Doublestep weighting doesn't apply if you just did a jump or a jack cost += calcMineCost( initialState, resultState, row, combinedColumns, columnCount);
cost += calcHoldSwitchCost( initialState, resultState, row, combinedColumns, columnCount);
costs[COST_BRACKETJACK] += calcBracketJackCost( initialState, resultState, rows, rowIndex, movedLeft, movedRight, jackedLeft, jackedRight, didJump, columnCount); cost += calcBracketTapCost( initialState, resultState, row, leftHeel, leftToe, rightHeel, rightToe, elapsedTime, columnCount);
costs[COST_DOUBLESTEP] += calcDoublestepCost(initialState, resultState, rows, rowIndex, movedLeft, movedRight, jackedLeft, jackedRight, didJump, columnCount); cost += calcBracketJackCost( initialState, resultState, rows, rowIndex, movedLeft, movedRight, jackedLeft, jackedRight, didJump, columnCount);
// costs[COST_JUMP] += calcJumpCost( row, movedLeft, movedRight, elapsedTime, columnCount); cost += calcDoublestepCost(initialState, resultState, rows, rowIndex, movedLeft, movedRight, jackedLeft, jackedRight, didJump, columnCount);
costs[COST_SLOW_BRACKET] += calcSlowBracketCost(row, movedLeft, movedRight, elapsedTime); cost += calcSlowBracketCost(row, movedLeft, movedRight, elapsedTime);
costs[COST_TWISTED_FOOT] += calcTwistedFootCost(resultState); cost += calcTwistedFootCost(resultState);
costs[COST_FACING] += calcFacingCosts( initialState, resultState, combinedColumns, columnCount); cost += calcFacingCosts( initialState, resultState, combinedColumns, columnCount);
costs[COST_SPIN] += calcSpinCosts(initialState, resultState, combinedColumns, columnCount); cost += calcSpinCosts(initialState, resultState, combinedColumns, columnCount);
costs[COST_FOOTSWITCH] += caclFootswitchCost( initialState, resultState, row, combinedColumns, elapsedTime, columnCount); cost += caclFootswitchCost( initialState, resultState, row, combinedColumns, elapsedTime, columnCount);
costs[COST_SIDESWITCH] += calcSideswitchCost( initialState, resultState, columnCount); cost += calcSideswitchCost( initialState, resultState, columnCount);
costs[COST_MISSED_FOOTSWITCH] += calcMissedFootswitchCost( row, jackedLeft, jackedRight, columnCount); cost += calcMissedFootswitchCost( row, jackedLeft, jackedRight, columnCount);
costs[COST_JACK] += calcJackCost( movedLeft, movedRight, jackedLeft, jackedRight, elapsedTime, columnCount); cost += calcJackCost( movedLeft, movedRight, jackedLeft, jackedRight, elapsedTime, columnCount);
costs[COST_DISTANCE] += calcBigMovementsQuicklyCost( initialState, resultState, elapsedTime, columnCount); cost += calcBigMovementsQuicklyCost( initialState, resultState, elapsedTime, columnCount);
// costs[COST_CROWDED_BRACKET] += calcCrowdedBracketCost(initialState, resultState, elapsedTime, columnCount);
// I don't like that we're updating columns here like this. // I don't like that we're updating columns here like this.
// We're basically updating columns with the final position of the feet // We're basically updating columns with the final position of the feet
@@ -119,12 +107,8 @@ float* StepParityCost::getActionCost(State * initialState, State * resultState,
resultState->whereTheFeetAre[combinedColumns[i]] = i; resultState->whereTheFeetAre[combinedColumns[i]] = i;
} }
} }
for(int i = 0; i < COST_TOTAL; i++)
{
costs[COST_TOTAL] += costs[i];
}
return costs; return cost;
} }
// This merges the `columns` properties of initialState and resultState, which // This merges the `columns` properties of initialState and resultState, which
@@ -172,7 +156,8 @@ void StepParityCost::mergeInitialAndResultPosition(State * initialState, State *
// Calculate the cost of avoiding a mine before the current step // Calculate the cost of avoiding a mine before the current step
// If a mine occurred just before a step, add to the cost // If a mine occurred just before a step, add to the cost
// ex: 00M0 // ex:
// 00M0
// 0010 <- add cost // 0010 <- add cost
// //
// 00M0 // 00M0
@@ -225,7 +210,8 @@ float StepParityCost::calcHoldSwitchCost(State * initialState, State * resultSta
// Calculate the cost of tapping a bracket during a hold note // Calculate the cost of tapping a bracket during a hold note
// //
// ex: 0200 // ex:
// 0200
// 0000 // 0000
// 1000 <- maybe bracketable, if left heel is holding Down arrow // 1000 <- maybe bracketable, if left heel is holding Down arrow
// 0300 // 0300
+1 -1
View File
@@ -50,7 +50,7 @@ namespace StepParity
/// @param rows /// @param rows
/// @param rowIndex The index of the row represented by resultState /// @param rowIndex The index of the row represented by resultState
/// @return The computed cost /// @return The computed cost
float* getActionCost(State * initialState, State * resultState, std::vector<Row> &rows, int rowIndex); float getActionCost(State * initialState, State * resultState, std::vector<Row> &rows, int rowIndex);
private: private:
void mergeInitialAndResultPosition(State * initialState, State * resultState, std::vector<StepParity::Foot> &combinedColumns, int columnCount); void mergeInitialAndResultPosition(State * initialState, State * resultState, std::vector<StepParity::Foot> &combinedColumns, int columnCount);
+2 -6
View File
@@ -273,12 +273,8 @@ Json::Value StepParityNode::ToJson()
Json::Value n; Json::Value n;
n["id"] = it->first->id; n["id"] = it->first->id;
Json::Value jsonCosts; Json::Value jsonCosts;
float * costs = it->second; float cost = it->second;
for(int i = 0; i < NUM_Cost; i++) n["cost"] = cost;
{
jsonCosts[COST_LABELS[i]] = costs[i];
}
n["costs"] = jsonCosts;
jsonNeighbors.append(n); jsonNeighbors.append(n);
} }
root["id"] = id; root["id"] = id;
+3 -7
View File
@@ -282,14 +282,10 @@ namespace StepParity {
State state; State state;
// Connections to, and the cost of moving to, the connected nodes // Connections to, and the cost of moving to, the connected nodes
std::unordered_map<StepParityNode *, float*> neighbors; std::unordered_map<StepParityNode *, float> neighbors;
~StepParityNode() ~StepParityNode()
{ {
for(auto neighbor: neighbors)
{
delete[] neighbor.second;
}
neighbors.clear(); neighbors.clear();
} }
StepParityNode(const State &_state) StepParityNode(const State &_state)
@@ -342,9 +338,9 @@ namespace StepParity {
/// @return /// @return
StepParityNode *addOrGetExistingNode(const State &state); StepParityNode *addOrGetExistingNode(const State &state);
void addEdge(StepParityNode* from, StepParityNode* to, float* costs) void addEdge(StepParityNode* from, StepParityNode* to, float cost)
{ {
from->neighbors[to] = costs; from->neighbors[to] = cost;
} }
int nodeCount() const int nodeCount() const
+4 -9
View File
@@ -61,10 +61,10 @@ void StepParityGenerator::buildStateGraph()
for(auto it = PermuteFootPlacements->begin(); it != PermuteFootPlacements->end(); it++) for(auto it = PermuteFootPlacements->begin(); it != PermuteFootPlacements->end(); it++)
{ {
State resultState = initResultState(state, row, *it); State resultState = initResultState(state, row, *it);
float* costs = costCalculator.getActionCost(&state, &resultState, rows, i); float cost = costCalculator.getActionCost(&state, &resultState, rows, i);
resultState.calculateHashes(); resultState.calculateHashes();
StepParityNode *resultNode = graph.addOrGetExistingNode(resultState); StepParityNode *resultNode = graph.addOrGetExistingNode(resultState);
graph.addEdge(initialNode, resultNode, costs); graph.addEdge(initialNode, resultNode, cost);
if(std::find(uniqueStates.begin(), uniqueStates.end(), resultState) == uniqueStates.end()) if(std::find(uniqueStates.begin(), uniqueStates.end(), resultState) == uniqueStates.end())
{ {
uniqueStates.push_back(resultState); uniqueStates.push_back(resultState);
@@ -90,12 +90,7 @@ void StepParityGenerator::buildStateGraph()
{ {
State state = previousStates.front(); State state = previousStates.front();
StepParityNode *node = graph.addOrGetExistingNode(state); StepParityNode *node = graph.addOrGetExistingNode(state);
float * emptyCosts = new float[NUM_Cost]; graph.addEdge(node, endNode, 0);
for(int i = 0; i < NUM_Cost; i++)
{
emptyCosts[i] = 0;
}
graph.addEdge(node, endNode, emptyCosts);
previousStates.pop(); previousStates.pop();
} }
} }
@@ -247,7 +242,7 @@ std::vector<int> StepParityGenerator::computeCheapestPath()
for(auto neighbor: node->neighbors) for(auto neighbor: node->neighbors)
{ {
int neighbor_id = neighbor.first->id; int neighbor_id = neighbor.first->id;
float weight = neighbor.second[COST_TOTAL]; float weight = neighbor.second;
if(cost[i] + weight < cost[neighbor_id]) if(cost[i] + weight < cost[neighbor_id])
{ {
cost[neighbor_id] = cost[i] + weight; cost[neighbor_id] = cost[i] + weight;