Ejemplo n.º 1
0
    def __getitem__(self, index):
        with gzip.open(self.sample_files[index], 'rb') as f:
            sample = pickle.load(f)

        # root data
        if sample['type'] == "root":
            sample_state, _, root_cands, root_action, root_cand_scores = sample[
                'root_state']
        else:
            root_filename = sample['root_state']
            sample_state, root_cands, root_action, root_cand_scores = _get_root_state(
                root_filename)

        c, e, v = sample_state
        root_c = c['values']
        root_ei = e['indices']
        root_ev = e['values']
        root_v = v['values']

        # data for gcnn
        obss, target, obss_feats, _ = sample['obss']
        v, gcnn_c_feats, gcnn_e = obss
        gcnn_v_feats = v[:, :19]  # gcnn features

        # target
        sample_cand_scores = obss_feats['scores']
        sample_cands = np.where(sample_cand_scores != -1)[0]
        cand_scores = sample_cand_scores[sample_cands]
        sample_action = np.where(sample_cands == target)[0][0]

        # data for mlp
        v_feats = v[sample_cands]
        v_feats = utilities._preprocess(v_feats, mode='min-max-2')

        weight = obss_feats['depth'] / sample['max_depth'] if sample[
            'max_depth'] else 1.0
        if self.weighing_scheme == "linear_decay":
            m = np.exp(-0.5) - 1
            c = 1
            weight = weight * m + c
        elif self.weighing_scheme == "sigmoidal_decay":
            weight = (1 + np.exp(-0.5)) / (1 + np.exp(weight - 0.5))
        elif self.weighing_scheme == "exponential_decay":
            weight = np.exp(weight * -0.5)
        elif self.weighing_scheme == "quadratic_decay":
            weight = (np.exp(-0.5) - 1) * weight**2 + 1
        elif self.weighing_scheme == "constant":
            weight = 1.0
        else:
            raise ValueError(f"Unknown weighing scheme:{self.weighing_scheme}")

        node_g = [
            gcnn_c_feats, gcnn_e['indices'], gcnn_e['values'], gcnn_v_feats
        ]
        node_attr = [v_feats, sample_action, sample_cands, cand_scores, weight]
        root_g = [
            root_c, root_ei, root_ev, root_v, root_cands, root_action,
            root_cand_scores
        ]
        return root_g, node_g, node_attr
Ejemplo n.º 2
0
    def branchexeclp(self, allowaddcons):

        candidate_vars, *_ = self.model.getPseudoBranchCands()
        candidate_mask = [var.getCol().getIndex() for var in candidate_vars]

        state = utilities.extract_state(self.model, self.state_buffer)
        c, e, v = state
        v = v['values'][candidate_mask]

        state_khalil = utilities.extract_khalil_variable_features(self.model, candidate_vars, self.khalil_root_buffer)

        var_feats = np.concatenate([v, state_khalil, np.ones((v.shape[0],1))], axis=1)
        var_feats = utilities._preprocess(var_feats, mode="min-max-2")
        # TODO: Move to(device) inside as_tensor()
        var_feats = torch.as_tensor(var_feats, dtype=torch.float32).to(device)
        
        with torch.no_grad():
            # TODO: drop .cpu().numpy() for faster running time?
            var_logits = self.policy(var_feats).cpu().numpy()

        best_var = candidate_vars[var_logits.argmax()]
        self.model.branchVar(best_var)
        result = scip.SCIP_RESULT.BRANCHED

        # fair node counting
        if result == scip.SCIP_RESULT.REDUCEDDOM:
            self.ndomchgs += 1
        elif result == scip.SCIP_RESULT.CUTOFF:
            self.ncutoffs += 1

        return {'result': result}
Ejemplo n.º 3
0
    def __getitem__(self, index):
        with gzip.open(self.sample_files[index], 'rb') as f:
            sample = pickle.load(f)

        is_root = "root" in self.sample_files[index]

        obss, target, obss_feats, _ = sample['obss']
        v, _, _ = obss
        sample_cand_scores = obss_feats['scores']
        sample_cands = np.where(sample_cand_scores != -1)[0]

        v_feats = v[sample_cands]
        v_feats = utilities._preprocess(v_feats, mode='min-max-2')

        cand_scores = sample_cand_scores[sample_cands]
        sample_action = np.where(sample_cands == target)[0][0]

        weight = obss_feats['depth']/sample['max_depth'] if sample['max_depth'] else 1.0
        if self.weighing_scheme == "linear_decay":
            m = np.exp(-0.5) - 1
            c = 1
            weight = weight * m + c
        elif self.weighing_scheme == "sigmoidal_decay":
            weight = (1 + np.exp(-0.5))/(1 + np.exp(weight - 0.5))
        elif self.weighing_scheme == "exponential_decay":
            weight = np.exp(weight * -0.5)
        elif self.weighing_scheme == "quadratic_decay":
            weight = (np.exp(-0.5) - 1) * weight ** 2 + 1
        elif self.weighing_scheme == "constant":
            weight = 1.0
        else:
            raise ValueError(f"Unknown value for node weights: {self.weighing_scheme}")

        return v_feats, sample_action, cand_scores, weight
Ejemplo n.º 4
0
    def branchexeclp(self, allowaddcons):

        candidate_vars, *_ = self.model.getPseudoBranchCands()
        candidate_mask = [var.getCol().getIndex() for var in candidate_vars]
        state = utilities.extract_state(self.model, self.state_buffer)
        c, e, v = state
        if self.model.getNNodes() == 1:
            state = (
                torch.as_tensor(c['values'], dtype=torch.float32),
                torch.as_tensor(e['indices'], dtype=torch.long),
                torch.as_tensor(e['values'], dtype=torch.float32),
                torch.as_tensor(v['values'], dtype=torch.float32),
                torch.as_tensor(c['values'].shape[0], dtype=torch.int32),
                torch.as_tensor(v['values'].shape[0], dtype=torch.int32),
            )
            state = map(lambda x: x.to(self.device), state)
            with torch.no_grad():
                if self.teacher is not None:
                    root_feats, _ = self.teacher(state)
                    state = root_feats
                self.root_params = self.policy_get_root_params(state)

        v = v['values'][candidate_mask]
        state_khalil = utilities.extract_khalil_variable_features(
            self.model, candidate_vars, self.khalil_root_buffer)

        var_feats = np.concatenate(
            [v, state_khalil, np.ones((v.shape[0], 1))], axis=1)
        var_feats = utilities._preprocess(var_feats, mode="min-max-2")
        var_feats = torch.as_tensor(var_feats, dtype=torch.float32).to(device)

        with torch.no_grad():
            var_logits = self.policy_predict(
                var_feats, self.root_params[candidate_mask]).cpu().numpy()

        best_var = candidate_vars[var_logits.argmax()]
        self.model.branchVar(best_var)
        result = scip.SCIP_RESULT.BRANCHED

        # fair node counting
        if result == scip.SCIP_RESULT.REDUCEDDOM:
            self.ndomchgs += 1
        elif result == scip.SCIP_RESULT.CUTOFF:
            self.ncutoffs += 1

        return {'result': result}