Esempio n. 1
0
    /**
     * Creates mixed integer problem.
     */
    private void build_mip()
    {
        int               i, j, row;
        SWIGTYPE_p_int    ind = GLPK.new_intArray(1 + 2);
        SWIGTYPE_p_double val = GLPK.new_doubleArray(1 + 2);
        String            name;

        prob = GLPK.glp_create_prob();
        GLPK.glp_set_obj_dir(prob, GLPK.GLP_MAX);
        /* create binary variables */
        x = new int[1 + n, 1 + n];
        for (i = 1; i <= n; i++)
        {
            for (j = 1; j <= n; j++)
            {
                if (i == j)
                {
                    x [i, j] = 0;
                }
                else
                {
                    x [i, j] = GLPK.glp_add_cols(prob, 1);
                    name     = "x[" + i + "," + j + "]";
                    GLPK.glp_set_col_name(prob, x [i, j], name);
                    GLPK.glp_set_col_kind(prob, x [i, j], GLPK.GLP_BV);
                    /* objective coefficient */
                    GLPK.glp_set_obj_coef(prob, x [i, j], w [i, j]);
                }
            }
        }
        /* create irreflexivity constraints (2) */
        for (i = 1; i <= n; i++)
        {
            for (j = i + 1; j <= n; j++)
            {
                row = GLPK.glp_add_rows(prob, 1);
                GLPK.glp_set_row_bnds(prob, row, GLPK.GLP_FX, 1, 1);
                GLPK.intArray_setitem(ind, 1, x [i, j]);
                GLPK.doubleArray_setitem(val, 1, 1.0);
                GLPK.intArray_setitem(ind, 2, x [j, i]);
                GLPK.doubleArray_setitem(val, 2, 1.0);
                GLPK.glp_set_mat_row(prob, row, 2, ind, val);
            }
        }

        GLPK.delete_intArray(ind);
        GLPK.delete_doubleArray(val);
    }
Esempio n. 2
0
 /// <summary>
 /// Construction du problème relaxé avec seulement les contraintes (1) et (2), pour le solveur
 /// </summary>
 private void buildProblem()
 {
     if (sol.varsIdList.Count < 2)
     {
         int               n   = data.nodeList.Count;
         SWIGTYPE_p_int    ind = GLPK.new_intArray(n - 1 + 1);
         SWIGTYPE_p_double val = GLPK.new_doubleArray(n - 1 + 1);
         problem = GLPK.glp_create_prob();                               //On créé le problème
         GLPK.glp_set_obj_dir(problem, GLPK.GLP_MIN);                    //Minimisation
         arc = new int[n, n];
         //Construit les variables
         buildVarsLow();
         //Contraintes (1)
         buildConstraint1Low(ind, val, out ind, out val);
         //Contraintes (2)
         buildConstraint2Low(ind, val, out ind, out val);
         GLPK.delete_intArray(ind);
         GLPK.delete_doubleArray(val);
     }
     else
     {
         //Sinon on peut prendre en compte les variables entrantes et sortantes
         //On répète la même opération
         enterId = sol.varsIdList[sol.varsIdList.Count - 1];
         outId   = sol.varsIdList[0];
         int               n   = data.nodeList.Count - sol.varsIdList.Count + 2;
         SWIGTYPE_p_int    ind = GLPK.new_intArray(n - 2 + 1);
         SWIGTYPE_p_double val = GLPK.new_doubleArray(n - 2 + 1);
         problem = GLPK.glp_create_prob();
         GLPK.glp_set_obj_dir(problem, GLPK.GLP_MIN);
         arc = new int[n, n];
         //Construit les variables
         buildVarsHigh(enterId, outId);
         //Contraintes (1)
         buildConstraint1High(ind, val, out ind, out val);
         //Contraintes (2)
         buildConstraint2High(ind, val, out ind, out val);
         GLPK.delete_intArray(ind);
         GLPK.delete_doubleArray(val);
     }
 }
Esempio n. 3
0
    public void solve(string[] arg)
    {
        glp_prob lp;
        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);

        try {
            // create problem
            lp = GLPK.glp_create_prob();

            // allocate workspace
            tran = GLPK.glp_mpl_alloc_wksp();

            // read model
            fname = arg [0];
            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 ApplicationException("Model file not valid: " + fname);
            }

            // generate model
            ret = GLPK.glp_mpl_generate(tran, null);
            if (ret != 0)
            {
                GLPK.glp_mpl_free_wksp(tran);
                GLPK.glp_delete_prob(lp);
                throw new ApplicationException("Cannot generate model: " + fname);
            }

            // build model
            GLPK.glp_mpl_build_prob(tran, lp);

            // set solver parameters
            iocp = new glp_iocp();
            GLPK.glp_init_iocp(iocp);
            iocp.presolve = GLPK.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, GLPK.GLP_MIP);
            }

            // free memory
            GLPK.glp_mpl_free_wksp(tran);
            GLPK.glp_delete_prob(lp);
        } catch (org.gnu.glpk.GlpkException e) {
            Console.Error.WriteLine("An error inside the GLPK library occured.");
            Console.Error.WriteLine(e.Message);
        } catch (ApplicationException e) {
            Console.Error.WriteLine(e.Message);
        }

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

        // check that the terinal hook function has been used
        if (!hookUsed)
        {
            throw new ApplicationException(
                      "The terminal output hook was not used.");
        }
    }
Esempio n. 4
0
    /**
     * Main method.
     * @param args Command line arguments
     */
    static void Main(string[] args)
    {
        glp_prob            lp;
        glp_arc             arc;
        glp_cli_arc_data    adata;
        glp_cli_vertex_data vdata;

        try {
            glp_graph graph =
                GLPK.glp_create_graph(
                    GLPK.GLP_CLI_V_SIZE,
                    GLPK.GLP_CLI_A_SIZE);
            GLPK.glp_set_graph_name(graph, "MinimumCostFlow");

            GLPK.glp_add_vertices(graph, 9);

            GLPK.glp_set_vertex_name(graph, 1, "v1");
            GLPK.glp_set_vertex_name(graph, 2, "v2");
            GLPK.glp_set_vertex_name(graph, 3, "v3");
            GLPK.glp_set_vertex_name(graph, 4, "v4");
            GLPK.glp_set_vertex_name(graph, 5, "v5");
            GLPK.glp_set_vertex_name(graph, 6, "v6");
            GLPK.glp_set_vertex_name(graph, 7, "v7");
            GLPK.glp_set_vertex_name(graph, 8, "v8");
            GLPK.glp_set_vertex_name(graph, 9, "v9");

            vdata     = GLPK.glp_cli_vertex_data_get(graph, 1);
            vdata.rhs = 20;
            vdata     = GLPK.glp_cli_vertex_data_get(graph, 9);
            vdata.rhs = -20;

            arc       = GLPK.glp_add_arc(graph, 1, 2);
            adata     = GLPK.glp_cli_arc_get_data(arc);
            adata.low = 0; adata.cap = 14; adata.cost = 0;

            arc       = GLPK.glp_add_arc(graph, 1, 4);
            adata     = GLPK.glp_cli_arc_get_data(arc);
            adata.low = 0; adata.cap = 23; adata.cost = 0;

            arc       = GLPK.glp_add_arc(graph, 2, 4);
            adata     = GLPK.glp_cli_arc_get_data(arc);
            adata.low = 0; adata.cap = 9; adata.cost = 3;

            arc       = GLPK.glp_add_arc(graph, 2, 3);
            adata     = GLPK.glp_cli_arc_get_data(arc);
            adata.low = 0; adata.cap = 10; adata.cost = 2;

            arc       = GLPK.glp_add_arc(graph, 4, 5);
            adata     = GLPK.glp_cli_arc_get_data(arc);
            adata.low = 0; adata.cap = 26; adata.cost = 0;

            arc       = GLPK.glp_add_arc(graph, 5, 2);
            adata     = GLPK.glp_cli_arc_get_data(arc);
            adata.low = 0; adata.cap = 11; adata.cost = 1;

            arc       = GLPK.glp_add_arc(graph, 3, 8);
            adata     = GLPK.glp_cli_arc_get_data(arc);
            adata.low = 0; adata.cap = 18; adata.cost = 0;

            arc       = GLPK.glp_add_arc(graph, 3, 5);
            adata     = GLPK.glp_cli_arc_get_data(arc);
            adata.low = 2; adata.cap = 12; adata.cost = 1;

            arc       = GLPK.glp_add_arc(graph, 5, 6);
            adata     = GLPK.glp_cli_arc_get_data(arc);
            adata.low = 0; adata.cap = 25; adata.cost = 5;

            arc       = GLPK.glp_add_arc(graph, 5, 7);
            adata     = GLPK.glp_cli_arc_get_data(arc);
            adata.low = 0; adata.cap = 4; adata.cost = 7;

            arc       = GLPK.glp_add_arc(graph, 6, 7);
            adata     = GLPK.glp_cli_arc_get_data(arc);
            adata.low = 0; adata.cap = 7; adata.cost = 0;

            arc       = GLPK.glp_add_arc(graph, 6, 8);
            adata     = GLPK.glp_cli_arc_get_data(arc);
            adata.low = 4; adata.cap = 8; adata.cost = 0;

            arc       = GLPK.glp_add_arc(graph, 8, 9);
            adata     = GLPK.glp_cli_arc_get_data(arc);
            adata.low = 0; adata.cap = 20; adata.cost = 9;

            arc       = GLPK.glp_add_arc(graph, 7, 9);
            adata     = GLPK.glp_cli_arc_get_data(arc);
            adata.low = 0; adata.cap = 15; adata.cost = 3;

            GLPK.glp_write_mincost(graph,
                                   GLPK.GLP_CLI_V_RHS,
                                   GLPK.GLP_CLI_A_LOW,
                                   GLPK.GLP_CLI_A_CAP,
                                   GLPK.GLP_CLI_A_COST,
                                   "mincost.dimacs");
            lp = GLPK.glp_create_prob();
            GLPK.glp_mincost_lp(lp, graph,
                                GLPK.GLP_ON,             // use symbolic names
                                GLPK.GLP_CLI_V_RHS,
                                GLPK.GLP_CLI_A_LOW,
                                GLPK.GLP_CLI_A_CAP,
                                GLPK.GLP_CLI_A_COST);
            GLPK.glp_delete_graph(graph);
            GLPK.glp_write_lp(lp, null, "mincost.lp");
            GLPK.glp_delete_prob(lp);
        } catch (org.gnu.glpk.GlpkException ex) {
            Console.WriteLine(ex.Message);
            Environment.Exit(1);
        }
    }
