@File : init.py @Time : 2024/06/14 13:45:31 @Author : Alejandro Marrero @Version : 1.0 @Contact : amarrerd@ull.edu.es @License : (C)Copyright 2024, Alejandro Marrero @Desc : None

save_results_to_files(base_dir, filename_pattern, result, variables_names=None, descriptor_names=None, lazily=False, only_instances=False, files_format='parquet')

Saves the results of the generation to CSV files. Args: filename_pattern (str): Pattern for the filenames. result (GenResult): Result of the generation. variables_names (Sequence[str]): Names of the variables. lazily (bool, optional): Whether the instances inside the result object should be collected as a LazyFrame or a DataFrame. Defaults ot False. only_instances (bool): Generate only the files with the resulting instances. Default True. If False, it would generate an history and arhice_metrics files. files_format (Literal[str] = "csv", "parquet"): Format to store the resulting instances file. Parquet is the most efficient for large datasets.

Source code in digneapy/utils/save_data.py
 62
 63
 64
 65
 66
 67
 68
 69
 70
 71
 72
 73
 74
 75
 76
 77
 78
 79
 80
 81
 82
 83
 84
 85
 86
 87
 88
 89
 90
 91
 92
 93
 94
 95
 96
 97
 98
 99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
def save_results_to_files(
    base_dir: Path,
    filename_pattern: str,
    result: GenerationResult,
    variables_names: Optional[Sequence[str]] = None,
    descriptor_names: Optional[Sequence[str]] = None,
    lazily: bool = False,
    only_instances: bool = False,
    files_format: Literal["csv", "parquet"] = "parquet",
):
    """Saves the results of the generation to CSV files.
    Args:
        filename_pattern (str): Pattern for the filenames.
        result (GenResult): Result of the generation.
        variables_names (Sequence[str]): Names of the variables.
        lazily (bool, optional): Whether the instances inside the result object
            should be collected as a LazyFrame or a DataFrame. Defaults ot False.
        only_instances (bool): Generate only the files with the resulting instances.
            Default True. If False, it would generate an history and arhice_metrics files.
        files_format (Literal[str] = "csv", "parquet"): Format to store the resulting instances file. Parquet is the most efficient for large datasets.
    """
    if files_format not in ("csv", "parquet"):
        warnings.warn(
            f"Unrecognised file format: {files_format}. Selecting parquet as fallback.",
            category=RuntimeWarning,
            stacklevel=2,
        )
        files_format = "parquet"
    _saving_fn = __saving_methods[files_format]
    if len(result.instances) != 0:
        frames = pl.concat(
            [
                instance.to_df(
                    variables_names=variables_names,
                    descriptor_names=descriptor_names,
                    portfolio_names=result.solvers,
                    lazy=lazily,
                )
                for instance in result.instances
            ],
            how="vertical_relaxed",
        )
        _saving_fn(
            frames=frames,
            base_dir=base_dir,
            filename_pattern=filename_pattern,
            lazily=lazily,
        )

        if not only_instances:
            result.history.to_df().write_csv(
                base_dir / f"{filename_pattern}_history.csv"
            )
            if result.metrics is not None:
                result.metrics.write_csv(
                    base_dir / f"{filename_pattern}_archive_metrics.csv"
                )
    else:
        warnings.warn(
            "Archive in Generation result is empty. Nothing to do in save_results_to_files.",
            category=RuntimeWarning,
            stacklevel=2,
        )

sort_knapsack_instances(instances)

sort_knapsack_instances(
    instances: np.ndarray,
) -> np.ndarray
sort_knapsack_instances(
    instances: Sequence[Instance],
) -> List[Instance]
sort_knapsack_instances(
    instances: Archive | CVTArchive | GridArchive,
) -> List[Instance]

Sorts a collection of Knapsack Instances Genotypes based on lexicograph order by (w_i, p_i)

Parameters:
  • instances (ndarray | Sequence[Instance]) –

    Instances to sort

Raises:
  • ValueError

    If the dimension of the genotypes (minus Q) is not even. Note that KP instances should contain N pairs of values plus the capacity.

Returns:
  • ndarray | List[Instance]

    np.ndarray | Sequence[Instance]: Sorted instances

Source code in digneapy/utils/sorting.py
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
def sort_knapsack_instances(
    instances: np.ndarray | Sequence[Instance] | Archive | CVTArchive | GridArchive,
) -> np.ndarray | List[Instance]:
    """Sorts a collection of Knapsack Instances Genotypes based on lexicograph order by (w_i, p_i)

    Args:
        instances (np.ndarray | Sequence[Instance]): Instances to sort

    Raises:
        ValueError: If the dimension of the genotypes (minus Q) is not even. Note that KP instances should contain N pairs of values plus the capacity.

    Returns:
        np.ndarray | Sequence[Instance]: Sorted instances
    """
    genotypes = np.empty(0)
    if isinstance(instances, np.ndarray) and (instances.shape[1] - 1) % 2 != 0:
        raise ValueError(
            f"Something is wrong with these KP instances. Shape 1 should be even and got {instances.shape[1]}"
        )
    elif dimension := (len(instances[0]) - 1) % 2 != 0:
        raise ValueError(
            f"Something is wrong with these KP instances. Shape 1 should be even and got {dimension}"
        )

    genotypes = np.asarray(instances, copy=True)
    M, N = genotypes.shape

    pairs = genotypes[:, 1:].reshape(M, -1, 2)
    order = np.lexsort((pairs[:, :, 1], pairs[:, :, 0]), axis=1)
    sorted_pairs = np.take_along_axis(pairs, order[:, :, None], axis=1)
    genotypes[:, 1:] = sorted_pairs.reshape(M, -1)

    if isinstance(instances, np.ndarray):
        return genotypes
    else:
        return [
            instances[i].clone_with(variables=genotypes[i])
            for i in range(len(instances))
        ]