700字范文,内容丰富有趣,生活中的好帮手!
700字范文 > Ilog cplex java 表示分段线性函数 piecewise function

Ilog cplex java 表示分段线性函数 piecewise function

时间:2018-08-04 18:25:14

相关推荐

Ilog cplex  java 表示分段线性函数 piecewise function

文章目录

1. 什么是分段线性函数2. Ilog CPLEX 表示 Piecewise Function 分段线性函数3. java Cplex 表示分段线性函数4. matlab/gurobi 中的分段线性函数

1. 什么是分段线性函数

Piecewise function (分段线性函数)是一组线段组成的函数(一般为连续函数)。

分段线性拟合可以用来拟合曲线.

例如下面这个函数

f(x)={300+xx≤100300+100+2(x−100)100&lt;x≤00+100+2∗(200−100)−3(x−200)x&gt;200f(x)=\begin{cases} 300+x &amp;x\leq 100\\ 300 + 100 + 2 (x-100) &amp; 100&lt;x\leq 200\\ 300 + 100 + 2 * (200-100) - 3 (x-200) &amp; x&gt;200 \end{cases} f(x)=⎩⎪⎨⎪⎧​300+x300+100+2(x−100)300+100+2∗(200−100)−3(x−200)​x≤100100<x≤200x>200​

是一个三段线性函数,它的图像是:

画图的 python 代码:

import matplotlib.pyplot as pltimport numpy as npimport matplotlib # for writing complex latex equationsmatplotlib.rc('text', usetex = True)matplotlib.rc('font', **{'family' : "sans-serif"})params= {'text.latex.preamble' : [r'\usepackage{amsmath}']}plt.rcParams.update(params)x = np.arange(0, 100, 1)plt.plot(x, 300 + x)x = np.arange(100, 200, 1)plt.plot(x, 300 + 100 + 2 * (x-100))x = np.arange(200, 300, 1)plt.plot(x, 300 + 100 + 2 * (200 - 100) - 3 * (x-200))plt.title(r'$f(x)=\begin{cases}300+x &x\leq 100\\300 + 100 + 2 (x-100) & 100<x\leq 200\\300 + 100 + 2 * (200-100) - 3 (x-200) & x>200\end{cases}$')plt.xlim((0, 350)) # x scaleplt.ylim((100, 800))plt.show()

2. Ilog CPLEX 表示 Piecewise Function 分段线性函数

上面的分段线性函数 用 cplex 的 Piecewise 语法表示为:

piecewise{1 -> 100; 2->200;-3}(0,300) x;

其中, 1, 2, -3 分别是3个线段的斜率, 100, 200 是3个线段的分割点(3个线段有两个分割点), 而 (0, 300) 表示分段线性函数其中一点的横坐标与纵坐标, x 是自变量。

还能写成更专业的形式:

int n=2;float objectiveForXEqualsStart=300;float breakpoint[1..n]=[100,200];float slope[1..n+1]=[1,2,-3];dvar int x;piecewise(i in 1..n) {slope[i] -> breakpoint[i]; slope[n+1]}(0,objectiveForXEqualsStart) x;

其中, slope[n+1] 表示最后一个线段的斜率

3. java Cplex 表示分段线性函数

java 的代码是:

IloCplex cplex = new IloCplex();IloNumVar x = cplex.numVar(-Double.MAX_VALUE, Double.MAX_VALUE);double[] points = {100, 200};double[] slopes = {1, 2, -3};IloNumExpr fx = cplex.piecewiseLinear(x, points, slopes, 0, 300);

4. matlab/gurobi 中的分段线性函数

matlab 目前只能针对符号函数构造分段线性函数,这在优化求解中仍然不方便。

而另一个数学规划软件 gurobi 目前只有一个 setPWLObj 方法针对目标函数设置分段线性函数。

本内容不代表本网观点和政治立场,如有侵犯你的权益请联系我们处理。
网友评论
网友评论仅供其表达个人看法,并不表明网站立场。