lemon-project-template-glpk

diff deps/glpk/examples/t1.cs @ 9:33de93886c88

Import GLPK 4.47
author Alpar Juttner <alpar@cs.elte.hu>
date Sun, 06 Nov 2011 20:59:10 +0100
parents
children
line diff
     1.1 --- /dev/null	Thu Jan 01 00:00:00 1970 +0000
     1.2 +++ b/deps/glpk/examples/t1.cs	Sun Nov 06 20:59:10 2011 +0100
     1.3 @@ -0,0 +1,99 @@
     1.4 +/*Find the minimum value which satisfies the linear inequality:
     1.5 +    a*x + b*y >= 1 {a,b,x,y Integers} {a,b > 0} {a,b entered on command line}
     1.6 +
     1.7 +  Nigel_Galloway@operamail.com
     1.8 +  February 2008
     1.9 +*/
    1.10 +using System;
    1.11 +using System.Runtime.InteropServices;
    1.12 +
    1.13 +unsafe class GLPK{
    1.14 +  double *lp;
    1.15 +  public int a;
    1.16 +  public int b;
    1.17 +
    1.18 +  const string glpkLibrary = "libglpk.so";
    1.19 +  readonly int GLP_FR = 1;
    1.20 +  readonly int GLP_IV = 2;
    1.21 +  readonly int GLP_DB = 4;
    1.22 +  struct ConstraintMatrix{
    1.23 +    public fixed int ia[3];
    1.24 +    public fixed int ja[3];
    1.25 +    public fixed double ar[3];
    1.26 +  }
    1.27 +  [DllImport(glpkLibrary, SetLastError=true)]
    1.28 +  static extern double* glp_create_prob();
    1.29 +  [DllImport(glpkLibrary, SetLastError=true)]
    1.30 +  static extern void glp_add_rows(double* lp, int rows);
    1.31 +  [DllImport(glpkLibrary, SetLastError=true)]
    1.32 +  static extern void glp_add_cols(double* lp, int cols);
    1.33 +  [DllImport(glpkLibrary, SetLastError=true)]
    1.34 +  static extern void glp_set_col_bnds(double* lp, int col, int bound_type, double lower_bound, double upper_bound);
    1.35 +  [DllImport(glpkLibrary, SetLastError=true)]
    1.36 +  static extern void glp_set_col_kind(double* lp, int col, int kind);
    1.37 +  [DllImport(glpkLibrary, SetLastError=true)]
    1.38 +  static extern void glp_load_matrix(double* lp, int elements, int* ia, int* ja, double* ar);
    1.39 +  public GLPK(int a, int b){
    1.40 +    this.a = a;
    1.41 +    this.b = b;
    1.42 +    lp = glp_create_prob();
    1.43 +    //Col 1 is x, Col 2 is y
    1.44 +    glp_add_rows(lp, 1);
    1.45 +    glp_add_cols(lp, 2);
    1.46 +    glp_set_col_bnds(lp, 1, GLP_FR, 0.0, 0.0);
    1.47 +    glp_set_col_bnds(lp, 2, GLP_FR, 0.0, 0.0);
    1.48 +    glp_set_col_kind(lp, 1, GLP_IV);
    1.49 +    glp_set_col_kind(lp, 2, GLP_IV);
    1.50 +    //Row 1 is a*x + b*y
    1.51 +    ConstraintMatrix CM = new ConstraintMatrix();
    1.52 +    CM.ia[1]=1; CM.ja[1]=1; CM.ar[1]=a;
    1.53 +    CM.ia[2]=1; CM.ja[2]=2; CM.ar[2]=b;
    1.54 +    glp_load_matrix(lp, 2, CM.ia, CM.ja, CM.ar);
    1.55 +    Console.WriteLine("Hello Nigel");
    1.56 +  }
    1.57 +
    1.58 +  [DllImport(glpkLibrary, SetLastError=true)]
    1.59 +  static extern void glp_set_row_bnds(double* lp, int row, int bound_type, double lower_bound, double upper_bound);
    1.60 +  [DllImport(glpkLibrary, SetLastError=true)]
    1.61 +  static extern void glp_simplex(double* lp, void* options);
    1.62 +  [DllImport(glpkLibrary, SetLastError=true)]
    1.63 +  static extern void glp_intopt(double* lp, void* options);
    1.64 +  [DllImport(glpkLibrary, SetLastError=true)]
    1.65 +  static extern double glp_mip_col_val(double* lp, int col);
    1.66 +  public int betterGuess(int upper_bound){
    1.67 +    //Find a new guess less than the old guess: 1 <= (a*x + b*y) <= (old guess - 1)
    1.68 +    glp_set_row_bnds(lp, 1, GLP_DB, 1.0, ((double)upper_bound)-0.5);
    1.69 +    glp_simplex(lp, null);
    1.70 +    glp_intopt(lp, null);
    1.71 +    int x = (int)glp_mip_col_val(lp, 1);
    1.72 +    int y = (int)glp_mip_col_val(lp, 2);
    1.73 +    int nextGuess = a*x + b*y;
    1.74 +    Console.WriteLine("x = {0}, y = {1}, a*x + b*y = {2}",x,y,nextGuess);
    1.75 +    return nextGuess;
    1.76 +  }
    1.77 +
    1.78 +  [DllImport(glpkLibrary, SetLastError=true)]
    1.79 +  static extern void glp_delete_prob(double* lp);
    1.80 +  ~GLPK(){
    1.81 +    glp_delete_prob(lp);
    1.82 +    Console.WriteLine("Goodbye Nigel");
    1.83 +  }
    1.84 +
    1.85 +}
    1.86 +
    1.87 +class test{
    1.88 +  static bool isMinimum(int a, int b, int guess){
    1.89 +    Console.WriteLine("Trying {0}",guess);
    1.90 +    if (a%guess > 0) return false;
    1.91 +    if (b%guess > 0) return false;
    1.92 +    Console.WriteLine("Solution is {0}",guess);
    1.93 +    return true;
    1.94 +  }
    1.95 +
    1.96 +  public static void Main(string[] args){
    1.97 +    Console.WriteLine("a = {0}, b = {1}",args[0], args[1]);
    1.98 +    GLPK lp = new GLPK(Int32.Parse(args[0]),Int32.Parse(args[1]));
    1.99 +    int guess = (lp.a > lp.b) ? lp.b : lp.a;
   1.100 +    while (!isMinimum(lp.a,lp.b,guess)) guess = lp.betterGuess(guess);
   1.101 +  }
   1.102 +}