Skip to content

Instantly share code, notes, and snippets.

@jamiecook
Last active August 29, 2015 14:13
Show Gist options
  • Save jamiecook/cae5deff71a5ac95b4db to your computer and use it in GitHub Desktop.
Save jamiecook/cae5deff71a5ac95b4db to your computer and use it in GitHub Desktop.
// SETUP CODE
// ALPHA < 0, BETA > 0?
bool has_min_deterrence = (alpha < 0 && beta > 0);
float turn_cost, min_utility = 0.0;
if (has_min_deterrence)
{
turn_cost = -alpha / beta;
min_utility = beta * turn_cost + alpha * log(turn_cost);
}
for (int i = 0; i < numCentroids; ++i)
{
if (attractions.at(i) > 0) log_attractions.at(i) = log(attractions.at(i));
float* skim_row = skim.Lock(i);
float* adj_skim_row = adj_skim.Lock(i);
float* log_skim_row = log_skim.Lock(i);
for (int j = 0; j < numCentroids; ++j)
{
float skim_value = has_min_deterrence ? std::min(skim_row[j], turn_cost) : skim_row[j];
if (skim_value < 0.0)
{
string str = "skim value was <= 0 at (" + ToString(i) + "," + ToString(j) + ")";
otRuby->rb_raise(otRuby->rb_eRuntimeError(), str.c_str());
}
adj_skim_row[j] = skim_value * destination_cost_factors.at(j) + destination_constants.at(j);
log_skim_row[j] = log(skim_value);
}
}
// Then main loop was this:
for (int col = 0; col < numCentroids; ++col)
{
float skim_value = skim_row[col];
if (attractions.at(col) == 0 || skim_value == 99999)
{
utilities.at(col) = 99999;
continue;
}
float adj_skim_value = adj_skim_row[col];
float log_skim_value = log_skim_row[col];
if (has_min_deterrence)
{
if (skim_value >= turn_cost)
utilities.at(col) = 99999.0;
else
{
float cost_based_utility = alpha * log_skim_value + beta * adj_skim_value - min_utility;
if (cost_based_utility < 0)
utilities.at(col) = 99999;
else
{
utilities.at(col) = log_attractions.at(col) + cost_based_utility;
}
}
}
else
{
utilities.at(col) = log_attractions.at(col) + alpha * log_skim_value + beta * adj_skim_value;
}
if (utilities.at(col) != 99999 && utilities.at(col) > maxV)
{
maxV = utilities.at(col);
destinationWithMaxV = col;
}
}
// Becomes, this:
for (int col = 0; col < numCentroids; ++col)
{
float skim_value = skim_row[col];
if (attractions.at(col) == 0 || skim_value == 99999)
{
utilities.at(col) = 99999;
continue;
}
float cost_based_utility = alpha * log_skim_row[col] + beta * adj_skim_row[col] - min_utility;
utilities.at(col) = (cost_based_utility < 0) ? 99999 : log_attractions.at(col) + cost_based_utility;
if (utilities.at(col) != 99999 && utilities.at(col) > maxV)
{
maxV = utilities.at(col);
destinationWithMaxV = col;
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment