《算法》第六章部分程序 part 8
▶ 书中第六章部分程序,加上自己补充的代码,包括单纯形法求解线性规划问题
● 单纯形法求解线性规划问题
// 表上作业法,I 为单位阵,y 为对偶变量,z 为目标函数值
// n m 1
// ┌───────────┬───────┬───┐
// │ │ │ │
// m │ A │ I │ b │
// a[m+1][n+m+1] = │ │ │ │
// ├───────────┼───────┼───┤
// 1 │ c │ y │ z │
// └───────────┴───────┴───┘ package package01; import edu.princeton.cs.algs4.StdOut;
import edu.princeton.cs.algs4.StdRandom; public class class01
{
private static final double EPSILON = 1.0E-10;
private double[][] a; // 工作表
private int m; // 约束数
private int n; // 变量数
private int[] basis; // basis[p] = q,第 p 行的基变量是 x[q] public class01(double[][] A, double[] b, double[] c)
{
m = b.length;
n = c.length;
for (int i = 0; i < m; i++)
{
if (b[i] < 0) // 要求 b 的分量均不小于 0
throw new IllegalArgumentException("RHS must be nonnegative");
}
a = new double[m + 1][n + m + 1];
for (int i = 0; i < m; i++) // a[0:m-1][0:n-1] = A(两边包含)
{
for (int j = 0; j < n; j++)
a[i][j] = A[i][j];
}
for (int i = 0; i < m; i++) // a[0:m-1][n:n+m-1] = I_m
a[i][n + i] = 1.0;
for (int i = 0; i < n; i++) // a[m][0:n-1] = c
a[m][i] = c[i];
for (int i = 0; i < m; i++) // a[0:m-1][n+m] = b
a[i][m + n] = b[i];
basis = new int[m];
for (int i = 0; i < m; i++)
basis[i] = n + i; for (;;) // 单纯形法求解
{
int q = -1;
for (int i = 0; q == -1 && i < m + n; i++) // 找到可优化的变量对应的列 q,即 c[j] > 0 的最靠前索引
q = (a[m][i] > 0) ? i : q;
if (q == -1) // 优化已完成
break; int p = -1; // 依最小比例规则选择离开向量 p
for (int i = 0; i < m; i++) // 要求 a[i][q] > 0 且要么 p 选第一个,要么选壁纸最大的那个
{
if (a[i][q] > EPSILON && (p == -1 || (a[i][m + n] / a[i][q]) < (a[p][m + n] / a[p][q])))
p = i;
}
if (p == -1) // 无离开向量,无界解
throw new ArithmeticException("Linear program is unbounded"); for (int i = 0; i <= m; i++) // 对其他行使用a[p][q] 进行高斯消元
{
for (int j = 0; j <= m + n; j++)
{
if (i != p && j != q)
a[i][j] -= a[p][j] * a[i][q] / a[p][q];
}
}
for (int i = 0; i <= m; i++) // 消元后其他行清零(消除浮点计算误差)
{
if (i != p)
a[i][q] = 0.0;
}
for (int j = 0; j <= m + n; j++)// 主元所在行归一化
{
if (j != q)
a[p][j] /= a[p][q];
}
a[p][q] = 1.0; basis[p] = q; // 更新基向量
}
assert check(A, b, c); // 检查结果
} private int dantzig() // 搜索 c 中值大于零的、最靠前的项的索引
{
int q = 0;
for (int j = 1; j < m + n; j++)
{
if (a[m][j] > a[m][q])
q = j;
}
return a[m][q] <= 0 ? -1 : q; // c 中各项均小于0,优化已完成
} public double value() // 最优解的目标函数值
{
return -a[m][m + n];
} public double[] primal() // 最优解的自变量值
{
double[] x = new double[n];
for (int i = 0; i < m; i++)
{
if (basis[i] < n)
x[basis[i]] = a[i][m + n];
}
return x;
} public double[] dual() // 最优解的对偶变量值
{
double[] y = new double[m];
for (int i = 0; i < m; i++)
y[i] = -a[m][n + i];
return y;
} private boolean isPrimalFeasible(double[][] A, double[] b) // 解的松弛性
{
double[] x = primal();
for (int j = 0; j < x.length; j++) // 检查最优解分量是否均非负
{
if (x[j] < 0.0)
{
StdOut.println("x[" + j + "] = " + x[j] + " is negative");
return false;
}
}
for (int i = 0; i < m; i++) // 检查约束条件
{
double sum = 0.0;
for (int j = 0; j < n; j++)
sum += A[i][j] * x[j];
if (sum > b[i] + EPSILON)
{
StdOut.println("not primal feasibleb\nb[" + i + "] = " + b[i] + ", sum = " + sum);
return false;
}
}
return true;
} private boolean isDualFeasible(double[][] A, double[] c) // 对偶解的松弛性
{
double[] y = dual();
for (int i = 0; i < y.length; i++) // 检查对偶变量不小于 0
{
if (y[i] < 0.0)
{
StdOut.println("y[" + i + "] = " + y[i] + " is negative");
return false;
}
}
for (int j = 0; j < n; j++) // 检查对偶约束条件 A*y ≥ c
{
double sum = 0.0;
for (int i = 0; i < m; i++)
sum += A[i][j] * y[i];
if (sum < c[j] - EPSILON)
{
StdOut.println("not dual feasible\nc[" + j + "] = " + c[j] + ", sum = " + sum);
return false;
}
}
return true;
} private boolean isOptimal(double[] b, double[] c) // 检查解的最优性 value == c*x == y*b
{
double[] x = primal(), y = dual();
double value = value(), value1 = 0.0;
for (int j = 0; j < x.length; j++)
value1 += c[j] * x[j];
double value2 = 0.0;
for (int i = 0; i < y.length; i++)
value2 += y[i] * b[i];
if (Math.abs(value - value1) > EPSILON || Math.abs(value - value2) > EPSILON)
{
StdOut.println("value = " + value + ", cx = " + value1 + ", yb = " + value2);
return false;
}
return true;
} private boolean check(double[][]A, double[] b, double[] c)
{
return isPrimalFeasible(A, b) && isDualFeasible(A, c) && isOptimal(b, c) && dantzig() == -1;
} private void show() // 输出工作表
{
StdOut.println("m = " + m);
StdOut.println("n = " + n);
for (int i = 0; i <= m; i++)
{
for (int j = 0; j <= m + n; j++)
StdOut.printf("%7.2f ", a[i][j]);
StdOut.println();
}
StdOut.println("value = " + value());
for (int i = 0; i < m; i++)
{
if (basis[i] < n)
StdOut.println("x_" + basis[i] + " = " + a[i][m + n]);
}
StdOut.println();
} private static void test(double[][] A, double[] b, double[] c) // 测试函数
{
class01 lp;
try // 有解正常输出,无解用异常提前退出
{
lp = new class01(A, b, c);
}
catch (ArithmeticException e)
{
System.out.println(e);
return;
}
StdOut.println("value = " + lp.value());
double[] x = lp.primal();
for (int i = 0; i < x.length; i++)
StdOut.println("x[" + i + "] = " + x[i]);
double[] y = lp.dual();
for (int j = 0; j < y.length; j++)
StdOut.println("y[" + j + "] = " + y[j]);
} private static void test1()
{
double[][] A = { { -1, 1, 0 },{ 1, 4, 0 },{ 2, 1, 0 },{ 3, -4, 0 },{ 0, 0, 1 } };
double[] b = { 5, 45, 27, 24, 4 }, c = { 1, 1, 1 };
test(A, b, c);
} private static void test2() // x[0] = 12, x[1] = 28, value = 800
{
double[][] A = { { 5.0, 15.0 },{ 4.0, 4.0 },{ 35.0, 20.0 } };
double[] b = { 480.0, 160.0, 1190.0 }, c = { 13.0, 23.0 };
test(A, b, c);
} private static void test3() // unbounded
{
double[][] A = { { -2.0, -9.0, 1.0, 9.0 },{ 1.0, 1.0, -1.0, -2.0 } };
double[] b = { 3.0, 2.0 }, c = { 2.0, 3.0, -1.0, -12.0 };
test(A, b, c);
} private static void test4() // x[0] = 1.0, x[1] = 0.0, x[2] = 1.0, x[3] = 0.0, value = 1.0,周期
{
double[][] A = { { 0.5, -5.5, -2.5, 9.0 },{ 0.5, -1.5, -0.5, 1.0 },{ 1.0, 0.0, 0.0, 0.0 } };
double[] b = { 0.0, 0.0, 1.0 }, c = { 10.0, -57.0, -9.0, -24.0 };
test(A, b, c);
} public static void main(String[] args)
{
StdOut.println("----- test 1 --------------------");
test1();
StdOut.println("\n----- test 2 --------------------");
test2();
StdOut.println("\n----- test 3 --------------------");
test3();
StdOut.println("\n----- test 4 --------------------");
test4();
StdOut.println("\n----- test random ---------------"); // 随机测试
int m = Integer.parseInt(args[0]), n = Integer.parseInt(args[1]);
double[][] A = new double[m][n];
double[] b = new double[m], c = new double[n];
for (int i = 0; i < m; i++)
{
for (int j = 0; j < n; j++)
A[i][j] = StdRandom.uniform(100);
}
for (int i = 0; i < m; i++)
b[i] = StdRandom.uniform(1000);
for (int j = 0; j < n; j++)
c[j] = StdRandom.uniform(1000);
class01 lp = new class01(A, b, c);
test(A, b, c);
}
}
《算法》第六章部分程序 part 8的更多相关文章
- 《算法》第六章部分程序 part 7
▶ 书中第六章部分程序,加上自己补充的代码,包括全局最小切分 Stoer-Wagner 算法,最小权值二分图匹配 ● 全局最小切分 Stoer-Wagner 算法 package package01; ...
- 《算法》第六章部分程序 part 6
▶ 书中第六章部分程序,包括在加上自己补充的代码,包括二分图最大匹配(最小顶点覆盖)的交替路径算法和 HopcroftKarp 算法 ● 二分图最大匹配(最小顶点覆盖)的交替路径算法 package ...
- 《算法》第六章部分程序 part 5
▶ 书中第六章部分程序,包括在加上自己补充的代码,网络最大流 Ford - Fulkerson 算法,以及用到的流量边类和剩余流量网络类 ● 网络最大流 Ford - Fulkerson 算法 pac ...
- 《算法》第六章部分程序 part 4
▶ 书中第六章部分程序,包括在加上自己补充的代码,利用后缀树查找最长重复子串.查找最大重复子串并输出其上下文(Key word in context,KWIC).求两字符串的最长公共子串 ● 利用后缀 ...
- 《算法》第六章部分程序 part 3
▶ 书中第六章部分程序,包括在加上自己补充的代码,后缀树的两种实现 ● 后缀树实现一 package package01; import java.util.Arrays; import edu.pr ...
- 《算法》第六章部分程序 part 2
▶ 书中第六章部分程序,包括在加上自己补充的代码,B-树 ● B-树 package package01; import edu.princeton.cs.algs4.StdOut; public c ...
- 《算法》第六章部分程序 part 1
▶ 书中第六章部分程序,包括在加上自己补充的代码,粒子碰撞系统及用到的粒子类 ● 粒子系统 package package01; import java.awt.Color; import edu.p ...
- 《算法》第一章部分程序 part 1
▶ 书中第一章部分程序,加上自己补充的代码,包括若干种二分搜索,寻找图上连通分量数的两种算法 ● 代码,二分搜索 package package01; import java.util.Arrays; ...
- 《算法》第二章部分程序 part 5
▶ 书中第二章部分程序,加上自己补充的代码,包括利用优先队列进行多路归并和堆排序 ● 利用优先队列进行多路归并 package package01; import edu.princeton.cs.a ...
随机推荐
- git 克隆指定分支
git clone -b v2.8.1 https://git.oschina.net/oschina/android-app.git
- 记使用vue-awesome-swiper遇到的一些问题
一.vue-awesome-swiper的使用 1.在项目中全局引用 import VueAwesomeSwiper from 'vue-awesome-swiper' // require s ...
- bzoj5008: 方师傅的房子
Description 方师傅来到了一个二维平面.他站在原点上,觉得这里风景不错,就建了一个房子.这个房子是n个点的凸多边形 ,原点一定严格在凸多边形内部.有m个人也到了这个二维平面.现在你得到了m个 ...
- 解决hash冲突的三个方法(转)
https://www.cnblogs.com/wuchaodzxx/p/7396599.html 目录 开放定址法 线性探测再散列 二次探测再散列 伪随机探测再散列 再哈希法 链地址法 建立公共溢出 ...
- Ring3创建事件Ring0设置事件
应用程序中创建的事件和在内核中创建的事件对象,本质上是同一个东西,在用户模式中,他用句柄表示,在内核模式下,他用KEVENT表示数据结构表示.在应用程序中,所有的内核对象都不会被用户看到,用户看到的知 ...
- 1127 ZigZagging on a Tree (30 分)
1127 ZigZagging on a Tree (30 分) Suppose that all the keys in a binary tree are distinct positive in ...
- []map[][]切片map小计
go中的map我们都知道在进行遍历的时候我们知道他是无序的.对于map[int]interface{}类型的,我们可以通过计算map的长度,通过定长的for循环,进行顺序的输出. 那么如果map的类型 ...
- win7 没有权限使用网络资源
局域网下同一工作组电脑无法访问 提示"....没有权限使用网络资源...." 一.组策略 win + R 输入gpedit.msc并回车,打开本地组策略编辑器 按如下展开 计算机配 ...
- [UE4]修改相机裁剪距离
在UE4中,相机距离一个物体太近,物体就会被裁剪,这个距离是一个全局设定,无法单个相机设置. 项目设置:
- Linux开机自动挂载windows网络共享
yum install samba-client yum install cifs.utils yum install samba-common 命令: mount -v -t cifs // ...