MPL OptiMax Samples

 

Delphi (Inprise Borland) - OptiMax called using Delphi



unit unitMPLdemo;

interface

uses
  Windows, Messages, SysUtils, Classes, Graphics, Controls, Forms, Dialogs,
  StdCtrls, MPLLib_TLB;

type
  TfrmMPLdemo = class(TForm)
    Label1: TLabel;
    editFilename: TEdit;
    Label2: TLabel;
    cbSolver: TComboBox;
    btnSolve: TButton;
    btnBrowse: TButton;
    ODFile: TOpenDialog;
    procedure btnSolveClick(Sender: TObject);
    procedure btnBrowseClick(Sender: TObject);
    function ParseAndSolveModel(model: String): Boolean;
  private
    { Private declarations }
  public
    { Public declarations }
  end;

var
  frmMPLdemo: TfrmMPLdemo;

  MPL: OptiMax;
  csModel: Model;

implementation

{$R *.DFM}

procedure TfrmMPLdemo.btnSolveClick(Sender: TObject);
begin

if cbSolver.ItemIndex < 0 then
   begin
   MessageDlg('You must choose a solver',mtError,[mbOk],0);
   end
else if not FileExists(editFilename.Text) then
   begin
   MessageDlg('Can''t find file: ' + editFilename.Text, mtError,[mbOk],0);
   end
else
   begin
   MPL := CoOptiMax.Create;
   MPL.Solvers.Add(cbSolver.Items[cbSolver.ItemIndex]);

   csModel := MPL.Models.Add('demo');

   if ParseAndSolveModel(editFilename.Text) then
      begin
      MessageDlg(csModel.Solution.ResultString
      + #13'Objective = ' + FloatToStr(csModel.Solution.ObjectValue)
      + #13'Solver used: ' + cbSolver.Items[cbSolver.ItemIndex]
      ,mtInformation,[mbOk],0);
      end;
   end;

end;

function TfrmMPLdemo.ParseAndSolveModel(model: String): Boolean;
var
   parseStatus, solveStatus: Integer;

begin
//Parse the model
parseStatus := csModel.ReadFile(model);

If parseStatus <> 0 Then
   begin
   // Errors were encountered, display message
   MessageDlg('Error parsing model' ,mtError, [mbOk], 0);
   Result := FALSE;
   Exit;
   end;

// Solve the model
solveStatus := csModel.Solve(MPL.CurrentSolver);
If solveStatus <> 0 Then
   begin
   // Errors were encountered, display message
   MessageDlg('Error solving model',mtError, [mbOk], 0);
   Result := FALSE;
   Exit;
   end;
// Success
Result := TRUE;
end;

procedure TfrmMPLdemo.btnBrowseClick(Sender: TObject);
begin
if ODFile.Execute then
   editFilename.Text := ODFile.Filename;
end;

end.