Beispiel #1
0
 def get_features(self):
     # create a copy to avoid polluting the current one.
     # returns constraint_features, edge_features, variable_features
     m = self._copy_model()
     ret = get_features_from_scip_model(m)
     del_scip_model(m)
     return ret
Beispiel #2
0
    def get_feasible_solution(self):
        # get any feasible solution.

        fixed_model = self._copy_model()
        # No objective -- only feasibility
        fixed_model.setObjective(scip.Expr())

        fixed_model.optimize()
        assert fixed_model.getStatus() == 'optimal', fixed_model.getStatus()

        ass = {
            't_' + var.name: fixed_model.getVal(var)
            for var in fixed_model.getVars()
        }
        del_scip_model(fixed_model)
        return ass
Beispiel #3
0
    def reset(self):
        # fix the graph for a few episodes to reduce load on the disk while loading datasets.
        if self._n_resets % self._sample_every_n_resets == 0:
            self.milp, self._sol, self._obj = self._sample()
            self.mip = SCIPMIPInstance.fromMIPInstance(self.milp.mip)
            # clean up previous scip model
            if hasattr(self, '_sub_mip_model'):
                del_scip_model(self._sub_mip_model)
            self._sub_mip_model = self.mip.get_scip_model()
        sol, obj, mip = self._sol, self._obj, self.mip
        c_f, e_f, v_f = load_pickled_features(self._dataset,
                                              self._dataset_type,
                                              self._milp_choice)
        self._var_names = var_names = list(
            map(lambda v: v.name.lstrip('t_'), mip.vars))
        # call init_features before init_ds
        self._init_features(var_names, v_f)
        self._init_ds()
        self._constraint_nodes = np.float32(c_f['values'])
        self._change_sol(sol, obj, sol, obj)
        self._best_quality = self._primal_gap(obj)
        self._final_quality = self._best_quality
        self._qualities = [self._best_quality]

        self._unfixed_variables = filter(lambda v: v.vtype() == 'CONTINUOUS',
                                         mip.vars)
        self._unfixed_variables = set(
            map(lambda v: v.name.lstrip('t_'), self._unfixed_variables))
        self._vars_unfixed_so_far = []

        # static features computed only once per episode.
        if self.config.make_obs_for_graphnet:
            raise Exception('Not yet supported!')
            self._static_graph_features = self._encode_static_graph_features()
        elif self.config.make_obs_for_bipartite_graphnet:
            self._static_graph_features = self._encode_static_bipartite_graph_features(
                e_f)
        self._n_resets += 1
        return restart(self._observation())
Beispiel #4
0
def get_sample(dataset, dataset_type, graph_idx):
  dataset_path = DATASET_PATH[dataset]

  with open(os.path.join(dataset_path, dataset_type, f'{graph_idx}.pkl'), 'rb') as f:
    milp = pickle.load(f)

  if milp.get('optimal_lp_sol', None) is None:
    if dataset in DATASET_INFO_PATH:
      with open(
          os.path.join(DATASET_INFO_PATH[dataset], 'aux_info', dataset_type, f'{graph_idx}.pkl'),
          'rb') as f:
        pkl = pickle.load(f)
        milp.optimal_lp_sol = pkl['optimal_lp_sol']
    else:
      solver = Model()
      solver.hideOutput()
      relax_integral_constraints(milp.mip).add_to_scip_solver(solver)
      solver.optimize()
      assert solver.getStatus() == 'optimal', solver.getStatus()
      ass = {var.name: solver.getVal(var) for var in solver.getVars()}
      milp = ConfigDict(milp)
      milp.optimal_lp_sol = ass
      del_scip_model(solver)
  return milp
Beispiel #5
0
 def __del__(self):
     del_scip_model(self.model)