File size: 3,834 Bytes
c84b37e
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
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
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
from functools import cmp_to_key
import numpy as np
import argparse
import pickle
import random
import time
import os

class pair: 
    def __init__(self): 
        self.x = 0
        self.y = 0
        self.val = 0

def cmp(a, b):
    if a.val > b.val: 
        return -1
    else:
        return 1
    
def FENNEL(n, m, k, site):
    '''
    Function Description:
    Use the FENNEL algorithm to partition the bipartite representation of the problem instance.

    Parameters:
    - n: Number of decision variables in the problem instance.
    - m: Number of constraints in the problem instance.
    - k: k[i] represents the number of decision variables in the i-th constraint.
    - site: site[i][j] represents which decision variable the j-th decision variable of the i-th constraint corresponds to.

    Return: 
    The result of the graph partitioning.
    '''
    raise NotImplementedError('FENNEL method should be implemented')

def generate_pair(
    number : int
):
    '''
    Function Description:
    Partition the problem based on the given problem instances and generate training data.

    Parameters:
    - number: Number of problem instances.

    Return: 
    The training data is generated and packaged as data.pickle. The function does not have a return value.
    '''
    for turn in range(number):
        print("=====", turn)
        # Check if data.pickle exists and read it if it exists.
        if(os.path.exists('./example/data' + str(turn) + '.pickle') == False):
            print("No problem file!")
            return 
        with open('./example/data' + str(turn) + '.pickle', "rb") as f:
            problem = pickle.load(f)
        
        # Check if solution.pickle exists and read it if it exists.
        if(os.path.exists('./example/sample' + str(turn) + '.pickle') == False):
            print("No solutuion file!")
            return 
        with open('./example/sample' + str(turn) + '.pickle', "rb") as f:
            solution = pickle.load(f)

        # obj_type represents the problem type (maximization/minimization).
        # n represents the number of decision variables.
        # m represents the number of constraints.
        # k[i] represents the number of decision variables in the i-th constraint.
        # site[i][j] represents which decision variable the j-th decision variable of the i-th constraint corresponds to.
        # value[i][j] represents the coefficient of the j-th decision variable in the i-th constraint.
        # constraint[i] represents the right-hand side value of the i-th constraint.
        # constraint_type[i] represents the type of the i-th constraint, where 1 represents <=, 2 represents >=, and 3 represents =.
        # coefficient[i] represents the coefficient of the i-th decision variable in the objective function.
        obj_type = problem[0]
        n = problem[1]
        m = problem[2]
        k = problem[3]
        site = problem[4]
        value = problem[5]
        constraint = problem[6]
        constraint_type = problem[7]
        coefficient = problem[8]

        variable_features = solution[0]
        constraint_features = solution[1]
        edge_indices = solution[2]
        edge_features = solution[3]
        optX = solution[4]
        
        # Get the graph partitioning result.
        new_color = FENNEL(n, m, k, site)
        
        with open('./example/pair' + str(turn) + '.pickle', 'wb') as f:
            pickle.dump([variable_features, constraint_features, edge_indices, edge_features, new_color, optX], f)

    
def parse_args():
    parser = argparse.ArgumentParser()
    parser.add_argument("--number", type = int, default = 10, help = 'The number of instances.')
    return parser.parse_args()

if __name__ == '__main__':
    args = parse_args()
    #print(vars(args))
    generate_pair(**vars(args))