File: CbcLinkedUtils.cpp

package info (click to toggle)
coinor-cbc 2.9.9+repack1-1
  • links: PTS, VCS
  • area: main
  • in suites: buster
  • size: 7,848 kB
  • ctags: 5,787
  • sloc: cpp: 104,337; sh: 8,921; xml: 2,950; makefile: 520; ansic: 491; awk: 197
file content (832 lines) | stat: -rw-r--r-- 25,535 bytes parent folder | download | duplicates (3)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
// Copyright (C) 2007, International Business Machines
// Corporation and others.  All Rights Reserved.
// This code is licensed under the terms of the Eclipse Public License (EPL).

/* $Id: CbcLinkedUtils.cpp 1899 2013-04-09 18:12:08Z stefan $ */

/*! \file CbcAugmentClpSimplex.cpp
    \brief Hooks to Ampl (for CbcLinked)

    This code is a condensation of ClpAmplStuff.cpp, renamed to better
    reflect its current place in cbc.

  The code here had ties to NEW_STYLE_SOLVER code. During the 091209 Watson
  meeting, NEW_STYLE_SOLVER code was eliminated. The code here was condensed
  from ClpAmplStuff.cpp. The hook into CbcLinked is loadNonLinear. Once you
  bring that in, all the rest follows. Still, we're down about 400 lines of
  code. In the process, it appears that ClpAmplObjective.cpp was never needed
  here; the code was hooked into ClpAmplStuff.cpp.  --lh, 091209 --
*/

#include "ClpConfig.h"
#include "CbcConfig.h"
#ifdef COIN_HAS_ASL
#include "CoinPragma.hpp"
#include "CoinHelperFunctions.hpp"
#include "CoinIndexedVector.hpp"
#include "ClpFactorization.hpp"
#include "ClpSimplex.hpp"
#include "ClpAmplObjective.hpp"
#include "ClpConstraintAmpl.hpp"
#include "ClpMessage.hpp"
#include "CoinUtilsConfig.h"
#include "CoinHelperFunctions.hpp"
#include "CoinWarmStartBasis.hpp"
#include "OsiSolverInterface.hpp"
#include "Cbc_ampl.h"
#include "CoinTime.hpp"
#include "CglStored.hpp"
#include "CoinModel.hpp"
#include "CbcLinked.hpp"

extern "C" {
    //# include "getstub.h"
# include "asl_pfgh.h"
}

// stolen from IPopt with changes
typedef struct {
    double obj_sign_;
    ASL_pfgh * asl_;
    double * non_const_x_;
    int * column_; // for jacobian
    int * rowStart_;
    double * gradient_;
    double * constraintValues_;
    int nz_h_full_; // number of nonzeros in hessian
    int nerror_;
    bool objval_called_with_current_x_;
    bool conval_called_with_current_x_;
    bool jacval_called_with_current_x_;
} CbcAmplInfo;

//#############################################################################
// Constructors / Destructor / Assignment
//#############################################################################

//-------------------------------------------------------------------
// Default Constructor
//-------------------------------------------------------------------
ClpAmplObjective::ClpAmplObjective ()
        : ClpObjective()
{
    type_ = 12;
    objective_ = NULL;
    amplObjective_ = NULL;
    gradient_ = NULL;
    offset_ = 0.0;
}

bool get_constraints_linearity(void * amplInfo, int  n,
                               int * const_types)
{
    CbcAmplInfo * info = (CbcAmplInfo *) amplInfo;
    ASL_pfgh* asl = info->asl_;
    //check that n is good
    assert(n == n_con);
    // check that there are no network constraints
    assert(nlnc == 0 && lnc == 0);
    //the first nlc constraints are non linear the rest is linear
    int i;
    for (i = 0; i < nlc; i++) {
        const_types[i] = 1;
    }
    // the rest is linear
    for (i = nlc; i < n_con; i++)
        const_types[i] = 0;
    return true;
}
static bool internal_objval(CbcAmplInfo * info , double & obj_val)
{
    ASL_pfgh* asl = info->asl_;
    info->objval_called_with_current_x_ = false; // in case the call below fails

    if (n_obj == 0) {
        obj_val = 0;
        info->objval_called_with_current_x_ = true;
        return true;
    }  else {
        double  retval = objval(0, info->non_const_x_, (fint*)&info->nerror_);
        if (!info->nerror_) {
            obj_val = info->obj_sign_ * retval;
            info->objval_called_with_current_x_ = true;
            return true;
        } else {
            abort();
        }
    }

    return false;
}

