Attached is sample.py which impliments the example from glpk.pdf in Python using Swig's array functions, running it produces:
ni...@darkstar:~/myGLPK$ python sample.py * 0: obj = 0.000000000e+00 infeas = 0.000e+00 (0) * 2: obj = 7.333333333e+02 infeas = 0.000e+00 (0) OPTIMAL SOLUTION FOUND Z = 733.333333333 ; x1 = 33.3333333333 ; x2 = 66.6666666667 ; x3 = 0.0 The original can be found in: http://geocities.yahoo.com/nigel_galloway/SWIG_GLPK.pdf dated June 1st., 2007 In summary: Python 2.5.2 (r252:60911, Mar 1 2008, 13:52:45) [GCC 4.2.3] on linux2 Type "help", "copyright", "credits" or "license" for more information. >>> import nigelzGLPK using Swig's array functions: >>> ia = nigelzGLPK.new_intArray(5) >>> ja = nigelzGLPK.new_intArray(5) >>> ar = nigelzGLPK.new_doubleArray(5) >>> nigelzGLPK.intArray_setitem(ia,1,1) >>> nigelzGLPK.intArray_setitem(ja,1,1) >>> nigelzGLPK.doubleArray_setitem(ar,1,5.0) >>> using IAJAAR: >>> p = nigelzGLPK.iajaar(5) >>> p.Add(1,1,5.0) >>> >>> now lets break it >>> >>> p.Add(2,2,23.0) >>> p.Add(3,5,-4.8) >>> p.Add(4,1,0) >>> p.Add(5,3,-10) >>> p.Add(6,1,13) Traceback (most recent call last): File "<stdin>", line 1, in <module> File "nigelzGLPK.py", line 76, in Add def Add(*args): return _nigelzGLPK.iajaar_Add(*args) nigelzGLPK.iajaarFull: <nigelzGLPK.iajaarFull; proxy of <Swig Object of type 'iajaarFull *' at 0x80f2cd0> > >>> which compares well to breaking Swig's array functions (or classes). --- On Tue, 10/2/09, Joao Pedro Pedroso <[email protected]> wrote: > From: Joao Pedro Pedroso <[email protected]> > Subject: python-glpk + Re: [Help-glpk] SWIG for Visual Studio 2008, C#, and > GLPK > To: [email protected] > Cc: [email protected] > Date: Tuesday, 10 February, 2009, 7:05 AM > Nigel, > > > Firstly, intArray and doubleArray are just my names. > If you want to > > include both it is only necassary to use different > names for the > > class arrays. > > I was tempted to say that they are just MY names, because I > said first > ;-) but then I realized that I just copied/pasted them from > the swig > documentation. So the copyright is not mine. :-( > > > Secondly array functions work with Python. > > I could not figure out how... (*) > > > Finally, to celebrate the long struggle to get these > multiple defines removed and the start of the struggle to > get 'in' and 'out' removed see: > > > http://lists.gnu.org/archive/html/help-glpk/2007-06/msg00000.html > > I am afraid I could not understand what you suggest. > > Anyway, I guess that your reluctance on accepting the lines > %array_class(int, intArray); > %array_class(double, doubleArray); > comes from some incompatibility issues in Java, and decided > to keep > python-glpk with a different swig interface for the time > being. > > The current version is 0.1.36, available at the usual > place: > http://www.dcc.fc.up.pt/~jpp/code/python-glpk/ > > Enjoy, > > Pedro > -- > (*) I hope you did not mean use it as follows; did you? > a = new_doubleArray(10) # Create an array > for i in range(0,10): > doubleArray_setitem(a,i,2*i) # Set a value > print_array(a) # Pass to C > delete_doubleArray(a) # Destroy array > > Current usage of C arrays in python-glpk is something like: > >>> a = intArray(10000000) # Array of > 10-million integers > >>> for i in xrange(10000): # Set some > values > ... a[i] = i > >>> sumitems(a,10000) # call C > function > 49995000 > >>> > > > > ----- Original Message ----- > > > From: xypron <[email protected]> > > > To: [email protected] > > > Subject: Re: [Help-glpk] SWIG for Visual Studio > 2008, C#, and GLPK > > > Date: Thu, 5 Feb 2009 12:43:11 -0800 (PST) > > > > > > > > > > > > Hello Nigel, > > > > > > thank you for your contribution. It would make > sense to integrate > > > instructions for creating c# libraries for GLPK > with swig into the GLPK > > > distribution. > > > > > > Glpk is based on the GNU licence. Hence binaries > can only be published if > > > the source is made available. > > > > > > For using glpk with C# you describe. > > > >> 5. Open Project -> Add Reference > -> Browse, then select: > > > >> glpkSWIG.dll > > > >> and nigelzGLPK.dll. > > > > > > How can the dlls be created from source? > > > > > > Best regards > > > > > > Xypron
import nigelzGLPK ia = nigelzGLPK.new_intArray(10); ja = nigelzGLPK.new_intArray(10); ar = nigelzGLPK.new_doubleArray(10); lp = nigelzGLPK.glp_create_prob(); nigelzGLPK.glp_set_prob_name(lp, "sample"); nigelzGLPK.glp_set_obj_dir(lp, nigelzGLPK.GLP_MAX); nigelzGLPK.glp_add_rows(lp, 3); nigelzGLPK.glp_set_row_name(lp, 1, "p"); nigelzGLPK.glp_set_row_bnds(lp, 1, nigelzGLPK.GLP_UP, 0.0, 100.0); nigelzGLPK.glp_set_row_name(lp, 2, "q"); nigelzGLPK.glp_set_row_bnds(lp, 2, nigelzGLPK.GLP_UP, 0.0, 600.0); nigelzGLPK.glp_set_row_name(lp, 3, "r"); nigelzGLPK.glp_set_row_bnds(lp, 3, nigelzGLPK.GLP_UP, 0.0, 300.0); nigelzGLPK.glp_add_cols(lp, 3); nigelzGLPK.glp_set_col_name(lp, 1, "x1"); nigelzGLPK.glp_set_col_bnds(lp, 1, nigelzGLPK.GLP_LO, 0.0, 0.0); nigelzGLPK.glp_set_obj_coef(lp, 1, 10.0); nigelzGLPK.glp_set_col_name(lp, 2, "x2"); nigelzGLPK.glp_set_col_bnds(lp, 2, nigelzGLPK.GLP_LO, 0.0, 0.0); nigelzGLPK.glp_set_obj_coef(lp, 2, 6.0); nigelzGLPK.glp_set_col_name(lp, 3, "x3"); nigelzGLPK.glp_set_col_bnds(lp, 3, nigelzGLPK.GLP_LO, 0.0, 0.0); nigelzGLPK.glp_set_obj_coef(lp, 3, 4.0); nigelzGLPK.intArray_setitem(ia, 1, 1); nigelzGLPK.intArray_setitem(ja, 1, 1); nigelzGLPK.doubleArray_setitem(ar, 1, 1.0); #/* a[1,1] = 1 */ nigelzGLPK.intArray_setitem(ia, 2, 1); nigelzGLPK.intArray_setitem(ja, 2, 2); nigelzGLPK.doubleArray_setitem(ar, 2, 1.0); #/* a[1,2] = 1 */ nigelzGLPK.intArray_setitem(ia, 3, 1); nigelzGLPK.intArray_setitem(ja, 3, 3); nigelzGLPK.doubleArray_setitem(ar, 3, 1.0); #/* a[1,3] = 1 */ nigelzGLPK.intArray_setitem(ia, 4, 2); nigelzGLPK.intArray_setitem(ja, 4, 1); nigelzGLPK.doubleArray_setitem(ar, 4, 10.0); #/* a[2,1] = 1 */ nigelzGLPK.intArray_setitem(ia, 5, 3); nigelzGLPK.intArray_setitem(ja, 5, 1); nigelzGLPK.doubleArray_setitem(ar, 5, 2.0); #/* a[3,1] = 2 */ nigelzGLPK.intArray_setitem(ia, 6, 2); nigelzGLPK.intArray_setitem(ja, 6, 2); nigelzGLPK.doubleArray_setitem(ar, 6, 4.0); #/* a[2,2] = 4 */ nigelzGLPK.intArray_setitem(ia, 7, 3); nigelzGLPK.intArray_setitem(ja, 7, 2); nigelzGLPK.doubleArray_setitem(ar, 7, 2.0); #/* a[3,2] = 2 */ nigelzGLPK.intArray_setitem(ia, 8, 2); nigelzGLPK.intArray_setitem(ja, 8, 3); nigelzGLPK.doubleArray_setitem(ar, 8, 5.0); #/* a[2,3] = 5 */ nigelzGLPK.intArray_setitem(ia, 9, 3); nigelzGLPK.intArray_setitem(ja, 9, 3); nigelzGLPK.doubleArray_setitem(ar, 9, 6.0); #/* a[3,3] = 6 */ nigelzGLPK.glp_load_matrix(lp, 9, ia, ja, ar); nigelzGLPK.glp_simplex(lp,None); Z = nigelzGLPK.glp_get_obj_val(lp); x1 = nigelzGLPK.glp_get_col_prim(lp, 1); x2 = nigelzGLPK.glp_get_col_prim(lp, 2); x3 = nigelzGLPK.glp_get_col_prim(lp, 3); print "\nZ = ",Z,"; x1 = ",x1,"; x2 = ",x2,"; x3 = ",x3,"\n" nigelzGLPK.delete_intArray(ia); nigelzGLPK.delete_intArray(ja); nigelzGLPK.delete_doubleArray(ar); nigelzGLPK.glp_delete_prob(lp);
_______________________________________________ Help-glpk mailing list [email protected] http://lists.gnu.org/mailman/listinfo/help-glpk