Esempio n. 5
0
        /// <summary>
        /// Construction du problème relaxé avec seulement les contraintes (1) et (2), pour le solveur
        /// </summary>
        private void buildProblem()
        {
            int               i, j, row;
            int               n   = data.nodeList.Count;                                                        //Nombre de noeuds du problème
            SWIGTYPE_p_int    ind = GLPK.new_intArray(n - 1 + 1);
            SWIGTYPE_p_double val = GLPK.new_doubleArray(n - 1 + 1);
            String            name;

            problem = GLPK.glp_create_prob();                                                   //On créé le problème
            GLPK.glp_set_obj_dir(problem, GLPK.GLP_MIN);                                        //Minimisation
            arc = new int[n, n];                                                                //On initialise le tableau des variables
            //On construit les variables
            for (i = 0; i < n; i++)
            {
                for (j = 0; j < n; j++)
                {
                    //On ne prend pas en compte l'arc de i vers i
                    if (i == j)
                    {
                        arc[i, j] = 0;
                    }
                    else
                    {
                        arc[i, j] = GLPK.glp_add_cols(problem, 1);                                                                              //On enregistre la variable
                        name      = i + "," + j;
                        //On créé la colonne du simplexe
                        GLPK.glp_set_col_name(problem, arc[i, j], name);                                                //Nom
                        GLPK.glp_set_col_kind(problem, arc[i, j], GLPK.GLP_BV);                                         //Valeur binaire
                        GLPK.glp_set_obj_coef(problem, arc[i, j], data.arcWeight[i, j]);                                //Coefficient
                    }
                }
            }
            //Contraintes (1)
            for (i = 0; i < n; i++)
            {
                row = GLPK.glp_add_rows(problem, 1);                                                                    //On créé la ligne du simplexe
                for (j = 0; j < n - 1; j++)
                {
                    int j2 = j;
                    //Exclusion de l'arc i->i
                    if (j >= i)
                    {
                        j2++;
                    }
                    GLPK.glp_set_row_bnds(problem, row, GLPK.GLP_FX, 1, 1);                             //On affecte la contrainte d'égalité
                    GLPK.intArray_setitem(ind, j + 1, arc[i, j2]);                                      //On considere la variable l'arc allant de i à j2
                    GLPK.doubleArray_setitem(val, j + 1, 1.0);                                          //On lui associe la valeur 1 dans la matrice du simplexe
                }
                GLPK.glp_set_mat_row(problem, row, n - 1, ind, val);                                    //On ajoute la ligne au problème
            }
            //Contraintes (2)
            for (i = 0; i < n; i++)
            {
                row = GLPK.glp_add_rows(problem, 1);                                                                    //On créé la ligne du simplexe
                for (j = 0; j < n - 1; j++)
                {
                    int j2 = j;
                    //Exclusion de l'arc i->i
                    if (j >= i)
                    {
                        j2++;
                    }
                    GLPK.glp_set_row_bnds(problem, row, GLPK.GLP_FX, 1, 1);                             //On affecte la contrainte d'égalité
                    GLPK.intArray_setitem(ind, j + 1, arc[j2, i]);                                      //On considere la variable l'arc allant de j2 à i
                    GLPK.doubleArray_setitem(val, j + 1, 1.0);                                          //On lui associe la valeur 1 dans la matrice du simplexe
                }
                GLPK.glp_set_mat_row(problem, row, n - 1, ind, val);                                    //On ajoute la ligne au problème
            }
            GLPK.delete_intArray(ind);                                                                  //On supprime les pointeurs
            GLPK.delete_doubleArray(val);                                                               //et les objets associés
        }
Esempio n. 6
0
    /**
     * Build a model with one column
     *
     * @return error error occurred
     */
    private bool run()
    {
        glp_prob          lp;
        glp_iocp          iocp;
        SWIGTYPE_p_int    ind;
        SWIGTYPE_p_double val;
        bool ret = false;

        try {
            //  Create problem
            lp = GLPK.glp_create_prob();
            Console.WriteLine("Problem created");
            GLPK.glp_set_prob_name(lp, "myProblem");

            //  Define columns
            GLPK.glp_add_cols(lp, 2);
            GLPK.glp_set_col_name(lp, 1, "x1");
            GLPK.glp_set_col_kind(lp, 1, GLPK.GLP_IV);
            GLPK.glp_set_col_bnds(lp, 1, GLPK.GLP_LO, 0, 0);
            GLPK.glp_set_col_name(lp, 2, "x2");
            GLPK.glp_set_col_kind(lp, 2, GLPK.GLP_IV);
            GLPK.glp_set_col_bnds(lp, 2, GLPK.GLP_LO, 0, 0);

            //  Create constraints
            GLPK.glp_add_rows(lp, 2);
            GLPK.glp_set_row_name(lp, 1, "c1");
            GLPK.glp_set_row_bnds(lp, 1, GLPK.GLP_UP, 0, 40);
            ind = GLPK.new_intArray(3);
            GLPK.intArray_setitem(ind, 1, 1);
            GLPK.intArray_setitem(ind, 2, 2);
            val = GLPK.new_doubleArray(3);
            GLPK.doubleArray_setitem(val, 1, 10);
            GLPK.doubleArray_setitem(val, 2, 7);
            GLPK.glp_set_mat_row(lp, 1, 2, ind, val);
            GLPK.delete_intArray(ind);
            GLPK.delete_doubleArray(val);

            ind = GLPK.new_intArray(3);
            GLPK.intArray_setitem(ind, 1, 1);
            GLPK.intArray_setitem(ind, 2, 2);
            val = GLPK.new_doubleArray(3);
            GLPK.glp_set_row_name(lp, 2, "c2");
            GLPK.glp_set_row_bnds(lp, 2, GLPK.GLP_UP, 0, 5);
            GLPK.doubleArray_setitem(val, 1, 1);
            GLPK.doubleArray_setitem(val, 2, 1);
            GLPK.glp_set_mat_row(lp, 2, 2, ind, val);
            GLPK.delete_intArray(ind);
            GLPK.delete_doubleArray(val);

            //  Define objective
            GLPK.glp_set_obj_name(lp, "obj");
            GLPK.glp_set_obj_dir(lp, GLPK.GLP_MAX);
            GLPK.glp_set_obj_coef(lp, 0, 0);
            GLPK.glp_set_obj_coef(lp, 1, 17);
            GLPK.glp_set_obj_coef(lp, 2, 12);

            //  solve model
            iocp = new glp_iocp();
            GLPK.glp_init_iocp(iocp);
            iocp.presolve = GLPK.GLP_ON;
            iocp.msg_lev  = GLPK.GLP_MSG_OFF;
            GLPK.glp_intopt(lp, iocp);

            // free memory
            GLPK.glp_delete_prob(lp);
        } catch (GlpkException ex) {
            Console.WriteLine(ex.Message);
            ret = true;
        }
        return(ret);
    }
Esempio n. 7
0
File: lp.cs Progetto: chyng2000/pro
    static void Main(string[] args)
    {
        glp_prob          lp;
        glp_smcp          parm;
        SWIGTYPE_p_int    ind;
        SWIGTYPE_p_double val;
        int ret;

        try {
            lp = GLPK.glp_create_prob();
            Console.WriteLine("Problem created");

            // Define columns
            GLPK.glp_add_cols(lp, 3);
            GLPK.glp_set_col_name(lp, 1, "x1");
            GLPK.glp_set_col_kind(lp, 1, GLPK.GLP_CV);
            GLPK.glp_set_col_bnds(lp, 1, GLPK.GLP_DB, 0, .5);
            GLPK.glp_set_col_name(lp, 2, "x2");
            GLPK.glp_set_col_kind(lp, 2, GLPK.GLP_CV);
            GLPK.glp_set_col_bnds(lp, 2, GLPK.GLP_DB, 0, .5);
            GLPK.glp_set_col_name(lp, 3, "x3");
            GLPK.glp_set_col_kind(lp, 3, GLPK.GLP_CV);
            GLPK.glp_set_col_bnds(lp, 3, GLPK.GLP_DB, 0, .5);

            // Create constraints

            // Allocate memory
            ind = GLPK.new_intArray(3);
            val = GLPK.new_doubleArray(3);

            // Create rows
            GLPK.glp_add_rows(lp, 2);

            // Set row details
            GLPK.glp_set_row_name(lp, 1, "c1");
            GLPK.glp_set_row_bnds(lp, 1, GLPK.GLP_DB, 0, 0.2);
            GLPK.intArray_setitem(ind, 1, 1);
            GLPK.intArray_setitem(ind, 2, 2);
            GLPK.doubleArray_setitem(val, 1, 1.0);
            GLPK.doubleArray_setitem(val, 2, -.5);
            GLPK.glp_set_mat_row(lp, 1, 2, ind, val);

            GLPK.glp_set_row_name(lp, 2, "c2");
            GLPK.glp_set_row_bnds(lp, 2, GLPK.GLP_UP, 0, 0.4);
            GLPK.intArray_setitem(ind, 1, 2);
            GLPK.intArray_setitem(ind, 2, 3);
            GLPK.doubleArray_setitem(val, 1, -1.0);
            GLPK.doubleArray_setitem(val, 2, 1.0);
            GLPK.glp_set_mat_row(lp, 2, 2, ind, val);

            // Free memory
            GLPK.delete_intArray(ind);
            GLPK.delete_doubleArray(val);

            // Define objective
            GLPK.glp_set_obj_name(lp, "z");
            GLPK.glp_set_obj_dir(lp, GLPK.GLP_MIN);
            GLPK.glp_set_obj_coef(lp, 0, 1.0);
            GLPK.glp_set_obj_coef(lp, 1, -.5);
            GLPK.glp_set_obj_coef(lp, 2, .5);
            GLPK.glp_set_obj_coef(lp, 3, -1);

            // Solve model
            parm = new glp_smcp();
            GLPK.glp_init_smcp(parm);
            ret = GLPK.glp_simplex(lp, parm);

            // Retrieve solution
            if (ret == 0)
            {
                write_lp_solution(lp);
            }
            else
            {
                Console.WriteLine("The problem could not be solved");
            }

            // Free memory
            GLPK.glp_delete_prob(lp);
        } catch (GlpkException) {
            Console.WriteLine("Error caught");
            ret = 1;
        }

        Environment.Exit(ret);
    }