Exemple #1
0
    def export_as_cpo_s(model, model_name: Optional[str] = None, local_root: Optional[str] = None, copy_to_csv: bool = False, **kwargs) -> str:
        """Export .cpo file of model in the 'DSX_PROJECT_DIR.datasets' folder.
        It can write a copy as a .csv file, so it can be exported to a local machine.
        If not in DSX, it will write to the local file system in the 'local_root/datasets' directory.

        Args:
            model (docplex.cp.model): The CPLEX model to be exported
            model_name (str): name of .lp file. If none specified, will use the model.name.
                Specify if the model.name is not a valid file-name.
            local_root (str): name of local directory. Will write .lp file here, if not in DSX
            copy_to_csv (bool): If true, will create a copy of the file with the extension `.csv`.
            **kwargs: Passed to model.export_model
        Returns:
            path (str) path to cpo file
        Raises:
            ValueError if root directory can't be established.
        """
        # Get model name:
        if model_name is None:
            model_name = model.name
        # Get root directory:
        sm = ScenarioManager(local_root=local_root)  # Just to call the get_root_directory()
        # root_dir = sm.get_root_directory()
        datasets_dir = sm.get_data_directory()
        # Write regular cpo-file:
        cpo_file_name_1 = os.path.join(datasets_dir, model_name + '.cpo')
        model.export_model(cpo_file_name_1)  # Writes the .cpo file
        # Copy to csv
        if copy_to_csv:
            cpo_file_name_2 = os.path.join(datasets_dir, model_name + '_to_csv.cpo')
            csv_file_name_2 = os.path.join(datasets_dir, model_name + '_cpo.csv')
            model.export_as_lp(cpo_file_name_2)
            os.rename(cpo_file_name_2, csv_file_name_2)
        # Return
        return cpo_file_name_1
Exemple #2
0
    def export_as_lp_s(model: docplex.mp.model,
                       model_name: Optional[str] = None,
                       local_root: Optional[str] = None,
                       copy_to_csv: bool = False) -> str:
        """Export .lp file of model in the 'DSX_PROJECT_DIR.datasets' folder.
        It can write a copy as a .csv file, so it can be exported to a local machine.
        If not in WSL, it will write to the local file system in the 'local_root/datasets' directory.

        Args:
            model (docplex.mp.model): The CPLEX model to be exported
            model_name (str): name of .lp file. If none specified, will use the model.name.
                Specify if the model.name is not a valid file-name.
            local_root (str): name of local directory. Will write .lp file here, if not in WSL
            copy_to_csv (bool): DEPRECATED. If true, will create a copy of the file with the extension `.csv`.
        Returns:
            path (str) path to lp file
        Raises:
            ValueError if root directory can't be established.
        """
        # Get model name:
        if model_name is None:
            model_name = model.name
        # Get root directory:
        sm = ScenarioManager(
            local_root=local_root)  # Just to call the get_root_directory()
        # root_dir = sm.get_root_directory()
        datasets_dir = sm.get_data_directory()
        # Write regular lp-file:
        # lp_file_name_1 = os.path.join(root_dir, 'datasets', model_name + '.lp')
        lp_file_name_1 = os.path.join(datasets_dir, model_name + '.lp')
        model.export_as_lp(lp_file_name_1)  # Writes the .lp file

        if ScenarioManager.env_is_cpd25():
            ScenarioManager.add_data_file_to_project_s(lp_file_name_1,
                                                       model_name + '.lp')
        # Copy to csv (Not supported in CPD25. Not necessary.):
        elif copy_to_csv:
            # lp_file_name_2 = os.path.join(root_dir, 'datasets', model_name + '_to_csv.lp')
            # csv_file_name_2 = os.path.join(root_dir, 'datasets', model_name + '_lp.csv')
            lp_file_name_2 = os.path.join(datasets_dir,
                                          model_name + '_to_csv.lp')
            csv_file_name_2 = os.path.join(datasets_dir,
                                           model_name + '_lp.csv')
            model.export_as_lp(lp_file_name_2)
            os.rename(lp_file_name_2, csv_file_name_2)
        # Return
        return lp_file_name_1
 def load_data_from_scenario(self, scenario_name):
     """TODO: see of by re-using a Client, this can be done faster"""
     sm = ScenarioManager(self.model_name, scenario_name, self.local_root, self.project_id,
                          self.project_access_token, self.project)
     inputs, outputs = sm.load_data_from_scenario()
     return inputs, outputs