Commit bf81dc3d authored by Martin Jonáš's avatar Martin Jonáš
Browse files

Apply automatic ruff fixes.

parent 9997d78f
Loading
Loading
Loading
Loading
+4 −3
Original line number Diff line number Diff line
import argparse
from pathlib import Path

import multiagent_steady_state_synthesis.graph as graph
import multiagent_steady_state_synthesis.objectives as objectives
from multiagent_steady_state_synthesis.strategy_synthesis.multi import synthesize_strategy
from multiagent_steady_state_synthesis import graph, objectives
from multiagent_steady_state_synthesis.strategy_synthesis.multi import (
    synthesize_strategy,
)


def get_opts() -> argparse.Namespace:
+1 −2
Original line number Diff line number Diff line
from dataclasses import dataclass
from pathlib import Path

from math import isclose
from pathlib import Path

from multiagent_steady_state_synthesis.graph import Node

+7 −5
Original line number Diff line number Diff line
@@ -2,13 +2,15 @@ from copy import deepcopy

from multiagent_steady_state_synthesis.graph import Graph, Node
from multiagent_steady_state_synthesis.graph_utils.cyclic_classes import (
    CyclicClasses,
    compute_cyclic_classes,
    node_to_cyclic_class_index,
    CyclicClasses
)

from multiagent_steady_state_synthesis.objectives import Objectives
from multiagent_steady_state_synthesis.strategy import MultiAgentStrategy, SingleAgentStrategy
from multiagent_steady_state_synthesis.strategy import (
    MultiAgentStrategy,
    SingleAgentStrategy,
)

ColorCyclicClassIntersections = list[dict[int, list[Node]]]

@@ -30,7 +32,7 @@ def compute_color_cc_nodes(objectives: Objectives, node_to_cc_idx: dict[Node, in
    return color_cc_intersections


class StrategyValue(object):
class StrategyValue:
    def __init__(
        self,
        graph: Graph,
@@ -53,7 +55,7 @@ class StrategyValue(object):
            for _ in range(self.num_of_colors)
        ]

    def copy(self) -> 'StrategyValue':
    def copy(self) -> "StrategyValue":
        return StrategyValue(
            graph=self.graph,
            num_of_colors=self.num_of_colors,
+9 −5
Original line number Diff line number Diff line
import multiagent_steady_state_synthesis.strategy_synthesis.single as single

from multiagent_steady_state_synthesis.graph_utils.cyclic_classes import compute_cyclic_classes
from multiagent_steady_state_synthesis.graph import Graph
from multiagent_steady_state_synthesis.graph_utils.cyclic_classes import (
    compute_cyclic_classes,
)
from multiagent_steady_state_synthesis.objectives import Objectives
from multiagent_steady_state_synthesis.strategy import MultiAgentStrategy, SingleAgentStrategy
from multiagent_steady_state_synthesis.strategy import (
    MultiAgentStrategy,
    SingleAgentStrategy,
)
from multiagent_steady_state_synthesis.strategy_evaluator import create_strategy_value
from multiagent_steady_state_synthesis.strategy_synthesis import single


def is_satisfied(objectives: Objectives, values: list[float]) -> bool:
@@ -32,7 +36,7 @@ def synthesize_strategy(
        num_agents = len(multi_strategy.agent_strategies)
        print(f"Finding best strategy for agent {num_agents}")

        best_distance = float('inf')
        best_distance = float("inf")
        best_value = value
        best_strategy: None | SingleAgentStrategy = None

+9 −6
Original line number Diff line number Diff line
@@ -6,7 +6,10 @@ from gurobipy import GRB
from multiagent_steady_state_synthesis.graph import Graph, Node
from multiagent_steady_state_synthesis.objectives import Objectives
from multiagent_steady_state_synthesis.strategy import SingleAgentStrategy
from multiagent_steady_state_synthesis.strategy_evaluator import StrategyValue, ColorCyclicClassIntersections
from multiagent_steady_state_synthesis.strategy_evaluator import (
    ColorCyclicClassIntersections,
    StrategyValue,
)


class GurobiStrategySynthesizer:
@@ -29,7 +32,7 @@ class GurobiStrategySynthesizer:
    def _declare_color_time_vars(self, num_of_colors: int, period: int) -> None:
        for color in range(num_of_colors):
            for time in range(period):
                name = f'{color=},{time=}'
                name = f"{color=},{time=}"
                var = self._model.addVar(vtype=GRB.CONTINUOUS, name=name)
                self._color_time_vars[(color, time)] = var

@@ -95,14 +98,14 @@ class GurobiStrategySynthesizer:
            current_value = value.get_color_value(color_idx=color_index)

            if current_value >= target_value:
                return
                continue

            cur_vals = value.get_probs_no_visit_of_color(color_idx=color_index)
            new_vals = [self._color_time_vars[(color_index, (time + cc_index) % period)] for time in range(period)]

            self._model.addConstr(
                1 - sum(
                    cur_val * (1 - period * new_val) / period for cur_val, new_val in zip(cur_vals, new_vals)
                    cur_val * (1 - period * new_val) / period for cur_val, new_val in zip(cur_vals, new_vals, strict=False)
                ) <= target_value,
                "objective_{color_index}",
            )
@@ -119,13 +122,13 @@ class GurobiStrategySynthesizer:
            current_value = value.get_color_value(color_idx=color_index)

            if current_value >= target_value:
                return
                continue

            cur_vals = value.get_probs_no_visit_of_color(color_idx=color_index)
            new_vals = [self._color_time_vars[(color_index, (time + cc_index) % period)] for time in range(period)]

            result += target_value - (
                1 - sum(cur_val * (1 - period * new_val) / period for cur_val, new_val in zip(cur_vals, new_vals))
                1 - sum(cur_val * (1 - period * new_val) / period for cur_val, new_val in zip(cur_vals, new_vals, strict=False))
            )

        self._model.setObjective(result, GRB.MINIMIZE)
Loading