static bool internal_conval(CbcAmplInfo * info , double * g)
{
    ASL_pfgh* asl = info->asl_;
    info->conval_called_with_current_x_ = false; // in case the call below fails
    assert (g);

    conval(info->non_const_x_, g, (fint*)&info->nerror_);

    if (!info->nerror_) {
        info->conval_called_with_current_x_ = true;
        return true;
    } else {
        abort();
    }
    return false;
}

static bool apply_new_x(CbcAmplInfo * info  , bool new_x, int  n, const double * x)
{
    ASL_pfgh* asl = info->asl_;

    if (new_x) {
        // update the flags so these methods are called
        // before evaluating the hessian
        info->conval_called_with_current_x_ = false;
        info->objval_called_with_current_x_ = false;
        info->jacval_called_with_current_x_ = false;

        //copy the data to the non_const_x_
        if (!info->non_const_x_) {
            info->non_const_x_ = new double [n];
        }

        for (int  i = 0; i < n; i++) {
            info->non_const_x_[i] = x[i];
        }

        // tell ampl that we have a new x
        xknowne(info->non_const_x_, (fint*)&info->nerror_);
        return info->nerror_ ? false : true;
    }

    return true;
}

static bool eval_f(void * amplInfo, int  n, const double * x, bool new_x, double & obj_value)
{
    CbcAmplInfo * info = (CbcAmplInfo *) amplInfo;
    if (!apply_new_x(info, new_x, n, x)) {
        return false;
    }

    return internal_objval(info, obj_value);
}

static bool eval_grad_f(void * amplInfo, int  n, const double * x, bool new_x, double * grad_f)
{
    CbcAmplInfo * info = (CbcAmplInfo *) amplInfo;
    ASL_pfgh* asl = info->asl_;
    if (!apply_new_x(info, new_x, n, x)) {
        return false;
    }
    int i;

    if (n_obj == 0) {
        for (i = 0; i < n; i++) {
            grad_f[i] = 0.;
        }
    } else {
        objgrd(0, info->non_const_x_, grad_f, (fint*)&info->nerror_);
        if (info->nerror_) {
            return false;
        }

        if (info->obj_sign_ == -1) {
            for (i = 0; i < n; i++) {
                grad_f[i] = -grad_f[i];
            }
        }
    }
    return true;
}

static bool eval_g(void * amplInfo, int  n, const double * x, bool new_x, double * g)
{
    CbcAmplInfo * info = (CbcAmplInfo *) amplInfo;
#ifndef NDEBUG
    ASL_pfgh* asl = info->asl_;
#endif
    // warning: n_var is a macro that assumes we have a variable called asl
    assert(n == n_var);

    if (!apply_new_x(info, new_x, n, x)) {
        return false;
    }

    return internal_conval(info, g);
}

static bool eval_jac_g(void * amplInfo, int  n, const double * x, bool new_x,
                       double * values)
{
    CbcAmplInfo * info = (CbcAmplInfo *) amplInfo;
    ASL_pfgh* asl = info->asl_;
    assert(n == n_var);

    assert (values);
    if (!apply_new_x(info, new_x, n, x)) {
        return false;
    }

    jacval(info->non_const_x_, values, (fint*)&info->nerror_);
    if (!info->nerror_) {
        return true;
    } else {
        abort();
    }
    return false;
}
//-------------------------------------------------------------------
// Useful Constructor
//-------------------------------------------------------------------
ClpAmplObjective::ClpAmplObjective (void * amplInfo)
        : ClpObjective()
{
    type_ = 12;
    activated_ = 1;
    gradient_ = NULL;
    objective_ = NULL;
    offset_ = 0.0;
    amplObjective_ = amplInfo;
}

//-------------------------------------------------------------------
// Copy constructor
//-------------------------------------------------------------------
ClpAmplObjective::ClpAmplObjective (const ClpAmplObjective & rhs)
        : ClpObjective(rhs)
{
    amplObjective_ = rhs.amplObjective_;
    offset_ = rhs.offset_;
    type_ = rhs.type_;
    if (!amplObjective_) {
        objective_ = NULL;
        gradient_ = NULL;
    } else {
        CbcAmplInfo * info = (CbcAmplInfo *) amplObjective_;
        ASL_pfgh* asl = info->asl_;

        int numberColumns = n_var;;
        if (rhs.objective_) {
            objective_ = new double [numberColumns];
            memcpy(objective_, rhs.objective_, numberColumns*sizeof(double));
        } else {
            objective_ = NULL;
        }
        if (rhs.gradient_) {
            gradient_ = new double [numberColumns];
            memcpy(gradient_, rhs.gradient_, numberColumns*sizeof(double));
        } else {
            gradient_ = NULL;
        }
    }
}


