unit tminer2;

interface
uses
	xMatDefs, MatReal, miner2, miner;

const
	pasoini= 1;
	errx= 1e-12;
	MaxNITersrel= 100;
	MaxNIters= 10000;

  function costo( kf: integer; var X: TVectR ): NReal; far;
  procedure grad( kf: integer;  var g, x: TVectR); far;
  function fr1( kf: integer;  var X: TVectR ): NReal; far;
  procedure gr1( kf: integer;  var g, x: TVectR); far;
  function fr2( kf: integer;  var X: TVectR ): NReal; far;
  procedure gr2( kf: integer;  var g, x: TVectR); far;
  
implementation

//costo= x1 + x2
function costo( kf: integer; var X: TVectR ): NReal; far;
begin
	result:= X.e(1) + X.e(2);
end;

//grad:= [1, 1]
procedure grad( kf: integer;  var g, x: TVectR); far;
begin
	g.pon_e(1, 1);
	g.pon_e(2, 1);
end;

//fr1(x1, x2) = 1 - x1 => fr1 < 0 si x1 > 1
function fr1( kf: integer;  var X: TVectR ): NReal; far;
begin
	fr1:= 1 - X.e(1);
end;

procedure gr1( kf: integer;  var g, x: TVectR); far;
begin
	g.pon_e(1, -1);
	g.pon_e(2, 0);
end;

//fr2(x1, x2) = 2 - x2 => fr2(x1, x2) < 0 si X2 > 2
function fr2( kf: integer;  var X: TVectR ): NReal; far;
begin
	fr2:= 2 - X.e(2);
end;

procedure gr2( kf: integer;  var g, x: TVectR); far;
begin
	g.pon_e(1, 0);
	g.pon_e(2, -1);
end;

procedure DefinirProblema;
begin
	Miner2.Init( 2, 2 );
	DefFunc( 0, costo, grad );
	DefFunc( 1, fr1, gr1 );
	DefFunc( 2, fr2, gr2 );
end;

{var
	x: TVectR;
	NIters, NItersRel: longint;
	ValCosto: NReal;
	Convergio: boolean;

begin
	DefinirProblema;
	x.Create_Init( 2);
	x.pon_e( 1, 10 );
	x.pon_e( 2, 10 );

	Minimizar(
		X, pasoini, errx,
		MaxNitersrel,	NItersRel,
		MaxNIters, NIters,
		ValCosto, Convergio );

	write(  'NIr: ', NItersRel:4);
	if convergio then
		writeln( '  x1: ',X.e(1):8:-4, '  x2: ', X.e(2):8:-4,' f(x): ', ValCosto:8:-4 )
	else
		writeln( '  NO CONVERGIO ');

	x.Free;
	Miner2.done;  }
end.
