首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >Glpk java和.mod文件

Glpk java和.mod文件
EN

Stack Overflow用户
提问于 2015-03-30 12:26:53
回答 2查看 856关注 0票数 1

我有一个.mod文件,我可以在java中运行它(使用netbeans)。该文件从另一个文件.dat获取数据,因为开发它的人使用GUSEK。现在我们需要用java实现它,但是我不知道如何将数据放在.mod文件中的K常量中。

不重要的方式,可以通过数据库查询或文件读取。

我对数学编程一无所知,我只需要为已经生成的glpk函数添加值。

下面是.mod函数:

代码语言:javascript
复制
# OPRE

set K;

param mc {k in K};
param phi {k in K};
param cman {k in K};
param ni {k in K};
param cesp;
param mf;

var x {k in K} binary;

minimize custo: sum {k in K} (mc[k]*phi[k]*(1-x[k]) + cman[k]*phi[k]*x[k]);

s.t. recursos: sum {k in K} (cman[k]*phi[k]*x[k]) - cesp <= 0;
s.t. ocorrencias: sum {k in K} (ni[k] + (1-x[k])*phi[k]) - mf <= 0;

end;

以下是java代码:

代码语言:javascript
复制
package br.com.genera.service.otimi;

import org.gnu.glpk.*;

public class Gmpl implements GlpkCallbackListener, GlpkTerminalListener {

    private boolean hookUsed = false;

    public static void main(String[] arg) {

        String[] nomeArquivo = new String[2];
        nomeArquivo[0] = "C:\\PodaEquipamento.mod";

        System.out.println(nomeArquivo[0]);
        GLPK.glp_java_set_numeric_locale("C");
        System.out.println(nomeArquivo[0]);
        new Gmpl().solve(nomeArquivo);
    }

    public void solve(String[] arg) {
        glp_prob lp = null;
        glp_tran tran;
        glp_iocp iocp;

        String fname;
        int skip = 0;
        int ret;

        // listen to callbacks
        GlpkCallback.addListener(this);
        // listen to terminal output
        GlpkTerminal.addListener(this);

        fname = arg[0];

        lp = GLPK.glp_create_prob();
        System.out.println("Problem created");
        tran = GLPK.glp_mpl_alloc_wksp();
        ret = GLPK.glp_mpl_read_model(tran, fname, skip);
        if (ret != 0) {
            GLPK.glp_mpl_free_wksp(tran);
            GLPK.glp_delete_prob(lp);
            throw new RuntimeException("Model file not found: " + fname);
        }

        // generate model
        GLPK.glp_mpl_generate(tran, null);
        // build model
        GLPK.glp_mpl_build_prob(tran, lp);
        // set solver parameters
        iocp = new glp_iocp();
        GLPK.glp_init_iocp(iocp);
        iocp.setPresolve(GLPKConstants.GLP_ON);

        // do not listen to output anymore
        GlpkTerminal.removeListener(this);
        // solve model
        ret = GLPK.glp_intopt(lp, iocp);
        // postsolve model
        if (ret == 0) {
            GLPK.glp_mpl_postsolve(tran, lp, GLPKConstants.GLP_MIP);
        }
        // free memory
        GLPK.glp_mpl_free_wksp(tran);
        GLPK.glp_delete_prob(lp);

        // do not listen for callbacks anymore
        GlpkCallback.removeListener(this);

        // check that the hook function has been used for terminal output.
        if (!hookUsed) {
            System.out.println("Error: The terminal output hook was not used.");
            System.exit(1);
        }
    }

    @Override
    public boolean output(String str) {
        hookUsed = true;
        System.out.print(str);
        return false;
    }

    @Override
    public void callback(glp_tree tree) {
        int reason = GLPK.glp_ios_reason(tree);
        if (reason == GLPKConstants.GLP_IBINGO) {
            System.out.println("Better solution found");
        }
    }
}

我把这个放在控制台里:

代码语言:javascript
复制
Reading model section from C:\PodaEquipamento.mod...
33 lines were read
Generating custo...
C:\PodaEquipamento.mod:24: no value for K
glp_mpl_build_prob: invalid call sequence

希望有人能帮忙谢谢。

EN

回答 2

Stack Overflow用户

回答已采纳

发布于 2015-04-08 10:58:58

我解决了将数据块从.data文件复制到.mod文件的问题。

不管怎样,谢谢你。

票数 0
EN

Stack Overflow用户

发布于 2015-03-31 08:49:19

最好的方法是按照读取模型文件的方式读取数据文件。

代码语言:javascript
复制
ret = GLPK.glp_mpl_read_data(tran, fname_data, skip);
if (ret != 0) {
    GLPK.glp_mpl_free_wksp(tran);
    GLPK.glp_delete_prob(lp);
    throw new RuntimeException("Data file not found: " + fname_data);
}
票数 3
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/29346236

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档