/* E04NE_T1W_F C++ Header Example Program.
*
* Copyright 2024 Numerical Algorithms Group.
* Mark 30.3, 2024.
*/
#include <dco.hpp>
#include <iostream>
#include <math.h>
#include <nag.h>
#include <nagad.h>
#include <stdio.h>
#include <string>
using namespace std;
int main()
{
// Scalars
int exit_status = 0;
cout << "E04NE_T1W_F C++ Header Example Program Results\n\n";
// Create AD configuration data object
Integer ifail = 0;
nag::ad::handle_t ad_handle;
// Skip first line of data file
string mystr;
getline(cin, mystr);
// Read problem sizes
Integer m, n, nclin;
cin >> m;
cin >> n;
cin >> nclin;
Integer liwork = n;
Integer lda = m, sda = n, ldc = nclin, lb = n + nclin;
Integer lwork;
lwork = 9 * n;
if (nclin > 0)
{
lwork = lwork + 2 * n * n + 6 * nclin;
}
nagad_t1w_w_rtype *a_in = 0, *a = 0, *b_in = 0, *b = 0, *bl = 0, *bu = 0,
*c = 0;
nagad_t1w_w_rtype *cvec = 0, *clamda = 0, *work = 0, *x = 0, *x_in = 0,
*rwsav = 0;
Integer *istate = 0, *iwork = 0, *iwsav = 0, *kx;
logical *lwsav = 0;
double * db = 0, *dbl = 0, *dbu = 0;
a = new nagad_t1w_w_rtype[lda * sda];
a_in = new nagad_t1w_w_rtype[lda * sda];
b = new nagad_t1w_w_rtype[m];
b_in = new nagad_t1w_w_rtype[m];
bl = new nagad_t1w_w_rtype[lb];
bu = new nagad_t1w_w_rtype[lb];
c = new nagad_t1w_w_rtype[nclin * n];
clamda = new nagad_t1w_w_rtype[lb];
x = new nagad_t1w_w_rtype[n];
x_in = new nagad_t1w_w_rtype[n];
kx = new Integer[n];
work = new nagad_t1w_w_rtype[lwork];
rwsav = new nagad_t1w_w_rtype[475];
lwsav = new logical[120];
istate = new Integer[lb];
iwork = new Integer[liwork];
iwsav = new Integer[610];
db = new double[m];
dbl = new double[lb];
dbu = new double[lb];
// Read problem parameters and register for differentiation
double yr;
for (int i = 0; i < m; i++)
{
for (int j = 0; j < n; j++)
{
Integer k = i + j * m;
cin >> yr;
a_in[k] = yr;
}
}
for (int i = 0; i < m; i++)
{
cin >> yr;
b_in[i] = yr;
}
for (int i = 0; i < nclin; i++)
{
for (int j = 0; j < n; j++)
{
Integer k = i + j * nclin;
cin >> yr;
c[k] = yr;
}
}
for (int i = 0; i < lb; i++)
{
cin >> yr;
bl[i] = yr;
}
for (int i = 0; i < lb; i++)
{
cin >> yr;
bu[i] = yr;
}
for (int i = 0; i < n; i++)
{
cin >> yr;
x_in[i] = yr;
}
double inc = 1.0, zero = 0.0;
nagad_t1w_w_rtype obj;
Integer k;
for (int i = 0; i < m + 2 * lb; i++)
{
if (i < m)
{
k = i;
dco::derivative(b_in[k]) = inc;
}
else if (i < m + lb)
{
k = i - m;
dco::derivative(bl[k]) = inc;
}
else
{
k = i - m - lb;
dco::derivative(bu[k]) = inc;
}
// Initialize sav arrays
ifail = 0;
char cwsav[1];
obj = 0.0;
nag::ad::e04wb("E04NCA", cwsav, 1, lwsav, 120, iwsav, 610, rwsav, 475,
ifail);
// Set option via string
Integer inform;
nag::ad::e04ne("Print Level = -1", lwsav, iwsav, rwsav, inform);
// Solve the problem
for (int j = 0; j < m; j++)
{
b[j] = b_in[j];
for (int l = 0; l < n; l++)
{
a[j + l * m] = a_in[j + l * m];
}
}
for (int j = 0; j < n; j++)
{
x[j] = x_in[j];
}
for (int j = 0; j < lb; j++)
{
istate[j] = 0.0;
}
Integer iter;
ifail = -1;
nag::ad::e04nc(ad_handle, m, n, nclin, ldc, lda, c, bl, bu, cvec, istate,
kx, x, a, b, iter, obj, clamda, iwork, liwork, work, lwork,
lwsav, iwsav, rwsav, ifail);
if (i < m)
{
dco::derivative(b_in[k]) = zero;
db[k] = dco::derivative(obj);
}
else if (i < m + lb)
{
dco::derivative(bl[k]) = zero;
dbl[k] = dco::derivative(obj);
}
else
{
dco::derivative(bu[k]) = zero;
dbu[k] = dco::derivative(obj);
}
}
// Primal results
cout.setf(ios::scientific, ios::floatfield);
cout.precision(4);
cout << "\n Optimal objective function value = ";
cout.width(12);
cout << dco::value(obj) << endl;
cout << "\n Solution point: " << endl;
for (int i = 0; i < n; i++)
{
cout.width(8);
cout << i;
cout.width(12);
cout << dco::value(x[i]) << endl;
}
cout << "\n Derivatives calculated: First order tangents\n";
cout << " Computational mode : algorithmic\n\n";
cout << " Derivatives:\n\n";
cout << "\nDerivatives of obj w.r.t. B, BL and BU\n\n";
NagError fail;
INIT_FAIL(fail);
x04cac(Nag_ColMajor, Nag_GeneralMatrix, Nag_NonUnitDiag, 1, m, db, 1,
" dobj/dB", 0, &fail);
cout << endl;
x04cac(Nag_ColMajor, Nag_GeneralMatrix, Nag_NonUnitDiag, 1, lb, dbl, 1,
" dobj/dBL", 0, &fail);
cout << endl;
x04cac(Nag_ColMajor, Nag_GeneralMatrix, Nag_NonUnitDiag, 1, lb, dbu, 1,
" dobj/dBU", 0, &fail);
delete[] a;
delete[] a_in;
delete[] b;
delete[] b_in;
delete[] bl;
delete[] bu;
delete[] c;
delete[] clamda;
delete[] x;
delete[] x_in;
delete[] kx;
delete[] work;
delete[] rwsav;
delete[] lwsav;
delete[] istate;
delete[] iwork;
delete[] iwsav;
delete[] db;
delete[] dbl;
delete[] dbu;
return exit_status;
}