Exemplo n.º 1
0
 def add_objective_resource_cost(
     self, list_of_resources: List[Resource], weight=1
 ) -> Union[ArithRef, Indicator]:
     """minimise the cost of selected resources"""
     cost_indicator = self.add_indicator_resource_cost(list_of_resources)
     MinimizeObjective("", cost_indicator, weight)
     return cost_indicator
Exemplo n.º 2
0
 def create_objective(self) -> bool:
     """create optimization objectives"""
     # in case of a single value to optimize
     if self.is_multi_objective_optimization_problem:
         # Replace objectives O_i, O_j, O_k with
         # O = WiOi+WjOj+WkOk etc.
         equivalent_single_objective = Int("EquivalentSingleObjective")
         weighted_objectives = []
         for obj in self.problem_context.objectives:
             variable_to_optimize = obj.target
             weight = obj.weight
             if isinstance(obj, MaximizeObjective):
                 weighted_objectives.append(-weight * variable_to_optimize)
             else:
                 weighted_objectives.append(weight * variable_to_optimize)
         self.add_constraint(
             equivalent_single_objective == Sum(weighted_objectives))
         # create an indicator
         equivalent_indicator = Indicator("EquivalentIndicator",
                                          equivalent_single_objective)
         self.objective = MinimizeObjective("EquivalentObjective",
                                            equivalent_indicator)
         self.add_constraint(equivalent_indicator.get_assertions())
     else:
         self.objective = self.problem.context.objectives[0]
Exemplo n.º 3
0
 def add_objective_makespan(self, weight=1) -> Union[ArithRef, Indicator]:
     """makespan objective"""
     if self.fixed_horizon:
         raise ValueError(
             "Horizon constrained to be fixed, no horizon optimization possible."
         )
     MinimizeObjective("MakeSpan", self.horizon, weight)
     return self.horizon
Exemplo n.º 4
0
 def add_objective_start_earliest(self, weight=1) -> Union[ArithRef, Indicator]:
     """minimize the greatest start time, i.e. tasks are schedules
     as early as possible"""
     maxi = Int("GreatestStartTime")
     greatest_start_time = Indicator("GreatestStartTime", maxi)
     greatest_start_time.add_assertion(
         Or([maxi == task.start for task in self.context.tasks])
     )
     for tsk in self.context.tasks:
         greatest_start_time.add_assertion(maxi >= tsk.start)
     MinimizeObjective("", greatest_start_time, weight)
     return greatest_start_time
Exemplo n.º 5
0
 def add_objective_flowtime(self, weight=1) -> Union[ArithRef, Indicator]:
     """the flowtime is the sum of all ends, minimize. Be carful that
     it is contradictory with makespan"""
     task_ends = []
     for task in self.context.tasks:
         if task.optional:
             task_ends.append(task.end * task.scheduled)
         else:
             task_ends.append(task.end)
     flow_time_expr = Sum(task_ends)
     flow_time = Indicator("FlowTime", flow_time_expr)
     MinimizeObjective("", flow_time, weight)
     return flow_time
Exemplo n.º 6
0
 def add_objective_priorities(self, weight=1) -> Union[ArithRef, Indicator]:
     """optimize the solution such that all task with a higher
     priority value are scheduled before other tasks"""
     all_priorities = []
     for task in self.context.tasks:
         if task.optional:
             all_priorities.append(task.end * task.priority * task.scheduled)
         else:
             all_priorities.append(task.end * task.priority)
     priority_sum = Sum(all_priorities)
     priority_indicator = Indicator("PriorityTotal", priority_sum)
     MinimizeObjective("", priority_indicator, weight)
     return priority_indicator
Exemplo n.º 7
0
    def add_objective_flowtime_single_resource(
        self, resource, time_interval=None, weight=1
    ) -> Union[ArithRef, Indicator]:
        """Optimize flowtime for a single resource, for all the tasks scheduled in the
        time interval provided. Is ever no time interval is passed to the function, the
        flowtime is minimized for all the tasks scheduled in the workplan."""
        if time_interval is not None:
            lower_bound, upper_bound = time_interval
        else:
            lower_bound = 0
            upper_bound = self.horizon
        uid = uuid.uuid4().hex
        # for this resource, we look for the minimal starting time of scheduled tasks
        # as well as the maximum
        flowtime = Int("FlowtimeSingleResource%s_%s" % (resource.name, uid))

        flowtime_single_resource_indicator = Indicator(
            "FlowTime(%s:%i:%s)" % (resource.name, lower_bound, upper_bound), flowtime
        )
        # find the max end time in the time_interval
        maxi = Int(
            "GreatestTaskEndTimeInTimePeriodForResource%s_%s" % (resource.name, uid)
        )

        asst_max = [
            Implies(
                And(task.end <= upper_bound, task.start >= lower_bound),
                maxi == task.end,
            )
            for task in resource.busy_intervals
        ]
        flowtime_single_resource_indicator.add_assertion(Or(asst_max))
        for task in resource.busy_intervals:
            flowtime_single_resource_indicator.add_assertion(
                Implies(
                    And(task.end <= upper_bound, task.start >= lower_bound),
                    maxi >= task.end,
                )
            )

        # and the mini
        mini = Int(
            "SmallestTaskEndTimeInTimePeriodForResource%s_%s" % (resource.name, uid)
        )

        asst_min = [
            Implies(
                And(task.end <= upper_bound, task.start <= lower_bound),
                mini == task.start,
            )
            for task in resource.busy_intervals
        ]
        flowtime_single_resource_indicator.add_assertion(Or(asst_min))
        for task in resource.busy_intervals:
            flowtime_single_resource_indicator.add_assertion(
                Implies(
                    And(task.end <= upper_bound, task.start >= lower_bound),
                    mini <= task.start,
                )
            )

        # the quantity to optimize
        flowtime_single_resource_indicator.add_assertion(flowtime == maxi - mini)
        flowtime_single_resource_indicator.add_assertion(flowtime >= 0)

        MinimizeObjective("", flowtime_single_resource_indicator, weight)
        return flowtime_single_resource_indicator
Exemplo n.º 8
0
 def minimize_indicator(self, indicator: Indicator) -> MinimizeObjective:
     """Minimize indicator"""
     return MinimizeObjective("", indicator)