//-------------------------------------------------------------------
// Destructor
//-------------------------------------------------------------------
ClpAmplObjective::~ClpAmplObjective ()
{
    delete [] objective_;
    delete [] gradient_;
}

//----------------------------------------------------------------
// Assignment operator
//-------------------------------------------------------------------
ClpAmplObjective &
ClpAmplObjective::operator=(const ClpAmplObjective & rhs)
{
    if (this != &rhs) {
        delete [] objective_;
        delete [] gradient_;
        amplObjective_ = rhs.amplObjective_;
        offset_ = rhs.offset_;
        type_ = rhs.type_;
        if (!amplObjective_) {
            objective_ = NULL;
            gradient_ = NULL;
        } else {
            CbcAmplInfo * info = (CbcAmplInfo *) amplObjective_;
            ASL_pfgh* asl = info->asl_;

            int numberColumns = n_var;;
            if (rhs.objective_) {
                objective_ = new double [numberColumns];
                memcpy(objective_, rhs.objective_, numberColumns*sizeof(double));
            } else {
                objective_ = NULL;
            }
            if (rhs.gradient_) {
                gradient_ = new double [numberColumns];
                memcpy(gradient_, rhs.gradient_, numberColumns*sizeof(double));
            } else {
                gradient_ = NULL;
            }
        }
    }
    return *this;
}

// Returns gradient
double *
ClpAmplObjective::gradient(const ClpSimplex * model,
                           const double * solution, double & offset, bool refresh,
                           int includeLinear)
{
    if (model)
        assert (model->optimizationDirection() == 1.0);
#ifndef NDEBUG
    bool scaling = model && (model->rowScale() || model->objectiveScale() != 1.0 || model->optimizationDirection() != 1.0);
#endif
    const double * cost = NULL;
    if (model)
        cost = model->costRegion();
    if (!cost) {
        // not in solve
        cost = objective_;
#ifndef NDEBUG
        scaling = false;
#endif
    }
    assert (!scaling);
    if (!amplObjective_ || !solution || !activated_) {
        offset = offset_;
        return objective_;
    } else {
        if (refresh || !gradient_) {
            CbcAmplInfo * info = (CbcAmplInfo *) amplObjective_;
            ASL_pfgh* asl = info->asl_;
            int numberColumns = n_var;;

            if (!gradient_)
                gradient_ = new double[numberColumns];
            assert (solution);
            eval_grad_f(amplObjective_, numberColumns, solution, true, gradient_);
            // Is this best way?
            double objValue = 0.0;
            eval_f(amplObjective_, numberColumns, solution, false, objValue);
            double objValue2 = 0.0;
            for (int i = 0; i < numberColumns; i++)
                objValue2 += gradient_[i] * solution[i];
            offset_ = objValue2 - objValue; // or other way???
            if (model && model->optimizationDirection() != 1.0) {
                offset *= model->optimizationDirection();
                for (int i = 0; i < numberColumns; i++)
                    gradient_[i] *= -1.0;
            }
        }
        offset = offset_;
        return gradient_;
    }
}

//-------------------------------------------------------------------
// Clone
//-------------------------------------------------------------------
ClpObjective * ClpAmplObjective::clone() const
{
    return new ClpAmplObjective(*this);
}
// Resize objective
void
ClpAmplObjective::resize(int newNumberColumns)
{
    CbcAmplInfo * info = (CbcAmplInfo *) amplObjective_;
    ASL_pfgh* asl = info->asl_;
    int numberColumns = n_var;;
    if (numberColumns != newNumberColumns) {
        abort();
    }

}
// Delete columns in  objective
void
ClpAmplObjective::deleteSome(int numberToDelete, const int * which)
{
    if (numberToDelete)
        abort();
}
/* Returns reduced gradient.Returns an offset (to be added to current one).
 */
