No parents
No children
gravatar
madarasi@cs.elte.hu
madarasi@cs.elte.hu
gap.cc and makefile added.
0 0 2
tip default
2 files changed with 270 insertions and 0 deletions:
236
↑ Collapse diff ↑
Ignore white space 6 line context
1
#include <iostream>
2
#include <lemon/lp.h>
3
#include <lemon/list_graph.h>
4
#include <lemon/lgf_reader.h>
5
#include <utility>
6
#include <fstream>
7
#include <string>
8
#include <vector>
9
#include <iterator>
10
#include <map>
11

	
12
using namespace lemon;
13
using namespace std;
14

	
15
//LP(opt)/IP(opt)
16
double IP_gap(lemon::ListGraph & g,
17
	      lemon::ListGraph::NodeMap < int > & set) {
18
  Mip mip;
19
  //column
20
  lemon::ListGraph::EdgeMap < Mip::Col > x(g);
21
  for (lemon::ListGraph::EdgeIt e(g); e != lemon::INVALID; ++e) {
22
    Mip::Col xi = mip.addCol();
23
    x[e] = xi;
24
  }
25
  //row
26
  for (lemon::ListGraph::NodeIt v(g); v != lemon::INVALID; ++v) {
27
    if (set[v] == -1) {
28
      Mip::Expr exprS1;
29
      Mip::Expr exprS2;
30
      for (lemon::ListGraph::IncEdgeIt e(g, v); e != lemon::INVALID; ++e) {
31
        if (set[g.oppositeNode(v, e)] == 1) {
32
          exprS1 += x[e];
33
        }
34
        if (set[g.oppositeNode(v, e)] == 2) {
35
          exprS2 += x[e];
36
        }
37
        if (set[g.oppositeNode(v, e)] == 0) {
38
          exprS1 += x[e];
39
          exprS2 += x[e];
40
        }
41
      }
42
      mip.addRow(exprS1 <= 1);
43
      mip.addRow(exprS2 <= 1);
44
    }
45
    else {
46
      Mip::Expr expr;
47
      for (lemon::ListGraph::IncEdgeIt e(g, v); e != lemon::INVALID; ++e) {
48
        expr += x[e];
49
      }
50
      mip.addRow(expr <= 1);
51
    }
52
  }
53
  //solve IP
54
  double ip;
55
  mip.max();
56
  Mip::Expr sum_ip;
57
  for (lemon::ListGraph::EdgeIt e(g); e != lemon::INVALID; ++e) {
58
    mip.colType(x[e], Mip::INTEGER);
59
    mip.colLowerBound(x[e], 0);
60
    mip.colUpperBound(x[e], 1);
61
    sum_ip += x[e];
62
  }
63
  mip.obj(sum_ip);
64
  mip.solve();
65
  if (mip.type() == Mip::OPTIMAL) {
66
    ip = mip.solValue();
67
  }
68
  else {
69
    ip = -1;
70
  }
71

	
72
  //solve LP
73
  double lp;
74
  mip.max();
75
  Mip::Expr sum_lp;
76
  for (lemon::ListGraph::EdgeIt e(g); e != lemon::INVALID; ++e) {
77
    mip.colType(x[e], Mip::REAL);
78
    mip.colLowerBound(x[e], 0);
79
    mip.colUpperBound(x[e], 1);
80
    sum_lp += x[e];
81
  }
82
  mip.obj(sum_lp);
83
  mip.solve();
84
  if (mip.type() == Mip::OPTIMAL) {
85
    lp = mip.solValue();
86
  }
87
  else {
88
    lp = 0;
89
  }
90

	
91
  //calculate gap
92
  double gap = lp / ip;
93
  if (gap > 0) {
94
    return gap;
95
  }
96
  else {
97
    return 0;
98
  }
99
}
100

	
101
//IP gap for partition(k)
102
double k_to_IP_gap(lemon::ListGraph & g,
103
		   lemon::ListGraph::NodeMap < int > & STmap,
104
		   int l,
105
		   int p,
106
		   int firstnode,
107
		   int G) {
108
  //create partition
109
  bool b = 1; //not seen any nodes in S
110
  lemon::ListGraph::NodeMap < int > set(g); //1:S1, 2:S2, 0:S1,S2, -1:T
111
  lemon::ListGraph::Node first = g.nodeFromId(0);
112
  for (int j = 0; j < G; j++) {
113
    lemon::ListGraph::Node n = g.nodeFromId(j);
114
    if (STmap[n] == 1) {
115
      if (b == 0) {
116
        int x = l / p;
117
        set[n] = x;
118
        l = l - x * p;
119
        p = p / 3;
120
      }
121
      if (b == 1) {
122
        first = n;
123
        b = 0; //seen the first node in S
124
      }
125
    }
126
    else {
127
      set[n] = -1;
128
    }
129
  }
130
  //first node in firstnode(=0,1)
131
  set[first] = firstnode;
132
  //calculate gap
133
  double gap = IP_gap(g, set);
134
  return gap;
135
}
136

	
137
int main() {
138
  //variables for maximum search
139
  double max = 0; //max value
140
  int max_first = 0; //where is the first node (0,1)
141
  int max_k = 0; //partition
142
  std::string max_graph; //graph's line
143

	
144
  int cnt = 0; //line counter
145

	
146
  //input
147
  std::string line;
148
  istream & f = std::cin;
149

	
150
  //graph parameters
151
  int N = 7; // |S|
152
  int G = 11; // |V(g)|
153

	
154
  //calculate greatest and pow
155
  int greatest = 0; //the greatest number of N-1 digits in ternary number system
156
  int pow = 1; //3^(N-2)
157
  for (int i = 0; i < N - 1; i++) {
158
    greatest += 2 * pow;
159
    pow = pow * 3;
160
  }
161
  pow = pow / 3;
162

	
163
  //read
164
  while (std::getline(std::cin, line)) {
165
    std::vector < std::string > lines;
166
    lines.push_back(line);
167
    for (int i = 0; i < 10000 && std::getline(std::cin, line); ++i) {
168
      lines.push_back(line);
169
    }
170

	
171
    //parallel
172
    #pragma omp parallel for schedule(dynamic, 1)
173
    for (int i = 0; i < lines.size(); ++i) {
174
      std::string line = lines[i];
175
      lemon::ListGraph g;
176
      lemon::ListGraph::NodeMap < int > STmap(g); //1:S, 0:T
177
      lemon::ListGraph::Node n;
178
      //create STmap
179
      for (int i = 0; i < G; i++) {
180
        n = g.addNode();
181
        if (i < N) {
182
          STmap[n] = 1; //set S
183
        }
184
	else {
185
          STmap[n] = 0; //set T
186
        }
187
      }
188
      //create g
189
      lemon::ListGraph::Node v = g.nodeFromId(N);
190
      int i = 0;
191
      while (line[i] != '.') {
192
        if (line[i] == ',') {
193
          v = g.nodeFromId(g.id(v) + 1);
194
        } else {
195
          g.addEdge(v, g.nodeFromId(line[i] - '1'));
196
        }
197
        i++;
198
      }
199

	
200
      //k_to_IP_gap for k=0..greatest and maximum search
201
      #pragma omp critical
202
      std::cout << ++cnt << std::endl;
203
      for (int k = 0; k < greatest + 1; k++) {
204
	//maximum search
205
        double gap = k_to_IP_gap(g, STmap, k, pow, 0, G); //set[first node]=0
206
        if (gap > max) {
207
          max = gap;
208
          max_k = k;
209
          max_first = 0;
210
          max_graph = line;
211
          std::cout << "max: " << max << std::endl;
212
          std::cout << "max_graph: " << max_graph << std::endl;
213
          std::cout << "max_first: " << max_first << std::endl;
214
          std::cout << "max_k: " << max_k << std::endl;
215
        }
216
        gap = k_to_IP_gap(g, STmap, k, pow, 1, G); //set[first node]=1
217
        if (gap > max) {
218
          max = gap;
219
          max_k = k;
220
          max_first = 1;
221
          max_graph = line;
222
          std::cout << "max: " << max << std::endl;
223
          std::cout << "max_graph: " << max_graph << std::endl;
224
          std::cout << "max_first: " << max_first << std::endl;
225
          std::cout << "max_k: " << max_k << std::endl;
226
        }
227
      }
228
    }
229
  }
230
  //print the answer
231
  std::cout << "max: " << max << std::endl;
232
  std::cout << "max_graph: " << max_graph << std::endl;
233
  std::cout << "max_first: " << max_first << std::endl;
234
  std::cout << "max_k: " << max_k << std::endl;
235
  return 0;
236
}
Ignore white space 6 line context
1
binaries = 	gap
2

	
3

	
4
#path of CPLEX
5
CPLEXDIR   = /opt/ibm/ILOG/CPLEX_Studio1263/cplex
6
#path of CPLEX concert
7
CONCERTDIR = /opt/ibm/ILOG/CPLEX_Studio1263/concert
8
#cpp compiler
9
CCC = g++
10

	
11
EXTRAFLAGS = -fopenmp
12

	
13
CCOPT = -O4 -fPIC -fno-strict-aliasing -fexceptions \
14
	-march=native -funroll-loops -std=c++11 -DNDEBUG -DIL_STD
15
LFLAGS = -L$(CPLEXDIR)/lib/x86-64_linux/static_pic/ \
16
	 -L$(CONCERTDIR)/lib/x86-64_linux/static_pic/
17
IFLAGS = -I$(CPLEXDIR)/include -I$(CONCERTDIR)/include
18
LIBFLAGS = -lemon -lconcert -lilocplex -lcplex -lm -ldl -lpthread 
19

	
20
.PHONY: all clean
21

	
22
all:	$(binaries)
23

	
24
%: %.cc
25
	$(CCC) $(CCOPT) $(IFLAGS) $(LFLAGS) -o$@ $< $(LIBFLAGS) $(EXTRAFLAGS)
26
dm2: locOptDM.h
27

	
28

	
29
clean:
30
	rm -f $(binaries) *.o
31
	rm -f $(binaries) *.d
32

	
33

	
34
-include *.d
... ...
 No newline at end of file
0 comments (0 inline)