zoukankan      html  css  js  c++  java
  • LINGO 解线性方程 例子

    简介

    没有什么比一个例子更好讲解Lingo的了,不行那就两个... ...

    Question

    已知某种商品6个仓库的存货量,8个客户对该商品的需求量,单位商品运价如下所示,试确定6个仓库到8个客户的商品调运数量,使总的运输费用最小。
    c=
    6 2 6 7 4 2 5 9
    4 9 5 3 8 5 8 2
    5 2 1 9 7 4 3 3
    7 6 7 3 9 2 7 1
    2 3 9 5 7 2 6 5
    5 5 2 2 8 1 4 3;
    6行 每行表示仓库  每列表示客户,中间的数值表示 仓库到客户的单位运价
    

    解题方程

    [min sum_{i=1}^{6} sum_{j=1}^{8} c_{i j} x_{i} ]

    [s . t .left{egin{array}{c} sum_{j=1}^{8} x_{i j} leq e_{i}, i=1,2, ldots, 6 \ sum_{i=1}^{6} x_{i j}=d_{j}, j=1,2, ldots, 8 \ x_{i j} geq 0, i=1,2, ldots, 6 ; j=1,2, ldots, 8 end{array} ight.]

    这个方程是什么意思呢?

    第一个表示:每个地方的单位运费 * 运送的单位数,要让这个结果最小
    第二个表示约束:

    约束1:每个仓库的输出的量要小于等于这个仓库中的量
    约束2:每个客户收到的量等于所有仓库发送到这个客户的量
    约束3:每个仓库的输出量为正数

    Lingo 方程

    model:
    sets:
      warehouses/1..6/:e;
      vendors/1..8/:d;
      links(warehouses, vendors):c,x;
    endsets
    data: !数据部分;
    e=60 55 51 43 41 52; !属性值 表示每个仓库的存货量;
    d=35 37 22 32 41 32 43 38; ! 表示需求量;
    c=6 2 6 7 4 2 5 9
    4 9 5 3 8 5 8 2
    5 2 1 9 7 4 3 3
    7 6 7 3 9 2 7 1
    2 3 9 5 7 2 6 5
    5 5 2 2 8 1 4 3;
    enddata
    min=@sum(links(i,j):c(i,j)*x(i,j)); !目标函数;
    @for(warehouses(i):@sum(vendors(j):x(i,j)) <= e(i)); !约束条件;
    @for(vendors(j):@sum(warehouses(i):x(i,j)) = d(j));
    end
    

    Focus

    我们学习lingo的目标就是如何根据我们建立好的数学模型正确书写出他的程式。

    第一
    模型书写在 model: end之中
    第二
    sets 集合的书写方式 warehouses/1..6/:e;
    links 构建二维集合
    第三
    data: enddata 对sets进行赋值
    data 是从1开始
    第四
    sum 和 for 都是 两部分的 第一部分:集合的名称 第二部分:表达式

    examle: 求特征值和矩阵中的未知的参数

    [A=left(egin{array}{ccc} 2 & -1 & 2 \ 5 & a & 3 \ -1 & b & -2 end{array} ight)]

    [P=left(egin{array}{c} 1 \ 1 \ -1 end{array} ight)]

    其中P是A的一个特征向量,求参数a,b及特征向量P所对应的特征值

    解方程Ap=lambda p

    lambda 是特征向量的特征值
    code

    model:
    sets:
    num/1..3/:p;
    link(num,num):a;
    endsets
    data:
    p=1 1 -1;
    a=2 -1 2
    5,,3
    -1,,-2;! 两个逗号之间的参数待定;
    enddata
    @for(num(i):@sum(num(j):a(i,j)*p(j)) = lambda * p(i));
    @free(lambda);!特征值可正可负;
    @for(link:@free(a));!注意未知参数取值是可正可负的;
    end
    

    答案

                                               Variable           Value
                                                 LAMBDA       -1.000000
                                                  P( 1)        1.000000
                                                  P( 2)        1.000000
                                                  P( 3)       -1.000000
                                               A( 1, 1)        2.000000
                                               A( 1, 2)       -1.000000
                                               A( 1, 3)        2.000000
                                               A( 2, 1)        5.000000
                                               A( 2, 2)       -3.000000
                                               A( 2, 3)        3.000000
                                               A( 3, 1)       -1.000000
                                               A( 3, 2)        0.000000
                                               A( 3, 3)       -2.000000
    
    Hope is a good thing,maybe the best of things,and no good thing ever dies.----------- Andy Dufresne
  • 相关阅读:
    Eclipse
    JAVA
    .Net Core下使用WCF—— Consuming WCF Services in .NET Core – Best Practices
    xml转class ——xsd实现
    从已有container中生成新的image&打标签——Creating a Docker Image from an Existing Container
    How to install xfs and create xfs file system on Debian/Ubuntu Linux
    Ubuntu系统安装软件包(其他软件包的安装 思路类似)—— Steps to Install XFS Package in Ubuntu
    postgresql——SQL update fields of one table from fields of another one(列的批量更新)
    skype邮件撤回——步骤
    单元测试 _ Unit testing best practices with .NET Core and .NET Standard
  • 原文地址:https://www.cnblogs.com/eat-too-much/p/13265520.html
Copyright © 2011-2022 走看看