Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Tetrisched - Pass in utility to ChooseExpression and MalleableChooseExpression #62

Merged
merged 6 commits into from
Oct 31, 2023
Merged
Show file tree
Hide file tree
Changes from 5 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
9 changes: 7 additions & 2 deletions schedulers/tetrisched/include/tetrisched/Expression.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -319,13 +319,16 @@ class ChooseExpression : public Expression {
Time duration;
/// The end time of the choice represented by this Expression.
Time endTime;
// The utility of the choice represented by this Expression.
TETRISCHED_ILP_TYPE utility;
/// The variables that represent the choice of each Partition for this
/// Expression.
std::unordered_map<uint32_t, VariablePtr> partitionVariables;

public:
ChooseExpression(std::string taskName, Partitions resourcePartitions,
uint32_t numRequiredMachines, Time startTime, Time duration);
uint32_t numRequiredMachines, Time startTime, Time duration,
TETRISCHED_ILP_TYPE utility);
void addChild(ExpressionPtr child) override;
ParseResultPtr parse(SolverModelPtr solverModel,
Partitions availablePartitions,
Expand All @@ -349,6 +352,8 @@ class MalleableChooseExpression : public Expression {
Time endTime;
/// The granularity at which the rectangle choices are to be made.
Time granularity;
// The utility of the choice represented by this Expression.
TETRISCHED_ILP_TYPE utility;
/// The variables that represent the choice of machines from each
/// Partition at each time corresponding to this Expression.
std::unordered_map<std::pair<uint32_t, Time>, VariablePtr,
Expand All @@ -358,7 +363,7 @@ class MalleableChooseExpression : public Expression {
public:
MalleableChooseExpression(std::string taskName, Partitions resourcePartitions,
uint32_t resourceTimeSlots, Time startTime,
Time endTime, Time granularity);
Time endTime, Time granularity, TETRISCHED_ILP_TYPE utility);
void addChild(ExpressionPtr child) override;
ParseResultPtr parse(SolverModelPtr solverModel,
Partitions availablePartitions,
Expand Down
1 change: 1 addition & 0 deletions schedulers/tetrisched/include/tetrisched/Partition.hpp
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
#ifndef _TETRISCHED_PARTITION_HPP_
#define _TETRISCHED_PARTITION_HPP_
#include <optional>
#include <unordered_map>
#include <vector>

Expand Down
9 changes: 5 additions & 4 deletions schedulers/tetrisched/python/Expressions.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -105,10 +105,10 @@ void defineSTRLExpressions(py::module_& tetrisched_m) {
"ChooseExpression")
.def(py::init([](std::string taskName, tetrisched::Partitions partitions,
uint32_t numRequiredMachines, tetrisched::Time startTime,
tetrisched::Time duration) {
tetrisched::Time duration, TETRISCHED_ILP_TYPE utility) {
return std::make_shared<tetrisched::ChooseExpression>(
taskName, partitions, numRequiredMachines, startTime,
duration);
duration, utility);
}),
"Initializes a ChooseExpression for the given task to be placed on "
"`numRequiredMachines` from the given partition at the given "
Expand All @@ -120,10 +120,11 @@ void defineSTRLExpressions(py::module_& tetrisched_m) {
tetrisched_m, "MalleableChooseExpression")
.def(py::init([](std::string taskName, tetrisched::Partitions partitions,
uint32_t resourceTimeSlots, tetrisched::Time startTime,
tetrisched::Time endTime, tetrisched::Time granularity) {
tetrisched::Time endTime, tetrisched::Time granularity,
TETRISCHED_ILP_TYPE utility) {
return std::make_shared<tetrisched::MalleableChooseExpression>(
taskName, partitions, resourceTimeSlots, startTime, endTime,
granularity);
granularity, utility);
}),
"Initializes a MalleableChooseExpression for the given task to be "
"placed on the given partitions at the given startTime, "
Expand Down
25 changes: 15 additions & 10 deletions schedulers/tetrisched/src/Expression.cpp
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
#include "tetrisched/Expression.hpp"

#include <algorithm>
#include <limits>
sukritkalra marked this conversation as resolved.
Show resolved Hide resolved

namespace tetrisched {

Expand Down Expand Up @@ -271,13 +272,15 @@ std::optional<SolutionResultPtr> Expression::getSolution() const {
ChooseExpression::ChooseExpression(std::string taskName,
Partitions resourcePartitions,
uint32_t numRequiredMachines, Time startTime,
Time duration)
Time duration,
TETRISCHED_ILP_TYPE utility)
: Expression(taskName, ExpressionType::EXPR_CHOOSE),
resourcePartitions(resourcePartitions),
numRequiredMachines(numRequiredMachines),
startTime(startTime),
duration(duration),
endTime(startTime + duration) {}
endTime(startTime + duration),
utility(utility) {}

void ChooseExpression::addChild(ExpressionPtr child) {
throw tetrisched::exceptions::ExpressionConstructionException(
Expand Down Expand Up @@ -368,16 +371,16 @@ ParseResultPtr ChooseExpression::parse(
solverModel->addConstraint(std::move(fulfillsDemandConstraint));

// Construct the Utility function for this Choose expression.
auto utility =
auto utilityFunction =
std::make_shared<ObjectiveFunction>(ObjectiveType::OBJ_MAXIMIZE);
utility->addTerm(1, isSatisfiedVar);
utilityFunction->addTerm(utility, isSatisfiedVar);

// Construct the return value.
parsedResult->type = ParseResultType::EXPRESSION_UTILITY;
parsedResult->startTime = startTime;
parsedResult->endTime = endTime;
parsedResult->indicator = isSatisfiedVar;
parsedResult->utility = std::move(utility);
parsedResult->utility = std::move(utilityFunction);
return parsedResult;
}

Expand Down Expand Up @@ -415,14 +418,16 @@ SolutionResultPtr ChooseExpression::populateResults(
/* Method definitions for GeneralizedChoose */
MalleableChooseExpression::MalleableChooseExpression(
std::string taskName, Partitions resourcePartitions,
uint32_t resourceTimeSlots, Time startTime, Time endTime, Time granularity)
uint32_t resourceTimeSlots, Time startTime, Time endTime, Time granularity,
TETRISCHED_ILP_TYPE utility)
: Expression(taskName, ExpressionType::EXPR_MALLEABLE_CHOOSE),
resourcePartitions(resourcePartitions),
resourceTimeSlots(resourceTimeSlots),
startTime(startTime),
endTime(endTime),
granularity(granularity),
partitionVariables() {}
partitionVariables(),
utility(utility) {}

void MalleableChooseExpression::addChild(ExpressionPtr child) {
throw tetrisched::exceptions::ExpressionConstructionException(
Expand Down Expand Up @@ -713,16 +718,16 @@ ParseResultPtr MalleableChooseExpression::parse(
solverModel->addConstraint(std::move(endTimeConstraint));

// Construct the Utility function for this Choose expression.
auto utility =
auto utilityFunction =
std::make_shared<ObjectiveFunction>(ObjectiveType::OBJ_MAXIMIZE);
utility->addTerm(1, isSatisfiedVar);
utilityFunction->addTerm(utility, isSatisfiedVar);

// Construct the return value.
parsedResult->type = ParseResultType::EXPRESSION_UTILITY;
parsedResult->startTime = startTimeVariable;
parsedResult->endTime = endTimeVariable;
parsedResult->indicator = isSatisfiedVar;
parsedResult->utility = std::move(utility);
parsedResult->utility = std::move(utilityFunction);
return parsedResult;
}

Expand Down
34 changes: 17 additions & 17 deletions schedulers/tetrisched/test/test_expression.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -12,9 +12,9 @@
TEST(Expression, TestChooseExpressionIsLeaf) {
tetrisched::Partitions partitions = tetrisched::Partitions();
auto chooseExpression = std::make_unique<tetrisched::ChooseExpression>(
"task1", partitions, 0, 0, 10);
"task1", partitions, 0, 0, 10, 1);
auto chooseExpression2 = std::make_unique<tetrisched::ChooseExpression>(
"task1", partitions, 0, 0, 10);
"task1", partitions, 0, 0, 10, 1);
EXPECT_THROW(chooseExpression->addChild(std::move(chooseExpression2)),
tetrisched::exceptions::ExpressionConstructionException);
}
Expand All @@ -23,10 +23,10 @@ TEST(Expression, TestMinExpressionIsNOTLeaf) {
tetrisched::Partitions partitions = tetrisched::Partitions();
tetrisched::ExpressionPtr chooseExpression =
std::make_shared<tetrisched::ChooseExpression>("task1", partitions, 0, 0,
10);
10, 1);
tetrisched::ExpressionPtr chooseExpression2 =
std::make_shared<tetrisched::ChooseExpression>("task1", partitions, 0, 0,
10);
10, 1);

tetrisched::ExpressionPtr minExpression =
std::make_shared<tetrisched::MinExpression>("TEST_MIN");
Expand All @@ -44,7 +44,7 @@ TEST(Expression, TestMaxExpressionOnlyAllowsChooseExpressionChildren) {
// Ensure ChooseExpression can be added to MaxExpression.
tetrisched::ExpressionPtr chooseExpression =
std::make_shared<tetrisched::ChooseExpression>("task1", partitions, 0, 0,
10);
10, 1);
tetrisched::ExpressionPtr maxExpression =
std::make_shared<tetrisched::MaxExpression>("TestMax");
EXPECT_NO_THROW(maxExpression->addChild(chooseExpression))
Expand All @@ -70,10 +70,10 @@ TEST(Expression, TestLessThanEnforcesOrdering) {
// Construct the choice for the two tasks.
tetrisched::ExpressionPtr chooseTask1 =
std::make_shared<tetrisched::ChooseExpression>("task1", partitions, 1, 0,
100);
100, 1);
tetrisched::ExpressionPtr chooseTask2 =
std::make_shared<tetrisched::ChooseExpression>("task2", partitions, 1,
200, 100);
200, 100, 1);

// Construct the LessThan expression.
tetrisched::ExpressionPtr lessThanExpression =
Expand Down Expand Up @@ -151,10 +151,10 @@ TEST(Expression, TestMaxExpressionEnforcesSingleChoice) {
// Construct two choices for a task.
tetrisched::ExpressionPtr chooseTask1_1 =
std::make_shared<tetrisched::ChooseExpression>("task1", partitions, 1, 0,
100);
100, 1);
tetrisched::ExpressionPtr chooseTask1_2 =
std::make_shared<tetrisched::ChooseExpression>("task1", partitions, 1,
100, 100);
100, 100, 1);

// Constrain only one choice to actually happen.
tetrisched::ExpressionPtr maxChooseExpr =
Expand Down Expand Up @@ -197,10 +197,10 @@ TEST(Expression, TestMinExpressionEnforcesAllChildrenSatisfied) {
// Construct two choices for a task.
tetrisched::ExpressionPtr chooseTask1_1 =
std::make_shared<tetrisched::ChooseExpression>("task1_1", partitions, 1,
0, 100);
0, 100, 1);
tetrisched::ExpressionPtr chooseTask1_2 =
std::make_shared<tetrisched::ChooseExpression>("task1_2", partitions, 1,
100, 100);
100, 100, 1);

// Constrain both choices to actually happen.
tetrisched::ExpressionPtr minChooseExpr =
Expand Down Expand Up @@ -248,10 +248,10 @@ TEST(Expression, TestMinExpressionEnforcesNoneSatisfied) {
// Construct two choices for a task.
tetrisched::ExpressionPtr chooseTask1_1 =
std::make_shared<tetrisched::ChooseExpression>("task1_1", partitions, 1,
0, 100);
0, 100, 1);
tetrisched::ExpressionPtr chooseTask1_2 =
std::make_shared<tetrisched::ChooseExpression>("task1_2", partitions, 1,
50, 100);
50, 100, 1);

// Constrain both choices to actually happen.
tetrisched::ExpressionPtr minChooseExpr =
Expand Down Expand Up @@ -296,7 +296,7 @@ TEST(Expression, TestScaleExpressionDoublesUtility) {
// Construct the choice for a task.
tetrisched::ExpressionPtr chooseExpression =
std::make_shared<tetrisched::ChooseExpression>("task1", partitions, 1, 0,
10);
10, 1);
// Scale the utility by 2.
tetrisched::ExpressionPtr scaleExpression =
std::make_shared<tetrisched::ScaleExpression>("TEST_SCALE", 4);
Expand Down Expand Up @@ -345,7 +345,7 @@ TEST(Expression, TestAllocationExpressionFailsChoice) {
// Construct the Choice for a second task.
tetrisched::ExpressionPtr chooseExpression =
std::make_shared<tetrisched::ChooseExpression>("task2", partitions, 1, 10,
20);
20, 1);

// Try to meet both of the Expressions.
tetrisched::ExpressionPtr minExpression =
Expand Down Expand Up @@ -393,7 +393,7 @@ TEST(Expression, TestMalleableChooseExpressionConstruction) {
// Construct the MalleableChooseExpression.
tetrisched::ExpressionPtr malleableChooseExpression =
std::make_shared<tetrisched::MalleableChooseExpression>(
"task1", partitions, 15, 5, 10, 1);
"task1", partitions, 15, 5, 10, 1, 1);

// Construct an ObjectiveExpression.
tetrisched::ExpressionPtr objectiveExpression =
Expand Down Expand Up @@ -431,7 +431,7 @@ TEST(Expression, TestMalleableChooseExpressionConstructsVariableRectangles) {
// usage.
tetrisched::ExpressionPtr malleableChooseExpression =
std::make_shared<tetrisched::MalleableChooseExpression>(
"task2", partitions, 10, 4, 10, 1);
"task2", partitions, 10, 4, 10, 1, 1);

// Construct an ObjectiveExpression.
tetrisched::ExpressionPtr objectiveExpression =
Expand Down
15 changes: 14 additions & 1 deletion schedulers/tetrisched_scheduler.py
Original file line number Diff line number Diff line change
@@ -1,7 +1,8 @@
import time
from typing import List, Mapping, Optional, Set

import absl # noqa: F401
import absl # noqa: F401
import numpy as np
import tetrisched_py as tetrisched

from schedulers import BaseScheduler
Expand Down Expand Up @@ -335,6 +336,17 @@ def construct_task_strl(
time_discretizations = self._get_time_discretizations_until(
current_time, task.deadline - execution_strategy.runtime
)

time_range = [time_discretization.time for time_discretization in time_discretizations]
# The placement reward skews the reward towards placing the task earlier.
# We interpolate the time range to a range between 2 and 1 and use that to
# skew the reward towards earlier placement.
placement_rewards = dict(
zip(
time_range,
np.interp(time_range, (min(time_range), max(time_range)), (2, 1)),
)
)
for placement_time in time_discretizations:
# Construct a ChooseExpression for placement at this time.
# TODO (Sukrit): We just assume for now that all Slots are the same and
Expand All @@ -345,6 +357,7 @@ def construct_task_strl(
num_slots_required,
placement_time.time,
execution_strategy.runtime.to(EventTime.Unit.US).time,
placement_rewards[placement_time.time],
)

# Register this expression with the MAX expression.
Expand Down