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

1. 什么是分段线性函数

Piecewise function (分段线性函数)是一组线段组成的函数(一般为连续函数)。
分段线性拟合可以用来拟合曲线.

例如下面这个函数
f(x)={300+xx≤100300+100+2(x−100)100&lt;x≤200300+100+2∗(200−100)−3(x−200)x&gt;200 f(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(x100)300+100+2(200100)3(x200)x100100<x200x>200
是一个三段线性函数,它的图像是:

在这里插入图片描述

画图的 python 代码:

import matplotlib.pyplot as plt
import numpy as np

import matplotlib # for writing complex latex equations
matplotlib.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 scale
plt.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 方法针对目标函数设置分段线性函数。

转载于:https://www.cnblogs.com/robinchen/p/11047525.html

  • 1
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值