|
From: | yoyovicks |
Subject: | Re: [Help-glpk] does anyone use yoyo's GLPKSharp? |
Date: | Fri, 29 May 2009 15:31:56 +0200 (CEST) |
> Message du 27/05/09 17:38
> De : "bo liu"
> A : "yoyovicks"
> Copie à : address@hidden
> Objet : Re: [Help-glpk] does anyone use yoyo's GLPKSharp?
>
>yoyovicks,
i'm so happy that i can receive your reply. this is the example of GLPK.pdf named sample.c .
maximize
> z = 10x1 + 6x2 + 4x3
> subject to
> x1 + x2 + x3 <= 100
> 10x1 + 4x2 + 5x3 <= 600
> 2x1 + 2x2 + 6x3 <= 300
> where all variables are non-negative
> x1>= 0, x2>=0, x3>=0this is a simple LP problem , and i want to use your GLPKSharp.dll to solve some problem like this (because i use C# and VS2005 too),so i translate it into your GLPKSharp form to study how to use your GLPKSharp.dll .
in this example ,the coefficients matrix of structural variables are prepared like this:
s21: ia[1] = 1, ja[1] = 1, ar[1] = 1.0; /* a[1,1] = 1 */
> s22: ia[2] = 1, ja[2] = 2, ar[2] = 1.0; /* a[1,2] = 1 */
> s23: ia[3] = 1, ja[3] = 3, ar[3] = 1.0; /* a[1,3] = 1 */
> s24: ia[4] = 2, ja[4] = 1, ar[4] = 10.0; /* a[2,1] = 10 */
> s25: ia[5] = 3, ja[5] = 1, ar[5] = 2.0; /* a[3,1] = 2 */
> s26: ia[6] = 2, ja[6] = 2, ar[6] = 4.0; /* a[2,2] = 4 */
> s27: ia[7] = 3, ja[7] = 2, ar[7] = 2.0; /* a[3,2] = 2 */
> s28: ia[8] = 2, ja[8] = 3, ar[8] = 5.0; /* a[2,3] = 5 */
> s29: ia[9] = 3, ja[9] = 3, ar[9] = 6.0; /* a[3,3] = 6 */
> s30: glp_load_matrix(lp, 9, ia, ja, ar);
if i use theGLPKSharp.dll , i can't accomplish it like this.
but finally i achieve it by this form:
int[] a = new int[] { 0, 1, 2, 3 };
p.SetMatRow(1, a, new double[] { 0, 1.0, 1.0, 1.0 });
> p.SetMatRow(2, a, new double[] { 0, 10.0, 4.0, 5.0 });
> p.SetMatRow(3, a, new double[] { 0, 2.0, 2.0, 6.0 });
i think the array a[] must be the array of column index,right?but in this array the first value is 0. i also know that GLPK need the array starts from a[1] instead of a[0] ,but in the C# array starts from a[0].Is this why the first value of array a[] is 0? i only add 3 columns p.AddCols(3) . but it works. i want to know that if this method works in other LP problem.
could it be that the GLPK read the array a[] from a[1]? the a[0] is left out.
and the new double[] { 0, 1.0, 1.0, 1.0 } are also the same as a[]?
but i still don't know how to use the p.LoadMatrix through your GLPKSharp.dll .
the follow is the your API form translate from sample.c (i made it ,and it works.this is my first time using GLPK through API and i use it in the WebForm)
using System;
> using System.Data;
> using System.Configuration;
> using System.Collections;
> using System.Web;
> using System.Web.Security;
> using System.Web.UI;
> using System.Web.UI.WebControls;
> using System.Web.UI.WebControls.WebParts;
> using System.Web.UI.HtmlControls;
> using GlpkSharp;protected void Page_Load(object sender, EventArgs e)
> {
> double z, x1, x2, x3;
> LPProblem p = new LPProblem();
> p.Name = "LEO";
> p.ObjectiveName = "test";
> p.ObjectiveDirection = OptimisationDirection.MAXIMISE;
> p.AddRows(3);
> p.SetRowName(1, "p");
> p.SetRowBounds(1, BOUNDSTYPE.Upper, 0.0, 100.0);
> p.SetRowName(2, "q");
> p.SetRowBounds(2, BOUNDSTYPE.Upper, 0.0, 600.0);
> p.SetRowName(3, "r");
> p.SetRowBounds(3, BOUNDSTYPE.Upper, 0.0, 300.0);
> p.AddCols(3);
> p.SetColName(1, "x1");
> p.SetColBounds(1, BOUNDSTYPE.Lower, 0.0, 0.0);
> p.SetObjCoef(1, 10.0);
> p.SetColName(2, "x2");
> p.SetColBounds(2, BOUNDSTYPE.Lower, 0.0, 0.0);
> p.SetObjCoef(2, 6.0);
> p.SetColName(3, "x3");
> p.SetColBounds(3, BOUNDSTYPE.Lower, 0.0, 0.0);
> p.SetObjCoef(3, 4.0);
int[] a = new int[] { 0, 1, 2, 3 };
p.SetMatRow(1, a, new double[] { 0, 1.0, 1.0, 1.0 });
> p.SetMatRow(2, a, new double[] { 0, 10.0, 4.0, 5.0 });
> p.SetMatRow(3, a, new double[] { 0, 2.0, 2.0, 6.0 });
>try
> {p.SolveSimplex();
> p.WriteSol("c:\\test1.txt");
> z = p.GetObjectiveValue();
> x1 = p.GetColPrimal(1);
> x2 = p.GetColPrimal(2);
> x3 = p.GetColPrimal(3);
> Response.Write(z + "
");
> Response.Write(x1 + "
");
> Response.Write(x2 + "
");
> Response.Write(x3 + "
");
> }
> catch (Exception ex)
> {
> Response.Write(ex.Message);
> }
>
[Prev in Thread] | Current Thread | [Next in Thread] |