Recent

Author Topic: SOLVED. Help with lpSolver  (Read 3019 times)

magleft

  • Full Member
  • ***
  • Posts: 125
SOLVED. Help with lpSolver
« on: April 05, 2024, 06:44:47 pm »
Hello.
In my application I need to find the optimal solution to a combination of conditions.
To achieve this I tried to use the example for delphi - free pascal found at https://lpsolve.sourceforge.net/5.5/ .
as shown in the code. However it does not create the correct file.
Assigns the ret variable the value of the number of columns.
Even if I omit checking the ret variable, it doesn't create the correct file and the app crashes.
The file created is as follows:


*<meta creator='lp_solve v5.5'>
*<meta rows=1>
*<meta columns=15>
*<meta equalities=0>
*<meta integers=0>
*<meta origsense='MIN'>
*
NAME
ROWS
  N R0
  L R1
COLUMNS
RHS
     RHS R1 27.000000000
ENDATA

Where is the error? Any help is welcome

Code: Pascal  [Select][+][-]
  1. procedure TFLipansi.SolveProblem ;
  2. const
  3.   TbFields:Array [0..11] of string = (
  4.   'n','p','k','Mg','Cu','Zn','Mn','Fe','B','C','Ca','S' );
  5. var
  6.   Ncol:Integer ;
  7.   j, ret, i: integer;
  8.   colno: PIntArray ;
  9.   row: PFloatArray ;
  10.   lp: THandle;
  11.  begin
  12.   ret := 0;
  13.   colno := nil;
  14.   row := nil;
  15.   (* We will build the model row by row
  16.      So we start with creating a model with 0 rows and 2 columns *)
  17.   Ncol := TbSolver.RecordCount ; (* there are two variables in the model *)
  18.   lp := make_lp(0, Ncol);
  19.   if (lp = 0) then
  20.     ret := 1; (* couldn't construct a new model... *)
  21.   (* let us name our variables. Not required, but can be usefull for debugging *)
  22.   TbSolver.First ;
  23.   while not(TbSolver.EOF) do
  24.   begin
  25.        set_col_name(lp,TbSolver.RecNo,PCHAR('C'+INTTOSTR(TbSolver.RecNo)))    ;  //pchar(TbSolver.FieldByName('NAME').AsString)
  26.        TbSolver.Next ;
  27.   end;
  28.   if (ret = 0) then
  29.   begin
  30.     (* create space large enough for one row *)
  31.     GetMem(colno, SizeOf(integer) * Ncol);
  32.     GetMem(row, SizeOf(double) * Ncol);
  33.     if ((colno = nil) or (row = nil)) then
  34.       ret := 2;
  35.   end;
  36.   if (ret = 0) then
  37.   begin
  38.     set_add_rowmode(lp, true);  (* makes building the model faster if it is done rows by row *)
  39.     for i:=0 to 11 do
  40.     Begin
  41.          if  strToFloat(StringGrid1.Cells[3,i])>0 then
  42.          Begin
  43.                j:=0;
  44.                s:='';
  45.                TbSolver.First ;
  46.                while not(TbSolver.eof) do
  47.                begin
  48.                     colno^[j] := j;
  49.                     row^[j] :=TbSolver.FieldByName(TbFields[i]).AsFloat  ;
  50.                     TbSolver.Next ;
  51.                     j:=j+1;
  52.                end;
  53.                if (not add_constraintex(lp, j, @row, @Ncol , 1, strToFloat(StringGrid1.Cells[3,i]))) then  
  54.                  ret := 3;
  55.          end;
  56.     end;
  57.   end;
  58.  
  59.   if (ret = 0) then
  60.   begin
  61.      set_add_rowmode(lp, false); (* rowmode should be turned off again when done building the model *)
  62.      j:=0;
  63.      TbSolver.First ;
  64.      while not(TbSolver.eof) do
  65.      begin
  66.              colno^[j] := J;
  67.              row^[j] := TbSolver.FieldByName('Price').AsFloat;
  68.              TbSolver.Next ;
  69.              j:=j+1;
  70.         end;
  71.        (* set the objective in lp_solve *)
  72.         if (not set_obj_fnex(lp, j, @row, @Ncol )) then  ;
  73. //          ret := 4;
  74.       end;
  75.  
  76.       if (ret = 0) then
  77.       begin
  78.         (* set the object direction to maximize *)
  79.         set_minim(lp);
  80.  
  81.         (* just out of curioucity, now show the model in lp format *)
  82.       write_mps (lp, 'Solution.mps');
  83.      end;
  84.   end;
  85.  
  86.  
« Last Edit: April 06, 2024, 05:37:07 pm by magleft »
windows 10 64

TRon

  • Hero Member
  • *****
  • Posts: 4153
Re: Help with lpSolver
« Reply #1 on: April 05, 2024, 07:15:26 pm »
How are we suppose to help you ?

Code: [Select]
fpc -B test.pas
Free Pascal Compiler version 3.2.2 [2021/05/16] for x86_64
Copyright (c) 1993-2021 by Florian Klaempfl and others
Target OS: Linux for x86-64
Compiling test.pas
Compiling lpsolve.pas
test.pas(24,11) Error: Identifier not found "TbSolver"
test.pas(29,3) Error: Identifier not found "TbSolver"
test.pas(30,13) Error: Identifier not found "TbSolver"
test.pas(32,24) Error: Identifier not found "TbSolver"
test.pas(32,58) Error: Identifier not found "TbSolver"
test.pas(33,8) Error: Identifier not found "TbSolver"
test.pas(48,25) Error: Identifier not found "StringGrid1"
test.pas(51,16) Error: Identifier not found "s"
test.pas(52,16) Error: Identifier not found "TbSolver"
test.pas(53,26) Error: Identifier not found "TbSolver"
test.pas(56,31) Error: Identifier not found "TbSolver"
test.pas(57,21) Error: Identifier not found "TbSolver"
test.pas(60,76) Error: Identifier not found "StringGrid1"
test.pas(70,6) Error: Identifier not found "TbSolver"
test.pas(71,16) Error: Identifier not found "TbSolver"
test.pas(74,25) Error: Identifier not found "TbSolver"
test.pas(75,14) Error: Identifier not found "TbSolver"
test.pas(98,4) Fatal: There were 17 errors compiling module, stopping
Fatal: Compilation aborted
Error: /home/apps/fpc/3.2.2/bin/x86_64-linux/ppcx64 returned an error exitcode
And that is after wasting time getting things setup and create a test project.
Today is tomorrow's yesterday.

magleft

  • Full Member
  • ***
  • Posts: 125
Re: Help with lpSolver
« Reply #2 on: April 05, 2024, 08:53:30 pm »
I am attaching the program.
I managed to create lpx file.
How could I run the solver inside lazarus and get the results.
windows 10 64

TRon

  • Hero Member
  • *****
  • Posts: 4153
Re: Help with lpSolver
« Reply #3 on: April 05, 2024, 10:02:52 pm »
So, you have attached some random units that I have to construct into a project that I needed to create manually.

Then download yet another archive, namely the lpSolver IDE in order to obtain the missing classes. Those I can't install because they are not packages but merely some units. So that requires me to write a package for those components in order to be able to install them into Lazarus so that the project can finally be loaded.

Unless I am missing something obvious, "No, thank you ma'am".
Today is tomorrow's yesterday.

magleft

  • Full Member
  • ***
  • Posts: 125
Re: Help with lpSolver
« Reply #4 on: April 06, 2024, 05:35:59 pm »
I use str_add_constraint function and all is ok.
Thanks all for help.
windows 10 64

 

TinyPortal © 2005-2018