summaryrefslogtreecommitdiffstats
path: root/05-advanced_algorithms_and_complexity/02-linear_programming/02-diet/diet.cpp
blob: 7e1056a4dacfe630d2ad91efc4959de5b7c47990 (plain)
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
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
#include <algorithm>
#include <limits>
#include <iostream>
#include <vector>
#include <cstdio>
using namespace std;

// #define DEBUG

typedef vector<vector<double>> matrix;

void print(matrix A, vector<double> b)
{
#ifdef DEBUG
  for (int i = 0; i < A.size(); i++) {
    for (int j = 0; j < A[0].size(); j++) {
      cerr << A[i][j] << " ";
    }
    cerr << ": " << b[i] << endl;
  }
  cerr << endl;
#endif
}

#define DELTA 0.000001
static inline bool same(double a, double b)
{
  double d = (a - b);
  return (d < 0 ? -d : d) < DELTA;
}

void gaussian_elimination(matrix& m)
{
  int rows = m.size();
  int cols = m[0].size();
  int np_row = 0;

  // each column
  for (int col = 0; col < (cols - 1); col++) {
    // each non-pivot row
    for (int row = np_row; row < rows; row++) {
      // leftmost non-zero
      if (m[row][col] != 0) {
        // swap row with np_row
        if (row != np_row)
          std::swap(m[np_row], m[row]);

        vector<double>& pivot_row = m[np_row];
        // scale pivot row
        if (pivot_row[col] != 1.0) {
          double f = 1.0 / pivot_row[col];
          for (int i = 0; i < cols; i++)
            pivot_row[i] *= f;
        }

        // scale then add to non-zero other rows
        for (int i = 0; i < rows; i++) {
          if (i != np_row && m[i][col] != 0) {
            vector<double>& r = m[i];
            double f = r[col] / pivot_row[col];
            for (int j = 0; j < cols; j++)
              r[j] -= pivot_row[j] * f;
          }
        }

        np_row++;
        break;
      }
    }
  }
}

#define BIG_NUM 1000000000
#define BEST  -std::numeric_limits<double>::max()

pair<int, vector<double>> solve(
  matrix A,
  vector<double> b,
  vector<double> c)
{
  int cstrs = A.size();   // n
  int vars = A[0].size(); // m

  matrix M(vars, vector<double>(vars + 1));
  double best = BEST;
  vector<double> sol(vars);

  // build permutations of A
  int nbits = 0;
  int v = 0;
  int max = (1 << cstrs) - 1;
  vector<bool> bits(cstrs, false);

  bool augmented = false;   // will be set to true when the last constraint is used

  // for each permutation of size vars
  for (int v = 0; v < max; v++) {
    // build next value
    for (int i = 0; i < bits.size(); i++) {
      if (!bits[i]) {
        bits[i] = true;
        nbits++;
        break;
      }
      bits[i] = !bits[i];
      nbits--;
    }

#ifdef DEBUG
    cerr << "####" << v + 1 << " : ";
    for (int i = 0; i < bits.size(); i++)
      cerr << bits[i] << " ";
    cerr << endl;
#endif

    if (nbits != vars)
      continue;

    if (!augmented && bits[bits.size() - 1]) {
      // no solution found so far
      if (same(best, BEST))
        return {-1, sol}; // no solution
      augmented = true;
    }

    // build matrix to solve
    int x = 0;
    for (int i = 0; i < bits.size(); i++) {
      if (bits[i]) {
        for (int j = 0; j < vars; j++)
          M[x][j] = A[i][j];
        M[x][vars] = b[i];
        x++;
      }
    }

    print(M, b);
    gaussian_elimination(M);
    print(M, b);

    bool ok = true;

    // check that solution matrix pivots are 1's
    for (int i = 0; i < vars; i++) {
      if (!same(M[i][i], 1.0)) {
        ok = false;
        break;
      }
    }
    if (!ok)
      continue;

    // check that other inequalities holds
    for (int i = 0; i < bits.size(); i++) {
      if (!bits[i]) {
        double r = 0;
        for (int j = 0; j < vars; j++)
          r += (M[j][vars] * A[i][j]);
        if (r > (b[i] + DELTA)) {
          ok = false;
          break;
        }
      }
    }
    if (!ok)
      continue;

    // compute fitness
    double r = 0;
    for (int j = 0; j < vars; j++)
      r += (M[j][vars] * c[j]);
    if (r > best) {
      if (augmented)
        return { 1, sol}; // infinity, see last paragraph in What To Do
      best = r;
      for (int j = 0; j < vars; j++)
        sol[j] = M[j][vars];
    }
  }

  if (same(best, BEST))
    return {-1, sol}; // no solution

  return {0, sol};
}

pair<int, vector<double>> solve_diet_problem(
  int n,
  int m,
  matrix A,
  vector<double> b,
  vector<double> c)
{
  // add m constraints : -var <= 0
  for (int i = 0; i < m; i++) {
    vector<double> v(m, 0);
    v[i] = -1;
    A.push_back(v);
    b.push_back(0);
  }
  // add augmented constraint : sum vars < BIG_NUM
  A.push_back(vector<double>(m, 1));
  b.push_back(BIG_NUM);

  return solve(A, b, c);
}

int main(){
  int n, m;
  cin >> n >> m;
  matrix A(n, vector<double>(m));
  for (int i = 0; i < n; i++) {
    for (int j = 0; j < m; j++) {
      cin >> A[i][j];
    }
  }
  vector<double> b(n);
  for (int i = 0; i < n; i++) {
    cin >> b[i];
  }
  vector<double> c(m);
  for (int i = 0; i < m; i++) {
    cin >> c[i];
  }

  pair<int, vector<double>> ans = solve_diet_problem(n, m, A, b, c);

  switch (ans.first) {
    case -1:
      printf("No solution\n");
      break;
    case 0:
      printf("Bounded solution\n");
      for (int i = 0; i < m; i++) {
        printf("%.14f%c", ans.second[i], " \n"[i + 1 == m]);
      }
      break;
    case 1:
      printf("Infinity\n");
      break;
  }
  return 0;
}