Forum > Third party

SOLVED. Help with lpSolver


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 .
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'>
  N R0
  L R1
     RHS R1 27.000000000

Where is the error? Any help is welcome

--- Code: Pascal  [+][-]window.onload = function(){var x1 = document.getElementById("main_content_section"); if (x1) { var x = document.getElementsByClassName("geshi");for (var i = 0; i < x.length; i++) { x[i].style.maxHeight='none'; x[i].style.height = Math.min(x[i].clientHeight+15,306)+'px'; x[i].style.resize = "vertical";}};} ---procedure TFLipansi.SolveProblem ;const  TbFields:Array [0..11] of string = (  'n','p','k','Mg','Cu','Zn','Mn','Fe','B','C','Ca','S' );var  Ncol:Integer ;  j, ret, i: integer;  colno: PIntArray ;  row: PFloatArray ;  lp: THandle; begin  ret := 0;  colno := nil;  row := nil;  (* We will build the model row by row     So we start with creating a model with 0 rows and 2 columns *)  Ncol := TbSolver.RecordCount ; (* there are two variables in the model *)  lp := make_lp(0, Ncol);  if (lp = 0) then    ret := 1; (* couldn't construct a new model... *)  (* let us name our variables. Not required, but can be usefull for debugging *)  TbSolver.First ;  while not(TbSolver.EOF) do  begin       set_col_name(lp,TbSolver.RecNo,PCHAR('C'+INTTOSTR(TbSolver.RecNo)))    ;  //pchar(TbSolver.FieldByName('NAME').AsString)       TbSolver.Next ;  end;  if (ret = 0) then  begin    (* create space large enough for one row *)    GetMem(colno, SizeOf(integer) * Ncol);    GetMem(row, SizeOf(double) * Ncol);    if ((colno = nil) or (row = nil)) then      ret := 2;  end;  if (ret = 0) then  begin    set_add_rowmode(lp, true);  (* makes building the model faster if it is done rows by row *)    for i:=0 to 11 do    Begin         if  strToFloat(StringGrid1.Cells[3,i])>0 then         Begin               j:=0;               s:='';               TbSolver.First ;               while not(TbSolver.eof) do               begin                    colno^[j] := j;                    row^[j] :=TbSolver.FieldByName(TbFields[i]).AsFloat  ;                    TbSolver.Next ;                    j:=j+1;               end;               if (not add_constraintex(lp, j, @row, @Ncol , 1, strToFloat(StringGrid1.Cells[3,i]))) then                   ret := 3;         end;    end;  end;   if (ret = 0) then  begin     set_add_rowmode(lp, false); (* rowmode should be turned off again when done building the model *)     j:=0;     TbSolver.First ;     while not(TbSolver.eof) do     begin             colno^[j] := J;             row^[j] := TbSolver.FieldByName('Price').AsFloat;             TbSolver.Next ;             j:=j+1;        end;       (* set the objective in lp_solve *)        if (not set_obj_fnex(lp, j, @row, @Ncol )) then  ;//          ret := 4;      end;       if (ret = 0) then      begin        (* set the object direction to maximize *)        set_minim(lp);         (* just out of curioucity, now show the model in lp format *)      write_mps (lp, 'Solution.mps');     end;  end;  

How are we suppose to help you ?

--- Code: ---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

--- End code ---
And that is after wasting time getting things setup and create a test project.

I am attaching the program.
I managed to create lpx file.
How could I run the solver inside lazarus and get the results.

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".

I use str_add_constraint function and all is ok.
Thanks all for help.


[0] Message Index

Go to full version