generated from Tiphereth-A/TINplate
-
Notifications
You must be signed in to change notification settings - Fork 4
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
72aac68
commit de40f52
Showing
12 changed files
with
668 additions
and
19 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,3 @@ | ||
From \url{https://learnxinyminutes.com/docs/bash/}. | ||
|
||
\inputminted[mathescape=false]{bash}{src/src/LearnBash.sh} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,90 @@ | ||
#ifndef TIFALIBS_OPT_SIMPLEX | ||
#define TIFALIBS_OPT_SIMPLEX | ||
|
||
#include "../util/fp_const.hpp" | ||
#include "../util/util.hpp" | ||
|
||
namespace tifa_libs::opt { | ||
|
||
template <class T = double> | ||
struct LPSolver { | ||
#define ltj(X) \ | ||
if (s == -1 || std::make_pair(X[j], N[j]) < std::make_pair(X[s], N[s])) s = j | ||
static constexpr T inf = 1 / .0; | ||
int m, n; // # m = contraints, # n = variables | ||
vec<int> N, B; // N[j] = non-basic variable (j-th column), = 0 | ||
vvec<T> D; // B[j] = basic variable (j-th row) | ||
LPSolver(vvec<T> const& A, vec<T> const& b, vec<T> const& c) : m(sz(b)), n(sz(c)), N(n + 1), B(m), D(m + 2, vec<T>(n + 2)) { | ||
for (int i = 0; i < m; ++i) | ||
for (int j = 0; j < n; ++j) D[i][j] = A[i][j]; | ||
for (int i = 0; i < m; ++i) B[i] = n + i, D[i][n] = -1, D[i][n + 1] = b[i]; | ||
// B[i]: basic variable for each constraint | ||
// D[i][n]: artificial variable for testing feasibility | ||
for (int j = 0; j < n; ++j) N[j] = j, D[m][j] = -c[j]; | ||
// D[m] stores negation of objective, | ||
// which we want to minimize | ||
N[n] = -1; | ||
D[m + 1][n] = 1; // to find initial feasible | ||
} // solution, minimize artificial variable | ||
void pivot(int r, int s) { // swap B[r] (row) | ||
T inv = 1 / D[r][s]; // with N[r] (column) | ||
for (int i = 0; i <= m + 1; ++i) | ||
if (i != r && abs(D[i][s]) > EPS<T>) { | ||
T binv = D[i][s] * inv; | ||
for (int j = 0; j < (n + 2); ++j) | ||
if (j != s) D[i][j] -= D[r][j] * binv; | ||
D[i][s] = -binv; | ||
} | ||
D[r][s] = 1; | ||
for (int j = 0; j < (n + 2); ++j) D[r][j] *= inv; // scale r-th row | ||
std::swap(B[r], N[s]); | ||
} | ||
bool simplex(int phase) { | ||
int x = m + phase - 1; | ||
while (1) { // if phase=1, ignore artificial variable | ||
int s = -1; | ||
for (int j = 0; j <= n; ++j) | ||
if (N[j] != -phase) ltj(D[x]); | ||
// find most negative col for nonbasic (NB) variable | ||
if (D[x][s] >= -EPS<T>) return 1; | ||
// can't get better sol by increasing NB variable | ||
int r = -1; | ||
for (int i = 0; i < m; ++i) { | ||
if (D[i][s] <= EPS<T>) continue; | ||
if (r == -1 || std::make_pair(D[i][n + 1] / D[i][s], B[i]) < std::make_pair(D[r][n + 1] / D[r][s], B[r])) r = i; | ||
// find smallest positive ratio | ||
} // -> max increase in NB variable | ||
if (r == -1) return 0; // objective is unbounded | ||
pivot(r, s); | ||
} | ||
} | ||
T solve(vec<T>& x) { // 1. check if x=0 feasible | ||
int r = 0; | ||
for (int i = (1); i < m; ++i) | ||
if (D[i][n + 1] < D[r][n + 1]) r = i; | ||
if (D[r][n + 1] < -EPS<T>) { // if not, find feasible start | ||
pivot(r, n); // make artificial variable basic | ||
assert(simplex(2)); // I think this will always be true?? | ||
if (D[m + 1][n + 1] < -EPS<T>) return -inf; | ||
// D[m+1][n+1] is max possible value of the negation of | ||
// artificial variable, optimal value should be zero | ||
// if exists feasible solution | ||
for (int i = 0; i < m; ++i) | ||
if (B[i] == -1) { // artificial var basic | ||
int s = 0; | ||
for (int j = (1); j <= n; ++j) ltj(D[i]); // -> nonbasic | ||
pivot(i, s); | ||
} | ||
} | ||
bool ok = simplex(1); | ||
x = vec<T>(n); | ||
for (int i = 0; i < m; ++i) | ||
if (B[i] < n) x[B[i]] = D[i][n + 1]; | ||
return ok ? D[m][n + 1] : inf; | ||
} | ||
}; | ||
#undef ltj | ||
|
||
} // namespace tifa_libs::opt | ||
|
||
#endif |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,3 +1,3 @@ | ||
设有 \(n\) 个球, \(m\) 个盒子 | ||
|
||
类似 \fullref{sec:球与盒(球全部相同, 盒全部相同)}, 答案为 \(f(n-m,m)\) | ||
类似 \fullref{sec:球与盒(球相同,盒相同)}, 答案为 \(f(n-m,m)\) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,3 +1,3 @@ | ||
设有 \(n\) 个球, \(m\) 个盒子 | ||
|
||
\fullref{sec:球与盒(球互不相同, 盒全部相同, 至多装一个球)} | ||
\fullref{sec:球与盒(球相同,盒相同,至多装一个球)} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,51 @@ | ||
求解如下线性规划问题: | ||
|
||
\[ | ||
\begin{array}{ll} | ||
\max & c^T x. \\ | ||
\textrm{s.t.} & Ax \le b, x \ge 0. | ||
\end{array} | ||
\] | ||
|
||
若无解则返回 \verb|-inf|, 若无界则返回 \verb|inf|, 否则返回 \(c^T x\) 的最大值. 输入的 \verb|x| 会被修改为解. 不保证数值稳定性. \(x = 0\) 最好在可行域中. | ||
|
||
\paragraph{使用} | ||
|
||
\inputminted{cpp}{src/src/simplex_usage.txt} | ||
|
||
\paragraph{原问题与对偶问题} | ||
|
||
\begin{tabular}{|c|c|} | ||
\hline | ||
原问题 & 对偶问题 \\ | ||
\hline | ||
\(\max_x c^T x\) & \(\min_y b^T y\) \\ | ||
\hline | ||
\(n\) 个变量 \(x_1,\dots,x_n\) & \(n\) 个限制条件 \(A^T y\gtreqqless c\) \\ | ||
\(x_i\geq 0\) & 第 \(i\) 个限制条件为 \(\geq c_i\) \\ | ||
\(x_i\leq 0\) & 第 \(i\) 个限制条件为 \(\leq c_i\) \\ | ||
\(x_i\in \mathrm{R}\) & 第 \(i\) 个限制条件为 \(=c_i\) \\ | ||
\hline | ||
\(m\) 个限制条件 \(Ax\gtreqqless b\) & \(m\) 个变量 \(y_1,\dots,y_m\) \\ | ||
第 \(i\) 个限制条件为 \(\geq b_i\) & \(y_i\leq 0\) \\ | ||
第 \(i\) 个限制条件为 \(\leq b_i\) & \(y_i\geq 0\) \\ | ||
第 \(i\) 个限制条件为 \(=b_i\) & \(y_i\in \mathrm{R}\) \\ | ||
\hline | ||
\end{tabular} | ||
|
||
\begin{tabular}{|cc|cc|} | ||
\hline | ||
\multicolumn{2}{|c|}{原问题} & \multicolumn{2}{|c|}{对偶问题} \\ | ||
\hline | ||
\(\max\) & \(3x_1+4x_2\) & \(\min\) & \(7y_1\) \\ | ||
\(\textrm{s.t.}\) & \(5x_1+6x_2=7\) & \(\textrm{s.t.}\) & \(5y_1\geq 3\) \\ | ||
& & & \(6y_2\geq 4\) \\ | ||
& \(x_1\geq 0,x_2\geq 0\) & & \(y_1\in\mathrm{R}\) \\ | ||
\hline | ||
\end{tabular} | ||
|
||
\paragraph{复杂度} | ||
|
||
\(O(NM \cdot \texttt{pivots})\), where a pivot may be e.g. an edge relaxation. \(O(2^N)\) in the general case. | ||
|
||
\paragraph{参考} \cite{bqi343cp} |
Oops, something went wrong.