 /*-----------------------------------------------------------------------------------------
 LIBRARY: ESNOPT
 COMPONENT: estim_param_deposito
 PARTITION: param
 EXPERIMENT: ajuste
 TEMPLATE: TRANSIENT
 CREATION DATE: 16/01/2015
-----------------------------------------------------------------------------------------*/
FUNCTION INTEGER coste_y_restricciones(IN REAL esnopt_x[], IN INTEGER needF, OUT REAL esnopt_F[], IN INTEGER explicit_derivatives, IN INTEGER needG, OUT REAL esnopt_G[])


BODY


	RESET_VARIABLES ()

   -- WRITE ("costeyrestriciones2 esnoptx:  %f %f %f %f\n", esnopt_x[1], esnopt_x[2], esnopt_x[3], esnopt_x[4] )

		k          = esnopt_x[1]
		Uamb       = esnopt_x[2]
		A          = esnopt_x[3]
		R          = esnopt_x[4]	
	
		pesos[1] = 1
		pesos[2] = 100
		
	-- Calculate cost function and constraints derivatives and set to ESnopt
	--esnopt_G[1] = x[4] * (2*x[1] + x[2] + x[3])	/* dF1/dx1 */
	--esnopt_G[2] = x[1] * x[4]							/* dF1/dx2 */
	--esnopt_G[3] = x[1] * x[4] + 1						/* dF1/dx3 */
	--esnopt_G[4] = x[1] * (x[1] + x[2] + x[3])		/* dF1/dx4 */
	
	--esnopt_G[5] = 1
	--esnopt_G[6] = 1
	--esnopt_G[7] = 1
	--esnopt_G[8] = 1
	
	--esnopt_G[9] = 2*x[1]
	--esnopt_G[10] = 2*x[2]
	--esnopt_G[11] = 2*x[3]
	--esnopt_G[12] = 2*x[4]
	
	--esnopt_G[13] = -x[2]*x[3]*x[4]
	--esnopt_G[14] = -x[1]*x[3]*x[4]
	--esnopt_G[15] = -x[1]*x[2]*x[4]
	--esnopt_G[16] = -x[1]*x[2]*x[3]

	--WRITE ("costeyrestriciones2 xfinal: %f %f %f %f\n", x[1], x[2], x[3], x[4])	

  --EXEC_INIT()

 -- RESET_EVENTS()
	 SET_INIT_ACTIVE(TRUE)		
	 
	TIME = 0
	TSTOP = 90
	CINT = 0.2

	INTEG()
	
	esnopt_F[1] = F_optim[1]
	esnopt_F[2] = F_optim[2]
	esnopt_F[3] = F_optim[3]
	esnopt_F[4] = F_optim[4]		
	esnopt_F[5] = F_optim[5]
	
	 --WRITE ("costeyrestriciones2 ffinal: %f %f %f %f %f\n", esnopt_F[1], esnopt_F[2], esnopt_F[3], esnopt_F[4], esnopt_F[5])
	
	RETURN 0
END FUNCTION


EXPERIMENT ajuste ON estim_param_deposito.param
   DECLS

		INTEGER n_dec_vars = 4
		INTEGER n_constraints = 4                     --  numero restricciones 
		INTEGER n_total                               -- (numero restricciones + una funcion objetivo)

		REAL param_estim[4]      -- variables de decision, size n_dec_var	
		
	  	REAL xlow[4]			-- valor inferior de las variables de decision, size n_dec_var
   	REAL xupp[4]			-- valor superior de las variables de decision, size n_dec_var
   	
   	REAL Flow[5]			-- valor inferior de la funcion objetivo y las restricciones, size n_total 
   	REAL Fupp[5]			-- valor superior de la funcion objetivo y las restricciones, size n_total

	
	
   OBJECTS
   INIT
      -- initial values for state variables

   BOUNDS
      -- Set equations for boundaries: boundVar = f(TIME;...)
     coef = 1

   BODY
	
		-- Inicializaciones
  		k = 0.180			--  correct value  0.1556
   	Uamb = 62		--  correct value  40
		A = 0.470			--  correct value  0.45
		R = 52			--  correct value  60
		
       n_total = n_constraints + 1 
		 
	--  Formar vector de variables de decisión

		param_estim[1] = k
		param_estim[2] = Uamb
		param_estim[3] = A
		param_estim[4] = R

      --  Limites de las variables de decision
	   	  --  k
		xlow[1] = 0.077
		xupp[1] =  0.24

		-- Uamb
		xlow[2] = 20
		xupp[2] = 63

	   	  --  A
		xlow[3] = 0.22
		xupp[3] =  0.67

		-- R
		xlow[4] = 30
		xupp[4] = 90	
		

			Flow[1] = -1.0e6
			Fupp[1] =  1.0e6			
			
			Flow[2] = LiminfT
			Fupp[2] = LimsupT		
			
			Flow[3] = LiminfT
			Fupp[3] = LimsupT

			Flow[4] = Liminfh
			Fupp[4] = Limsuph
			
			Flow[5] = Liminfh
			Fupp[5] = Limsuph
	

   	--Optimization extern routine call
		setSilentMode(TRUE)
		SET_REPORT_ACTIVE("#MONITOR",FALSE)
			
		esnopt_init (n_dec_vars, n_constraints)

		esnopt_set_variables_bounds_and_initial_values (xlow, xupp, param_estim)

		/* Unneeded, because ESnopt init them to this values by default */
		//esnopt__set_cost_function_bounds_and_initial_value (-Flow[1], Fupp[1], F[1])

		esnopt_set_constraints_bounds_and_initial_values (Flow, Fupp, F_optim)
	
		esnopt_set_cost_function_and_constraints (coste_y_restricciones)

		esnopt_set_explicit_derivatives (0)

		esnopt_set_function_precision (1.0e-6)
		REL_ERROR = 1.0e-7
		-- for STEADY Calls  TOLERANCE = 1.0e-7		

		esnopt_set_iterations_limit (200) 

		-- esnopt_set_convert_to_nonlinear_constraints (1)

//      esnopt_print_data ()

		esnopt ()

		setSilentMode(FALSE)
		SET_REPORT_ACTIVE("#MONITOR",TRUE)
		
		RESET_VARIABLES ()
		
		esnopt_get_variables_values(param_estim)
		--esnopt_get_cost_function_and_constraints_values(F_optim)
		-- esnopt_get_lagrange_multipliers_values(  )

		esnopt_free ()	
		
		k = 	 param_estim[1]
		Uamb = param_estim[2]
		A = 	 param_estim[3]
		R = 	 param_estim[4]
	
	   SET_INIT_ACTIVE(TRUE)		

		
		WRITE("\nVariables de decisión:\n")
		FOR (i IN 1,n_dec_vars)
			WRITE ("%f\t", param_estim[i])
		END FOR
		WRITE ("\n\n")
	
      -- report results in file reportAll.rpt
      --REPORT_TABLE("reportAll.rpt", "*",TRUE)
      --  integrate the model 15 seconds and obtain results every 0.1 seconds
      TIME = 0
      TSTOP = 90
      CINT = 0.2
		IMETHOD = DASSL
      INTEG()

END EXPERIMENT
