Skip to content

Instantly share code, notes, and snippets.

@barelyknown
Created December 9, 2015 16:25
Show Gist options
  • Save barelyknown/382c8ef2bab0206dfe29 to your computer and use it in GitHub Desktop.
Save barelyknown/382c8ef2bab0206dfe29 to your computer and use it in GitHub Desktop.
optimize-roster-1.js
function addPositionConstraints(engine) {
// for each position
for (var row = 1; row <= POSITIONS.getNumRows(); row++) {
var position = POSITIONS.getCell(
row,
POSITIONS_POSITION_COLUMN
).getValue();
var positionSpots = POSITIONS.getCell(
row,
POSITIONS_SPOTS_COLUMN
).getValue();
// add a constraint that sets the number of spots
// required for the position
var constraint = engine.addConstraint(
positionSpots,
positionSpots
);
// for each player
for (var playerRow = 1; playerRow <= PLAYERS.getNumRows(); playerRow++) {
var player = PLAYERS.getCell(
playerRow,
PLAYERS_PLAYER_COLUMN
).getValue();
var playerPosition = PLAYERS.getCell(
playerRow,
PLAYERS_POSITION_COLUMN
).getValue();
// if the player plays the position
if (playerPosition == position) {
// add the player to the position contraint
constraint.setCoefficient(player, 1);
}
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment