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);
    }
        // 制約式の係数
        public void LoadMatrix(int size, int[] ia, int[] ja, double[] ar)
        {
            // 係数を問題に代入する
            var ia_ = GLPK.new_intArray(size + 1);
            var ja_ = GLPK.new_intArray(size + 1);
            var ar_ = GLPK.new_doubleArray(size + 1);

            for (int i = 0; i < size; ++i)
            {
                GLPK.intArray_setitem(ia_, i + 1, ia[i] + 1);
                GLPK.intArray_setitem(ja_, i + 1, ja[i] + 1);
                GLPK.doubleArray_setitem(ar_, i + 1, ar[i]);
            }
            GLPK.glp_load_matrix(problem, size, ia_, ja_, ar_);
            GLPK.delete_intArray(ia_);
            GLPK.delete_intArray(ja_);
            GLPK.delete_doubleArray(ar_);
        }
Esempio n. 3
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);
     }
 }
 public Dictionary <int, double> this[int index] {
     get {
         var matrix = new Dictionary <int, double>();
         // まずは変数に読み込む
         var length = GLPK.glp_get_mat_row(mip.problem, index + 1, null, null);
         var key    = GLPK.new_intArray(length + 1);
         var value  = GLPK.new_doubleArray(length + 1);
         GLPK.glp_get_mat_row(mip.problem, index + 1, key, value);
         // 次にDictionaryにコピーする
         for (int i = 1; i <= length; ++i)
         {
             matrix.Add(
                 GLPK.intArray_getitem(key, i) - 1,
                 GLPK.doubleArray_getitem(value, i)
                 );
         }
         // 最後に後片付けする
         GLPK.delete_intArray(key);
         GLPK.delete_doubleArray(value);
         return(matrix);
     }
 }
Esempio n. 5
0
    /**
     * Generates violated transitivity constraints and adds them to the current
     * subproblem. As suggested by Juenger et al., only only arc-disjoint
     * violated constraints are considered.
     *
     * @return number of generated constraints
     */
    private int generate_rows()
    {
        int i, j, k, cnt, row;

        int[,] u;
        SWIGTYPE_p_int    ind = GLPK.new_intArray(1 + 3);
        SWIGTYPE_p_double val = GLPK.new_doubleArray(1 + 3);
        double            r;

        /* u[i,j] = 1, if arc (i->j) is covered by some constraint */
        u   = new int[1 + n, 1 + n];
        cnt = 0;
        for (i = 1; i <= n; i++)
        {
            for (j = 1; j <= n; j++)
            {
                for (k = 1; k <= n; k++)
                {
                    if (i == j)
                    {
                    }
                    else if (i == k)
                    {
                    }
                    else if (j == k)
                    {
                    }
                    else if (u [i, j] != 0 || u [j, i] != 0)
                    {
                    }
                    else if (u [i, k] != 0 || u [k, i] != 0)
                    {
                    }
                    else if (u [j, k] != 0 || u [k, j] != 0)
                    {
                    }
                    else
                    {
                        /* check if x[i,j] + x[j,k] + x[k,i] <= 2 */
                        r = GLPK.glp_get_col_prim(prob, x [i, j])
                            + GLPK.glp_get_col_prim(prob, x [j, k])
                            + GLPK.glp_get_col_prim(prob, x [k, i]);

                        /* should note that it is not necessary to add to the
                         * current subproblem every violated constraint (3), for
                         * which r > 2; if r < 3, we can stop adding violated
                         * constraints, because for integer feasible solution
                         * the value of r is integer, so r < 3 is equivalent to
                         * r <= 2; on the other hand, adding violated
                         * constraints leads to tightening the feasible region
                         * of LP relaxation and, thus, may reduce the size of
                         * the search tree */
                        if (r > 2.15)
                        {
                            /* generate violated transitivity constraint */
                            row = GLPK.glp_add_rows(prob, 1);
                            GLPK.glp_set_row_bnds(prob, row,
                                                  GLPK.GLP_UP, 0, 2);
                            GLPK.intArray_setitem(ind, 1, x [i, j]);
                            GLPK.doubleArray_setitem(val, 1, 1);
                            GLPK.intArray_setitem(ind, 2, x [j, k]);
                            GLPK.doubleArray_setitem(val, 2, 1);
                            GLPK.intArray_setitem(ind, 3, x [k, i]);
                            GLPK.doubleArray_setitem(val, 3, 1);
                            GLPK.glp_set_mat_row(prob, row, 3, ind, val);
                            u [i, j] = u [j, i] = 1;
                            u [i, k] = u [k, i] = 1;
                            u [j, k] = u [k, j] = 1;
                            cnt++;
                        }
                    }
                }
            }
        }
        GLPK.delete_intArray(ind);
        GLPK.delete_doubleArray(val);
        Console.WriteLine(cnt + " violated constraints were generated");
        return(cnt);
    }
Esempio n. 6
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. 7
0
        /// <summary>
        /// Génère les contraintes violées
        /// </summary>
        /// <returns></returns>
        private int generate_rows()
        {
            int               row;
            int               cnt  = 0;
            int               n    = data.nodeList.Count;
            List <Route>      subs = getSubroutes(false);       //Calcule les sous routes
            SWIGTYPE_p_int    ind;
            SWIGTYPE_p_double val;

            if (subs.Count > 1)
            {
                OnPartialSolutionFound(new SolutionFoundEventArgs(subs));                       //On retourne une solution partielle
                foreach (Route sub in subs)
                {
                    //Si la sous route a plus de deux noeuds
                    if (sub.nodesIndex.Count >= 2)
                    {
                        double constraintValue = 0;
                        for (int i = 0; i < sub.nodesIndex.Count - 1; i++)
                        {
                            constraintValue += GLPK.glp_get_col_prim(problem, arc[sub.nodesIndex[i], sub.nodesIndex[i + 1]]);
                        }
                        constraintValue += GLPK.glp_get_col_prim(problem, arc[sub.nodesIndex[sub.nodesIndex.Count - 1], sub.nodesIndex[0]]);


                        if (constraintValue > sub.nodesIndex.Count - 1)
                        {
                            cnt++;
                            ind = GLPK.new_intArray(sub.nodesIndex.Count + 1);
                            val = GLPK.new_doubleArray(sub.nodesIndex.Count + 1);

                            row = GLPK.glp_add_rows(problem, 1);
                            GLPK.glp_set_row_bnds(problem, row, GLPK.GLP_UP, 0, sub.nodesIndex.Count - 1);
                            for (int i = 0; i < sub.nodesIndex.Count - 1; i++)
                            {
                                GLPK.intArray_setitem(ind, i + 1, arc[sub.nodesIndex[i], sub.nodesIndex[i + 1]]);
                                GLPK.doubleArray_setitem(val, i + 1, 1);
                            }
                            GLPK.intArray_setitem(ind, sub.nodesIndex.Count - 1 + 1, arc[sub.nodesIndex[sub.nodesIndex.Count - 1], sub.nodesIndex[0]]);
                            GLPK.doubleArray_setitem(val, sub.nodesIndex.Count - 1 + 1, 1);
                            GLPK.glp_set_mat_row(problem, row, sub.nodesIndex.Count, ind, val);
                            GLPK.delete_intArray(ind);
                            GLPK.delete_doubleArray(val);
                        }
                    }
                }
            }
            ind = GLPK.new_intArray(n - 1 + 1);
            val = GLPK.new_doubleArray(n - 1 + 1);

            for (int i = 0; i < n; i++)
            {
                double cVal = 0;

                for (int j = 0; j < n - 1; j++)
                {
                    int j2 = j;

                    if (j >= i)
                    {
                        j2++;
                    }

                    cVal += GLPK.glp_get_col_prim(problem, arc[i, j2]);
                }

                if (Math.Abs(cVal - 1) > Math.Pow(10, -5))
                {
                    cnt++;

                    row = GLPK.glp_add_rows(problem, 1);                                            //On créé la ligne du simplexe

                    for (int k = 0; k < n - 1; k++)
                    {
                        int k2 = k;

                        //Exclusion de l'arc i->i
                        if (k >= i)
                        {
                            k2++;
                        }


                        GLPK.glp_set_row_bnds(problem, row, GLPK.GLP_FX, 1, 1);                         //On affecte la contrainte d'égalité
                        GLPK.intArray_setitem(ind, k + 1, arc[i, k2]);                                  //On considere la variable l'arc allant de i à j2
                        GLPK.doubleArray_setitem(val, k + 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
                }
            }

            ind = GLPK.new_intArray(n - 1 + 1);
            val = GLPK.new_doubleArray(n - 1 + 1);

            for (int i = 0; i < n; i++)
            {
                double cVal = 0;

                for (int j = 0; j < n - 1; j++)
                {
                    int j2 = j;

                    if (j >= i)
                    {
                        j2++;
                    }

                    cVal += GLPK.glp_get_col_prim(problem, arc[j2, i]);
                }

                if (Math.Abs(cVal - 1) > Math.Pow(10, -5))
                {
                    cnt++;

                    row = GLPK.glp_add_rows(problem, 1);                                            //On créé la ligne du simplexe

                    for (int k = 0; k < n - 1; k++)
                    {
                        int k2 = k;

                        //Exclusion de l'arc i->i
                        if (k >= i)
                        {
                            k2++;
                        }

                        GLPK.glp_set_row_bnds(problem, row, GLPK.GLP_FX, 1, 1);                         //On affecte la contrainte d'égalité
                        GLPK.intArray_setitem(ind, k + 1, arc[k2, i]);                                  //On considere la variable l'arc allant de i à j2
                        GLPK.doubleArray_setitem(val, k + 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);
            GLPK.delete_doubleArray(val);

            logger.log(cnt + " violated constraints were generated", Message.Level.Normal);
            Console.WriteLine(cnt + " violated constraints were generated");
            return(cnt);
        }
Esempio n. 8
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. 9
0
        private int generate_rows()
        {
            int row;

            List <Route> subs = getSubroutes(false);

            if (subs.Count <= 1)
            {
                return(0);
            }
            else
            {
                int n = nodeCount;

                int cnt = 0;

                SWIGTYPE_p_int    ind = GLPK.new_intArray(n - 1 + 1);
                SWIGTYPE_p_double val = GLPK.new_doubleArray(n - 1 + 1);

                for (int i = 0; i < nodeCount; i++)
                {
                    int arcCount = 0;

                    for (int j = 0; j < nodeCount - 1; j++)
                    {
                        int j2 = j;

                        if (j >= i)
                        {
                            j2++;
                        }

                        if (GLPK.glp_get_col_prim(problem, arc[i, j2]) == 1)
                        {
                            arcCount++;
                        }
                    }

                    if (arcCount != 1)
                    {
                        cnt++;

                        row = GLPK.glp_add_rows(problem, 1);                                                //On créé la ligne du simplexe

                        for (int k = 0; k < n - 1; k++)
                        {
                            int k2 = k;

                            //Exclusion de l'arc i->i
                            if (k >= i)
                            {
                                k2++;
                            }


                            GLPK.glp_set_row_bnds(problem, row, GLPK.GLP_FX, 1, 1);                             //On affecte la contrainte d'égalité
                            GLPK.intArray_setitem(ind, k + 1, arc[i, k2]);                                      //On considere la variable l'arc allant de i à j2
                            GLPK.doubleArray_setitem(val, k + 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
                    }
                }

                for (int i = 0; i < nodeCount; i++)
                {
                    cnt++;

                    int arcCount = 0;

                    for (int j = 0; j < nodeCount - 1; j++)
                    {
                        int j2 = j;

                        if (j >= i)
                        {
                            j2++;
                        }

                        if (GLPK.glp_get_col_prim(problem, arc[j2, i]) == 1)
                        {
                            arcCount++;
                        }
                    }

                    if (arcCount != 1)
                    {
                        row = GLPK.glp_add_rows(problem, 1);                                                //On créé la ligne du simplexe

                        for (int k = 0; k < n - 1; k++)
                        {
                            int k2 = k;

                            //Exclusion de l'arc i->i
                            if (k >= i)
                            {
                                k2++;
                            }

                            GLPK.glp_set_row_bnds(problem, row, GLPK.GLP_FX, 1, 1);                             //On affecte la contrainte d'égalité
                            GLPK.intArray_setitem(ind, k + 1, arc[k2, i]);                                      //On considere la variable l'arc allant de i à j2
                            GLPK.doubleArray_setitem(val, k + 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);
                GLPK.delete_doubleArray(val);

                //logger.log(cnt + " violated constraints were generated", Message.Level.Normal);
                //Console.WriteLine(cnt + " violated constraints were generated");
                return(cnt);
            }
        }
Esempio n. 10
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);
    }