double
ClpAmplObjective::reducedGradient(ClpSimplex * model, double * region,
                                  bool useFeasibleCosts)
{
    int numberRows = model->numberRows();
    int numberColumns = model->numberColumns();

    //work space
    CoinIndexedVector  * workSpace = model->rowArray(0);

    CoinIndexedVector arrayVector;
    arrayVector.reserve(numberRows + 1);

    int iRow;
#ifdef CLP_DEBUG
    workSpace->checkClear();
#endif
    double * array = arrayVector.denseVector();
    int * index = arrayVector.getIndices();
    int number = 0;
    const double * costNow = gradient(model, model->solutionRegion(), offset_,
                                      true, useFeasibleCosts ? 2 : 1);
    double * cost = model->costRegion();
    const int * pivotVariable = model->pivotVariable();
    for (iRow = 0; iRow < numberRows; iRow++) {
        int iPivot = pivotVariable[iRow];
        double value;
        if (iPivot < numberColumns)
            value = costNow[iPivot];
        else if (!useFeasibleCosts)
            value = cost[iPivot];
        else
            value = 0.0;
        if (value) {
            array[iRow] = value;
            index[number++] = iRow;
        }
    }
    arrayVector.setNumElements(number);

    // Btran basic costs
    model->factorization()->updateColumnTranspose(workSpace, &arrayVector);
    double * work = workSpace->denseVector();
    ClpFillN(work, numberRows, 0.0);
    // now look at dual solution
    double * rowReducedCost = region + numberColumns;
    double * dual = rowReducedCost;
    const double * rowCost = cost + numberColumns;
    for (iRow = 0; iRow < numberRows; iRow++) {
        dual[iRow] = array[iRow];
    }
    double * dj = region;
    ClpDisjointCopyN(costNow, numberColumns, dj);

    model->transposeTimes(-1.0, dual, dj);
    for (iRow = 0; iRow < numberRows; iRow++) {
        // slack
        double value = dual[iRow];
        value += rowCost[iRow];
        rowReducedCost[iRow] = value;
    }
    return offset_;
}
/* Returns step length which gives minimum of objective for
   solution + theta * change vector up to maximum theta.

   arrays are numberColumns+numberRows
*/
double
ClpAmplObjective::stepLength(ClpSimplex * model,
                             const double * solution,
                             const double * change,
                             double maximumTheta,
                             double & currentObj,
                             double & predictedObj,
                             double & thetaObj)
{
    // Assume convex
    CbcAmplInfo * info = (CbcAmplInfo *) amplObjective_;
    ASL_pfgh* asl = info->asl_;

    int numberColumns = n_var;;
    double * tempSolution = new double [numberColumns];
    double * tempGradient = new double [numberColumns];
    // current
    eval_f(amplObjective_, numberColumns, solution, true, currentObj);
    double objA = currentObj;
    double thetaA = 0.0;
    // at maximum
    int i;
    for (i = 0; i < numberColumns; i++)
        tempSolution[i] = solution[i] + maximumTheta * change[i];
    eval_f(amplObjective_, numberColumns, tempSolution, true, thetaObj);
    double objC = thetaObj;
    double thetaC = maximumTheta;
    double objB = 0.5 * (objA + objC);
    double thetaB = 0.5 * maximumTheta;
    double gradientNorm = 1.0e6;
    while (gradientNorm > 1.0e-6 && thetaC - thetaA > 1.0e-8) {
        for (i = 0; i < numberColumns; i++)
            tempSolution[i] = solution[i] + thetaB * change[i];
        eval_grad_f(amplObjective_, numberColumns, tempSolution, true, tempGradient);
        eval_f(amplObjective_, numberColumns, tempSolution, false, objB);
        double changeObj = 0.0;
        gradientNorm = 0.0;
        for (i = 0; i < numberColumns; i++) {
            changeObj += tempGradient[i] * change[i];
            gradientNorm += tempGradient[i] * tempGradient[i];
        }
        gradientNorm = fabs(changeObj) / sqrt(gradientNorm);
        // Should try and get quadratic convergence by interpolation
        if (changeObj < 0.0) {
            // increasing is good
            thetaA = thetaB;
        } else {
            // decreasing is good
            thetaC = thetaB;
        }
        thetaB = 0.5 * (thetaA + thetaC);
    }
    delete [] tempSolution;
    delete [] tempGradient;
    predictedObj = objB;
    return thetaB;
}
// Return objective value (without any ClpModel offset) (model may be NULL)
double
ClpAmplObjective::objectiveValue(const ClpSimplex * model, const double * solution) const
{
    CbcAmplInfo * info = (CbcAmplInfo *) amplObjective_;
    ASL_pfgh* asl = info->asl_;

    int numberColumns = n_var;;
    // current
    double currentObj = 0.0;
    eval_f(amplObjective_, numberColumns, solution, true, currentObj);
    return currentObj;
}
// Scale objective
void
ClpAmplObjective::reallyScale(const double * columnScale)
{
    abort();
}
/* Given a zeroed array sets nonlinear columns to 1.
   Returns number of nonlinear columns
*/
int
ClpAmplObjective::markNonlinear(char * which)
{
    int iColumn;
    CbcAmplInfo * info = (CbcAmplInfo *) amplObjective_;
    ASL_pfgh* asl = info->asl_;
    int nonLinear = CoinMax(nlvc, nlvo);
    for (iColumn = 0; iColumn < nonLinear; iColumn++) {
        which[iColumn] = 1;
    }
    int numberNonLinearColumns = 0;
    int numberColumns = n_var;;
    for (iColumn = 0; iColumn < numberColumns; iColumn++) {
        if (which[iColumn])
            numberNonLinearColumns++;
    }
    return numberNonLinearColumns;
}
// Say we have new primal solution - so may need to recompute
void
ClpAmplObjective::newXValues()
{
    CbcAmplInfo * info = (CbcAmplInfo *) amplObjective_;
    info->conval_called_with_current_x_ = false;
    info->objval_called_with_current_x_ = false;
    info->jacval_called_with_current_x_ = false;
}

