Initializing help system before first use

Partial derivatives


Type: Programming
Rating: 2 (easy-medium)
Description: Nonlinear example demonstrating the cost of numerical derivatives
File(s): xnlp_derivatives.mos


xnlp_derivatives.mos
! XNLP example demonstrating the cost of numerical derivatives
!
! This example solves the same simple problem several times using different 
! differentiation techniques and solvers
!
! This example demonstrates a particular non-linear optimization concept as related 
! to Xpress NonLinear.
! The version of the example is for Xpress 7.5.
!
!  (c) 2013-2025 Fair Isaac Corporation
!       author: Zsolt Csizmadia
model mmxnlp_nlp_duals
uses "mmxnlp", "mmsystem"; 

! Scaling parameter for problem size.
parameters
	 N = 200
end-parameters

declarations	
 R   = 1..N
 x   : array(R) of mpvar
 start, finish : real;
end-declarations

!setparam("xnlp_verbose",true)

forall(i in R) do
 x(i) >= 1 - 1/i
 x(i) <= 1 + 1/i
end-do

Objective := sum(i in R) ( x(i)^3 )

! Solve with a local solver to use derivatives
setparam("XPRS_NLPSOLVER",1)

! Default solve usign analytical derivatives, expected to take no time
write("Solve problem using analytical derivatives: ") 
start := gettime
minimize(Objective)
finish := gettime
writeln(finish-start,"s")

! Use the first order solver SLP to solve the problem
setparam("xnlp_solver",XNLP_SOLVER_XSLP)
setparam("xslp_derivatives", 0)
write("Solve problem using finite differences, 1st order solver: ") 
start := gettime
minimize(Objective)
finish := gettime
writeln(finish-start,"s")

! Now resolve using second order numerical derivatives
setparam("xnlp_solver",XNLP_SOLVER_KNITRO)
! Numerical derivatives here are enforced by the means of setting a control. 
! However, the need for numerical derivatives can arise naturally from 
! black-bock user functions
setparam("xslp_derivatives", 0)
write("Solve problem using finite differences, 2nd order solver: ") 
start := gettime
minimize(Objective)
finish := gettime
writeln(finish-start,"s")

! Revert back to the second order Knitro solver, but approximate the Hessian 
! matrix instead
setparam("xnlp_solver",XNLP_SOLVER_KNITRO)
setparam("xktr_param_hessopt", 2) ! BFGS approximation
write("Solve problem using finite differences, 2nd order solver, BFGS: ") 
start := gettime
minimize(Objective)
finish := gettime
writeln(finish-start,"s")

end-model


© 2001-2025 Fair Isaac Corporation. All rights reserved. This documentation is the property of Fair Isaac Corporation (“FICO”). Receipt or possession of this documentation does not convey rights to disclose, reproduce, make derivative works, use, or allow others to use it except solely for internal evaluation purposes to determine whether to purchase a license to the software described in this documentation, or as otherwise set forth in a written software license agreement between you and FICO (or a FICO affiliate). Use of this documentation and the software described in it must conform strictly to the foregoing permitted uses, and no other use is permitted.