//#############################################################################
// Constructors / Destructor / Assignment
//#############################################################################
//-------------------------------------------------------------------
// Default Constructor
//-------------------------------------------------------------------
ClpConstraintAmpl::ClpConstraintAmpl ()
        : ClpConstraint()
{
    type_ = 3;
    column_ = NULL;
    coefficient_ = NULL;
    numberCoefficients_ = 0;
    amplInfo_ = NULL;
}

//-------------------------------------------------------------------
// Useful Constructor
//-------------------------------------------------------------------
ClpConstraintAmpl::ClpConstraintAmpl (int row, void * amplInfo)
        : ClpConstraint()
{
    type_ = 3;
    rowNumber_ = row;
    amplInfo_ = amplInfo;
    CbcAmplInfo * info = (CbcAmplInfo *) amplInfo_;
#ifndef NDEBUG
    ASL_pfgh* asl = info->asl_;
#endif
    // warning: nlc is a macro that assumes we have a variable called asl
    assert (rowNumber_ < nlc);
    numberCoefficients_ = info->rowStart_[rowNumber_+1] - info->rowStart_[rowNumber_];
    column_ = CoinCopyOfArray(info->column_ + info->rowStart_[rowNumber_], numberCoefficients_);
    coefficient_ = new double [numberCoefficients_];;
}

//-------------------------------------------------------------------
// Copy constructor
//-------------------------------------------------------------------
ClpConstraintAmpl::ClpConstraintAmpl (const ClpConstraintAmpl & rhs)
        : ClpConstraint(rhs)
{
    numberCoefficients_ = rhs.numberCoefficients_;
    column_ = CoinCopyOfArray(rhs.column_, numberCoefficients_);
    coefficient_ = CoinCopyOfArray(rhs.coefficient_, numberCoefficients_);
}


//-------------------------------------------------------------------
// Destructor
//-------------------------------------------------------------------
ClpConstraintAmpl::~ClpConstraintAmpl ()
{
    delete [] column_;
    delete [] coefficient_;
}

//----------------------------------------------------------------
// Assignment operator
//-------------------------------------------------------------------
ClpConstraintAmpl &
ClpConstraintAmpl::operator=(const ClpConstraintAmpl & rhs)
{
    if (this != &rhs) {
        delete [] column_;
        delete [] coefficient_;
        numberCoefficients_ = rhs.numberCoefficients_;
        column_ = CoinCopyOfArray(rhs.column_, numberCoefficients_);
        coefficient_ = CoinCopyOfArray(rhs.coefficient_, numberCoefficients_);
    }
    return *this;
}
//-------------------------------------------------------------------
// Clone
//-------------------------------------------------------------------
ClpConstraint * ClpConstraintAmpl::clone() const
{
    return new ClpConstraintAmpl(*this);
}

// Returns gradient
int
ClpConstraintAmpl::gradient(const ClpSimplex * model,
                            const double * solution,
                            double * gradient,
                            double & functionValue,
                            double & offset,
                            bool useScaling,
                            bool refresh) const
{
    CbcAmplInfo * info = (CbcAmplInfo *) amplInfo_;
    ASL_pfgh* asl = info->asl_;
    int numberColumns = n_var;;
    // If not done then do all
    if (!info->jacval_called_with_current_x_) {
        bool getStuff = eval_g(amplInfo_, numberColumns, solution, true, info->constraintValues_);
        assert (getStuff);
        getStuff = eval_jac_g(amplInfo_, numberColumns, solution, false, info->gradient_);
        assert (getStuff);
        info->jacval_called_with_current_x_ = getStuff;
    }
    if (refresh || !lastGradient_) {
        functionValue_ = info->constraintValues_[rowNumber_];
        offset_ = functionValue_; // sign??
        if (!lastGradient_)
            lastGradient_ = new double[numberColumns];
        CoinZeroN(lastGradient_, numberColumns);
        assert (!(model && model->rowScale() && useScaling));
        int i;
        int start = info->rowStart_[rowNumber_];
        assert (numberCoefficients_ == info->rowStart_[rowNumber_+1] - start);
        for (i = 0; i < numberCoefficients_; i++) {
            int iColumn = column_[i];
            double valueS = solution[iColumn];
            double valueG = info->gradient_[start+i];
            lastGradient_[iColumn] = valueG;
            offset_ -= valueS * valueG;
        }
    }
    functionValue = functionValue_;
    offset = offset_;
    memcpy(gradient, lastGradient_, numberColumns*sizeof(double));
    return 0;
}
// Resize constraint
void
ClpConstraintAmpl::resize(int newNumberColumns)
{
    abort();
}
// Delete columns in  constraint
void
ClpConstraintAmpl::deleteSome(int numberToDelete, const int * which)
{
    if (numberToDelete) {
        abort();
    }
}
// Scale constraint
void
ClpConstraintAmpl::reallyScale(const double * columnScale)
{
    abort();
}
/* Given a zeroed array sets nonlinear columns to 1.
   Returns number of nonlinear columns
*/
int
ClpConstraintAmpl::markNonlinear(char * which) const
{
    CbcAmplInfo * info = (CbcAmplInfo *) amplInfo_;
    ASL_pfgh* asl = info->asl_;
    int iColumn;
    int numberNon = 0;
    int nonLinear = CoinMax(nlvc, nlvo);
    for (iColumn = 0; iColumn < numberCoefficients_; iColumn++) {
        int jColumn = column_[iColumn];
        if (jColumn < nonLinear) {
            which[jColumn] = 1;
            numberNon++;
        }
    }
    return numberNon;
}
/* Given a zeroed array sets possible nonzero coefficients to 1.
   Returns number of nonzeros
*/
int
ClpConstraintAmpl::markNonzero(char * which) const
{
    int iColumn;
    for (iColumn = 0; iColumn < numberCoefficients_; iColumn++) {
        which[column_[iColumn]] = 1;
    }
    return numberCoefficients_;
}
// Number of coefficients
int
ClpConstraintAmpl::numberCoefficients() const
{
    return numberCoefficients_;
}
// Say we have new primal solution - so may need to recompute
void
ClpConstraintAmpl::newXValues()
{
    CbcAmplInfo * info = (CbcAmplInfo *) amplInfo_;
    info->conval_called_with_current_x_ = false;
    info->objval_called_with_current_x_ = false;
    info->jacval_called_with_current_x_ = false;
}

/* Load nonlinear part of problem from AMPL info
   Returns 0 if linear
   1 if quadratic objective
   2 if quadratic constraints
   3 if nonlinear objective
   4 if nonlinear constraints
   -1 on failure
*/
int
ClpSimplex::loadNonLinear(void * amplInfo, int & numberConstraints,
                          ClpConstraint ** & constraints)
{
    numberConstraints = 0;
    constraints = NULL;
    CbcAmplInfo * info = (CbcAmplInfo *) amplInfo;
    ASL_pfgh* asl = info->asl_;
    // For moment don't say quadratic
    int type = 0;
    if (nlo + nlc) {
        // nonlinear
        if (!nlc) {
            type = 3;
            delete objective_;
            objective_ = new ClpAmplObjective(amplInfo);
        } else {
            type = 4;
            numberConstraints = nlc;
            constraints = new ClpConstraint * [numberConstraints];
            if (nlo) {
                delete objective_;
                objective_ = new ClpAmplObjective(amplInfo);
            }
            for (int i = 0; i < numberConstraints; i++) {
                constraints[i] = new ClpConstraintAmpl(i, amplInfo);
            }
        }
    }
    return type;
}
#else
#include "ClpSimplex.hpp"
#include "ClpConstraint.hpp"
int
ClpSimplex::loadNonLinear(void * , int & ,
                          ClpConstraint ** & )
{
    abort();
    return 0;
}
#endif