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
 833
 834
 835
 836
 837
 838
 839
 840
 841
 842
 843
 844
 845
 846
 847
 848
 849
 850
 851
 852
 853
 854
 855
 856
 857
 858
 859
 860
 861
 862
 863
 864
 865
 866
 867
 868
 869
 870
 871
 872
 873
 874
 875
 876
 877
 878
 879
 880
 881
 882
 883
 884
 885
 886
 887
 888
 889
 890
 891
 892
 893
 894
 895
 896
 897
 898
 899
 900
 901
 902
 903
 904
 905
 906
 907
 908
 909
 910
 911
 912
 913
 914
 915
 916
 917
 918
 919
 920
 921
 922
 923
 924
 925
 926
 927
 928
 929
 930
 931
 932
 933
 934
 935
 936
 937
 938
 939
 940
 941
 942
 943
 944
 945
 946
 947
 948
 949
 950
 951
 952
 953
 954
 955
 956
 957
 958
 959
 960
 961
 962
 963
 964
 965
 966
 967
 968
 969
 970
 971
 972
 973
 974
 975
 976
 977
 978
 979
 980
 981
 982
 983
 984
 985
 986
 987
 988
 989
 990
 991
 992
 993
 994
 995
 996
 997
 998
 999
1000
1001
1002
1003
1004
1005
1006
1007
1008
1009
1010
1011
1012
1013
1014
1015
1016
1017
1018
1019
1020
1021
1022
1023
1024
1025
1026
1027
1028
1029
1030
1031
1032
1033
1034
1035
1036
1037
1038
1039
// IASolverBend.cpp
// Interval Assignment for Meshkit
//
#include "meshkit/IASolverBend.hpp"
#include "meshkit/IAData.hpp"
#include "meshkit/IPData.hpp"
#include "meshkit/IPBend.hpp"
#include "meshkit/IASolution.hpp"
#include "meshkit/IABendNlp.hpp"

#include <stdio.h>
#include <math.h>
#include <limits.h>

#include "IpIpoptApplication.hpp"

namespace MeshKit 
{
    
IASolverBend::IASolverBend(const IAData * ia_data_ptr, IASolution *relaxed_solution_ptr, <--- Member variable 'IASolverBend::myianlp' is not initialized in the constructor.<--- Member variable 'IASolverBend::bendData' is not initialized in the constructor.
  const bool set_silent) 
  : IASolverToolInt(ia_data_ptr, relaxed_solution_ptr, true), evenConstraintsActive(false),
    silent(set_silent), debugging(true)
//    silent(set_silent), debugging(false)
{ 
  ip_data(new IPData);
  // initialize copies relaxed solution, then we can overwrite relaxed_solution_pointer with our integer solution 
  ip_data()->initialize(relaxed_solution_ptr->x_solution); 
}

/** default destructor */
IASolverBend::~IASolverBend() 
{
  delete ip_data();
}
    
bool IASolverBend::solve_nlp() // IABendNlp *mynlp
{
  if (debugging)
  {
    printf("IASolverBend::solve_nlp() == ");        
    printf("BEND problem formulation\n");
    printf("Attempting to find a naturally-integer solution by linearizing and bending the objective function at integer values.\n");
    printf("x = sum of positive and negative deltas around the floor of the relaxed solution. Deltas are within [0,1]. Deltas are dynamically added. Objective is linear function of weighted deltas, randomized to break ties.\n");
  }
  
  // solver setup  
  Ipopt::SmartPtr<Ipopt::IpoptApplication> app = IpoptApplicationFactory();

  //  jac_d_constant
  if (evenConstraintsActive && !ia_data()->sumEvenConstraints.empty() )
  {
    app->Options()->SetStringValue("jac_d_constant", "no"); // default
  }
  else
  {
    app->Options()->SetStringValue("jac_d_constant", "yes");
  }
  // even with the even-constraints, the hessian is constant
  app->Options()->SetStringValue("hessian_constant", "yes"); // no by default

/* try leaving defaults
  // convergence parameters
  // see $IPOPTDIR/Ipopt/src/Interfaces/IpIpoptApplication.cpp
  // our real criteria are: all integer, constraints satisfied. How to test the "all_integer" part?
  app->Options()->SetNumericValue("tol", 1e-6); //"converged" if NLP error<this, default is 1e-7. Obj are scaled to be >1, so e-2 is plenty // was 1e-2
  app->Options()->SetNumericValue("max_cpu_time", sqrt( iaData->num_variables() ) ); // max time allowed in seconds
  app->Options()->SetIntegerValue("max_iter", 3 * (10 + iaData->num_variables() ) ); // max number of iterations
  // app->Options()->SetNumericValue("primal_inf_tol", 1e-2 ); 
  app->Options()->SetNumericValue("dual_inf_tol", 1e-2 ); // how close to infeasibility? // was 1e-2
  app->Options()->SetNumericValue("constr_viol_tol", 1e-2 ); // by how much can constraints be violated?
  app->Options()->SetNumericValue("compl_inf_tol", 1e-6 ); // max norm of complementary condition // was 1e-2
  
  // second criteria convergence parameters: quit if within this tol for many iterations
  // was  app->Options()->SetIntegerValue("acceptable_iter", 4 + sqrt( iaData->num_variables() ) ); //as "tol"
  app->Options()->SetNumericValue("acceptable_tol", 1e-6 ); //as "tol" was 1e-1
  
  app->Options()->SetStringValue("mu_strategy", "adaptive");
  // print level 0 to 12, Ipopt default is 5
  const int print_level = (silent) ? 0 : 1;  // simple info is 1, debug at other values
  app->Options()->SetIntegerValue("print_level", print_level);  
  // uncomment next line to write the solution to an output file
  // app->Options()->SetStringValue("output_file", "IA.out");  
  // The following overwrites the default name (ipopt.opt) of the options file
  // app->Options()->SetStringValue("option_file_name", "IA.opt");
  
  */
  const int print_level = 2;  // simple info is 1, debug at other values
  app->Options()->SetIntegerValue("print_level", print_level);  
  
  // Intialize the IpoptApplication and process the options
  Ipopt::ApplicationReturnStatus status;
  status = app->Initialize();
  if (status != Ipopt::Solve_Succeeded) {
    if (!silent)
      printf("\n\n*** Error during initialization!\n");
    return (int) status;
  }
  
  bool try_again = false; // only try again if we didn't converge and want to spend more time
  int iter = 0;
  
  // print();
  bool solution_ok = false;
  
  myianlp->even_constraints_active( evenConstraintsActive );
  
  do {
    if (debugging)
    {
      print();
      printf("%d Bend iteration\n", iter );
      // build the hessian, evaluate it and f at the current solution?
    }
      
    // Ask Ipopt to solve the problem
    status = app->OptimizeTNLP(myianlp); // the inherited IANlp
    
    // see /CoinIpopt/build/include/coin/IpReturnCodes_inc.h
    /*
    Solve_Succeeded=0,
    Solved_To_Acceptable_Level=1,
    Infeasible_Problem_Detected=2,
    Search_Direction_Becomes_Too_Small=3,
    Diverging_Iterates=4,
    User_Requested_Stop=5,
    Feasible_Point_Found=6,
    
    Maximum_Iterations_Exceeded=-1,
    Restoration_Failed=-2,
    Error_In_Step_Computation=-3,
    Maximum_CpuTime_Exceeded=-4,
    Not_Enough_Degrees_Of_Freedom=-10,
    Invalid_Problem_Definition=-11,
    Invalid_Option=-12,
    Invalid_Number_Detected=-13,
    
    Unrecoverable_Exception=-100,
    NonIpopt_Exception_Thrown=-101,
    Insufficient_Memory=-102,
    Internal_Error=-199
     */

    bool solved_full = false;
    bool solved_partial = false;
    bool solved_failure = false;
    bool problem_bad = false;
    bool problem_unbounded = false;
    (void) solved_failure;
    (void) problem_bad;
    (void) problem_unbounded;

    switch (status) {
      case Ipopt::Solve_Succeeded:
      case Ipopt::Solved_To_Acceptable_Level:
      case Ipopt::Feasible_Point_Found:
        solved_full = true;
        break;
      case Ipopt::Maximum_Iterations_Exceeded:
      case Ipopt::User_Requested_Stop:
      case Ipopt::Maximum_CpuTime_Exceeded:
      case Ipopt::Search_Direction_Becomes_Too_Small:
        solved_partial = true;
        break;
      case Ipopt::Infeasible_Problem_Detected:
      case Ipopt::Not_Enough_Degrees_Of_Freedom:
      case Ipopt::Invalid_Problem_Definition:
      case Ipopt::Invalid_Option:
      case Ipopt::Invalid_Number_Detected:
        problem_bad = true;
        break;
      case Ipopt::Diverging_Iterates:
        problem_unbounded = true;
        solved_partial = true;
        break;
      case Ipopt::Restoration_Failed:
      case Ipopt::Error_In_Step_Computation:
      case Ipopt::Unrecoverable_Exception:
      case Ipopt::NonIpopt_Exception_Thrown:
      case Ipopt::Insufficient_Memory:
      case Ipopt::Internal_Error:        
        solved_failure = true;
        break;
        
      default:
        break;
    }
  
    if (!silent)
    {
      if (solved_full) {
        printf("\n\n*** BendNlp solved!\n");
      }
      else if (solved_partial) {
        printf("\n\n*** BendNlp partial success!\n");
      }
      else {
        printf("\n\n*** BendNlp FAILED!\n");
      }
    }
    
    if (debugging)
    {
      printf("\nChecking solution.\n");
      bool integer_sat = solution_is_integer(true);
      bool even_sat = even_constraints( false, true);
      bool equal_sat = equal_constraints( false, true );
      printf("Bend solution summary, %s, equal-constraints %s, even-constraints %s.\n", 
             integer_sat ? "integer" : "NON-INTEGER",
             equal_sat ? "satisfied" : "VIOLATED", 
             even_sat ? "satisfied" : "VIOLATED" );
    }
    try_again = false; 
  
    if ( solved_full || solved_partial )
    {
      return true;
    }
    else
    {
      // todo: tweak the problem and try again
      return false;
    }
    
  } while (try_again);
  
  return solution_ok;
  
}

bool IASolverBend::round_solution()
{
  IASolution nlp_solution;
  nlp_solution.x_solution = ia_solution()->x_solution; // vector copy
  IASolverToolInt sti( ia_data(), &nlp_solution );
  sti.round_solution();
  if (debugging)
    printf("Checking rounded bend solution.\n");
  if (sti.equal_constraints(false, debugging) && sti.even_constraints(false, debugging) )
  {
  if (debugging)
    printf("Rounding worked.\n");
    
    // rounding was a valid integer solution
    ia_solution()->x_solution.swap( nlp_solution.x_solution );
    // ia_solution()->obj_value is no longer accurate, as it was for the non-rounded solution
    return true;
  }
  return false;
}

void IASolverBend::cleanup()
{
  // the solution includes the delta values.
  // remove those
  iaSolution->x_solution.resize( (unsigned) iaData->num_variables());
}


// the objective function we should use for deriving the weights
double IASolverBend::f_x_value( double I_i, double x_i ) const
{
  return myianlp->f_x_value(I_i, x_i);
}

  
double IASolverBend::fpow(double f) const
{
  return f*f*f; // f^3
  // todo, experiment with squaring, or less
}
  
 
  /* Idea: a form of IARoundingNlp with larger variable bounds, but still with a natural integer solution.
   x in [1..inf]
   xr = x optimal relaxed solution with objective function fnlp, see IANlp.xpp 
   f is piecewise linear, with corners at integer values. f slopes are unique (we hope)
   Slope definitions
   for x between xl = floor xr and xh = ceil xr, we use the difference in fnlp between xl and xh
   
   case A. xr > ceil g, g is goal I[i]
   for x above xh, 
   let h+ be fnlp ( xh+1 ) - fnlp ( xh )
   let kp be the number of intervals x is above xh 
   then slope = floor kp * h+
   for x below xl, h- = sqrt(11) / 5 h+, and slope = floor km * h-
   all this is weighted by some unique weight
   
   case B. xr in [ floor g, ceil g] 
   h+ = fnlp ( xh+1 ) - fnlp ( xh )
   h- = fnlp ( xl-1 ) - fnlp ( xl ), not used if xl == 1
   
   case C. xr < floor g
   h- = fnlp ( xl-1 ) - fnlp ( xl )
   h+ = sqrt(10)/5 h-
   If g < 2, then h- is unused, and h+ = as in case B
   
   // representation:
   h0 is weights 0..n-1
   h+ is weights n..2n-1
   h- is weights 2n..
   
   */

  
/* try parabolic constraints instead
 
void IASolverBend::add_bend_sum_weights(unsigned int i, const double factor)
{
  // scaling issue, 
  // in order to overcome the slopes of the integers, the minimum slope should be larger
  // than the largest active slope, where active means the weight of the bend delta at the current solution
  
  IPBend &bend = bendData.bendVec[i];
  // designed so xl is the "ideal"
  // const double xl = bend.xl; 
  
  const double s = even_value(i);
  const double g = s/2.;

  // pluses
  for (int j = 0; j < bend.numDeltaPlus; ++j)
  {
    double w = bendData.maxActiveVarWeight;
    // if current sum is between xl and xl + 1, underweight the first delta
    if ( (j == 0) && (g > bend.xl) )
    {
      double f = ceil(g) - g;
      assert( f >= 0.499 ); // xl was chosen to be the closest integer to s/2
      w *= f;
    }
    w *= factor * 2.2 * sqrt( 1.1 * j + 1.1 );    
    weights.push_back(w);
  }
  
  // minuses
  for (int j = 0; j < bend.numDeltaMinus; ++j)
  {
    double w = bendData.maxActiveVarWeight;
    if ( (j == 0) && (g < bend.xl) )
    {
      double f = g - floor(g);
      assert( f >= 0.499 ); // xl was chosen to be the closest integer to s/2
      w *= f;
    }
    w *= factor * 2.1 * sqrt( 1.02 * j + 1.02 );    
    weights.push_back(w);
  }
}
  
 */
  
  /*
  deltapluses
  // now modify weights based on tilts
  for (std::vector<IPBend::IPTilt>::iterator t = bend.plusTilts.begin();
       t != bend.plusTilts.end(); ++t)
  {
    double tbig = t->first;
    double tlit = tbig - 1;
    if (tlit < g)
      tlit = g;
      const double ftlit = f_x_value(g, tlit); 
      const double ftbig = f_x_value(g, tbig);
      double slope = fpow(ftbig) - fpow(ftlit); 
      slope *= t->second;
      
      if (xbig >= tbig) //if +1
      {
        assert(w>0.);
        assert(slope>0.);
        w += fabs(slope);
      }
  }
  for (std::vector<IPBend::IPTilt>::iterator t = bend.minusTilts.begin();
       t != bend.minusTilts.end(); ++t)
  {
    double tbig = t->first;
    double tlit = tbig + 1;
    if (tlit > g)
      tlit = g;
      const double ftlit = f_x_value(g, tlit); 
      const double ftbig = f_x_value(g, tbig);
      double slope = fpow(ftbig) - fpow(ftlit); // always positive
      slope *= t->second;
      
      if (xlit <= tbig)
      {
        assert(w<0.);
        assert(slope>0.);
        w -= fabs(slope);
      }
  }
   deltaminuses
  // done with tilts
   // now modify weights based on tilts
   // this is slow and could be sped up by saving prior calculations
   for (std::vector<IPBend::IPTilt>::iterator t = bend.plusTilts.begin();
   t != bend.plusTilts.end(); ++t)
   {
   double tbig = t->first;
   double tlit = tbig - 1;
   if (tlit < g)
   tlit = g;
   const double ftlit = f_x_value(g, tlit); 
   const double ftbig = f_x_value(g, tbig);
   double slope = fpow(ftbig) - fpow(ftlit); 
   slope *= t->second;
   
   if (xbig >= tbig)
   {
   assert(w<0.);
   assert(slope>0.);
   w -= fabs(slope);
   }
   }
   for (std::vector<IPBend::IPTilt>::iterator t = bend.minusTilts.begin();
   t != bend.minusTilts.end(); ++t)
   {
   double tbig = t->first;
   double tlit = tbig + 1;
   if (tlit > g)
   tlit = g;
   const double ftlit = f_x_value(g, tlit); 
   const double ftbig = f_x_value(g, tbig);
   double slope = fpow(ftbig) - fpow(ftlit); // always positive
   slope *= t->second;
   
   if (xlit <= tbig)
   {
   assert(w>0.);
   assert(slope>0.);
   w += fabs(slope);
   }
   }
   // done with tilts
   

  */
 
void IASolverBend::merge_tilts(IPBend::TiltVec &tilts)
{
  if (tilts.size() < 2)
    return;
  
  std::sort( tilts.begin(), tilts.end() ); // sorts by .first

  // copy into a new vec
  IPBend::TiltVec new_tilts;
  new_tilts.reserve(tilts.size());
  
  // multiply tilts for the same index together, increasing them faster than additively
  for (unsigned int t = 0; t < tilts.size()-1; )
  {
    unsigned int u = t + 1;
    while (u < tilts.size() && tilts[t].first == tilts[u].first)
    {
      tilts[t].second *= tilts[u].second;
      u++;
    }
    t = u;
  }
  tilts.swap(new_tilts);  
}
  
void IASolverBend::tilt_weight(const IPBend::TiltVec &tilts, const int tilt_direction, const double g, const double xlit, const double xbig, const int delta_direction, double &w)
{
  // O( num_deltas * num_tilts)
  // this could be sped up some by saving prior calculations or
  // by using the sorted order of the tilts to skip some deltas.
  for (IPBend::TiltVec::const_iterator t = tilts.begin();
       t != tilts.end(); ++t)
  {
    if (0 && debugging)
    {
      printf("  tilt (%d) %f at %d\n", tilt_direction, t->second, t->first);
    }
    double tbig = t->first;                   
    double tlit = tbig - tilt_direction;
    // if tilt_direction is negative, then tlit index is larger than tbig, 
    // but always tlit has smaller f value than tbig
    double slope = (tilt_direction > 0) ? raw_weight(g, tlit, tbig) : -raw_weight(g, tbig, tlit);
    assert(slope > 0. ); // always defined this way because of tilt_direction
    assert(t->second > 0.);
    
    slope *= t->second;
    
    // impose an absolute minimum slope of 0.1, to get out of flat regions around g
    
    if (tilt_direction > 0 && (xbig >= tbig))
    {
      if (delta_direction > 0)
      {
        assert(w>0.);
        w += fabs(slope);
      }
      else 
      {
        assert(w<0.);
        w -= fabs(slope); 
      }
    }
    else if (tilt_direction < 0 && (xlit <= tbig))
    {
      if (delta_direction > 0)
      {
        assert(w<0.);
        w -= fabs(slope);
      }          
      else {
        assert(w>0.);
        w += fabs(slope);
      }
    }
  }
}
  
double IASolverBend::raw_weight( const double g, double xlit, double xbig)
{
  assert(xlit < xbig);
  // special handling when crossing goal to avoid zero slope
  if ( xlit < g && xbig > g )
  {
    if ( g - xlit < xbig - g) 
      xlit = g;
    else
      xbig = g;
  }
  assert( xbig >= 1.);
  assert( xlit >= 1.);

  const double flit = f_x_value(g, xlit); 
  const double fbig = f_x_value(g, xbig);
  const double w = fpow(fbig) - fpow(flit);
  return w;
}
  
void IASolverBend::add_bend_weights(unsigned int i)
{
  // shorthands
  IPBend &bend = bendData.bendVec[i];
  const double xl = bend.xl;
  const double g = iaData->I[i]; // goal
  
  // current x solution for finding active weight
  const double x = iaSolution->x_solution[i]; 

  // pluses
  for (int j = 0; j < bend.numDeltaPlus; ++j)
  {
    double xlit = xl + j;
    double xbig = xlit + 1.;
    double w = raw_weight( g, xlit, xbig );
        
    if (0 && debugging)
    {
      printf("x[%u], g %f, xl %f, dplus[%u] raw_w %f\n", i, g, xl, j, w);<--- %u in format string (no. 4) requires 'unsigned int' but the argument type is 'int'.
    }
    // now modify weights based on tilts
    tilt_weight(bend.plusTilts, 1, g, xlit, xbig, 1, w);
    tilt_weight(bend.minusTilts, -1, g, xlit, xbig, 1, w);
    if (0 && debugging)
    {
      if ( bend.plusTilts.size() || bend.minusTilts.size() )
        printf("              tilted_w %f\n", w);
    }

    weights.push_back(w);

    // active? or nearly active
    if (x <= xbig + 1. && x >= xlit)
    {
      // biggest?
      if (fabs(w) > bendData.maxActiveVarWeight)
        bendData.maxActiveVarWeight = fabs(w);
    }
  }
  
  // minuses
  for (int j = 0; j < bend.numDeltaMinus; ++j)
  {
    double xbig = xl - j;   
    double xlit = xbig - 1.;
    assert(xlit >= 1.); // if this fails, then the numDeltaMinus is too large
    
    double w = - raw_weight( g, xlit, xbig );
    
    if (0 && debugging)
    {
      printf("x[%u], g %f, xl %f, dminus[%u] raw_w %f\n", i, g, xl, j, w);<--- %u in format string (no. 4) requires 'unsigned int' but the argument type is 'int'.
    }
    // now modify weights based on tilts
    tilt_weight(bend.plusTilts, 1, g, xlit, xbig, -1, w);
    tilt_weight(bend.minusTilts, -1, g, xlit, xbig, -1, w);    
    if (0 && debugging)
    {
      if ( bend.plusTilts.size() || bend.minusTilts.size() )
        printf("             tilted_w %f\n", w);
    }
    
    weights.push_back(w);

    // active? or nearly active
    if (x <= xbig && x >= xlit - 1.)
    {
      // biggest?
      if (fabs(w) > bendData.maxActiveVarWeight)
        bendData.maxActiveVarWeight = fabs(w);
    }
  }

}
  

void IASolverBend::initialize_ip_bends()
{
  // problem layout:
  // vars:
  //   x variables that are supposed to be integer
  //   sums that are supposed to be even
  //   for i = 0 .. iaData->num_variables()
  //     x[i] delta pluses
  //     x[i] delta minuses
  // 
  // constraints
  //    base-problem
  //      equal
  //      even
  //    x=deltas, x = xl(const) + deltasplus - deltasminus, <==> x - deltasplus + deltasminus = -xl
  //    s=deltas
  //
  bendData.numSumVars = (int) iaData->sumEvenConstraints.size();
  bendData.sumVarStart = iaData->num_variables();
  
  // loop over the vars, 
  // finding upper and lower rounding values
  // assign weights
  bendData.bendVec.resize( iaData->num_variables() ); // + bendData.numSumVars if we want to do those using bends rather than waves
  weights.reserve(bendData.bendVec.size()*4);
  
  bendData.maxActiveVarWeight = 0.;
  
  int d_start = iaData->num_variables();
  for (int i = 0; i < iaData->num_variables(); ++i)
  {
    IPBend &bend = bendData.bendVec[i]; // shorthand
    bend.deltaIStart = d_start;
    
    double x = ipData->relaxedSolution[i];
    // fix any out-of-bounds roundoff issue
    if ( x < 1. ) 
      x = 1.;
    double xl = bend.xl = floor(x);
    assert(xl >= 1.);
    // to do, experimenting with starting with +2, -1 
//    if ( x - floor(x) > 0.5 )
    {
      bend.numDeltaPlus = 2;
      bend.numDeltaMinus = 1; 
    }
    // just one bend, two deltas, is a bad idea, because it can lead to an unbounded objective funtion
//    else
//    {
//      bend.numDeltaPlus = 1;
//      bend.numDeltaMinus = 1;      
//    }

    /*
    // debug, test negative deltas by starting with an xl that's too high
    x += 2.;
    xl = bend.xl = floor(x);
    bend.numDeltaPlus = 1;
    bend.numDeltaMinus = 1;      
    */

    // avoid minus deltas that allow the x solution to be less than 1
    if (bend.numDeltaMinus > xl - 1. )
      bend.numDeltaMinus = xl - 1;
    
    // always have at least one deltaMinus and one deltaPlus, so that the full range of x can be represented
    assert( ( xl == 1 ) || (bend.numDeltaMinus >= 1) );
    assert( bend.numDeltaPlus >= 1 );
    
    d_start += bend.num_deltas();

    add_bend_weights( i );
  }
  
  /* handle the sum-evens using parabolic constraints intead.

   // initialize all the sum-evens to have no bends - try to enforce those after iteration 1
  // as the initial int rounding could change the sum values a lot,
  // and unlike the x's there is no intrinsic goal for them.

  for (int i = 0; i < bendData.numSumVars; ++i)
  {
    IPBend &bend = bendData.bendVec[i + bendData.sumVarStart]; // shorthand
    // get current sum-even-value
    double s = even_value(i);
    bend.xl = floor( s / 2. + 0.5 ); // closest integer to s/2
    bend.numDeltaMinus = 1;
    bend.numDeltaPlus = 1;
    bend.deltaIStart = d_start;

    add_bend_sum_weights( i, 0. );

    d_start += bend.num_deltas();
  }
   */
  
  // gather the weights in a vector 
  
  // uniquify the weights
  weights.uniquify(1., 1.e6); // 1.e4 ??
  
  
  // also do that later in a lazy fasion after we see which vars are not integer
  
  
}
  
 
bool IASolverBend::update_ip_bends()
{

  // ===============
  // check that the structure of the deltas is as expected
  // 1, 1, 1, 0.5, 0, 0, 0   or 
  // 1, 1, 1,   1, 1, 1, 3
  if (debugging)
  {
    for (int i = 0; i < iaData->num_variables(); ++i)
    {
      IPBend &bend = bendData.bendVec[i]; // shorthand
      
      bool plus_deltas = false;
      (void) plus_deltas;
      double xprior = 1.;
      for (int j = 0; j < bend.numDeltaPlus-1; ++j)
      {
        const int di = bend.deltaIStart + j;
        const double xp = iaSolution->x_solution[ di ];
        assert(xp <= xprior + 0.1 );
        xprior = xp;
        if (xprior < 0.9)
          xprior = 0.;
        if (xp > 0.1) 
          plus_deltas = true;
      }
      const int diplast = bend.deltaIStart + bend.numDeltaPlus-1;
      const double xp = iaSolution->x_solution[ diplast ];
      assert( xprior > 0.9 || xp < 1. );
      if ( xp > 0.1 )
        plus_deltas = true;
      
      bool minus_deltas = false;
      (void) minus_deltas;
      xprior = 1.;
      for (int j = 0; j < bend.numDeltaMinus-1; ++j)
      {
        const int di = bend.deltaIStart + bend.numDeltaPlus + j;
        const double xm = iaSolution->x_solution[ di ];
        assert( xm <= xprior + 0.1);
        xprior = xm;
        if (xprior < 0.9)
          xprior = 0.;
        if (xm > 0.1)
          minus_deltas = true;
      }
      const int dimlast = bend.deltaIStart + bend.numDeltaPlus + bend.numDeltaMinus-1;
      const double xm = iaSolution->x_solution[ dimlast ];
      assert( xprior > 0.9 || xm < 1. );
      if (xm > 0.1)
        minus_deltas = true;
      assert( ! (plus_deltas && minus_deltas) );
    }
  }
  // ===============
  
  
  // real algorithm

  // ====== new bends
  bool new_bend = false; // was at least one new bend added?
  bool randomized = false;
  
  int d_start = iaData->num_variables();
  for (int i = 0; i < iaData->num_variables(); ++i)
  {
    IPBend &bend = bendData.bendVec[i]; // shorthand

    // delta > 1? 
    // add more deltas
    const int num_delta_plus_old = bend.numDeltaPlus;
    const int num_delta_minus_old = bend.numDeltaMinus;
    if (bend.numDeltaPlus > 0)
    {
      const int di = bend.deltaIStart + num_delta_plus_old - 1;
      const double xp = iaSolution->x_solution[ di ]; 
      if (xp > 1.01)
      {
        double num_dp_added = floor(xp + 0.1);

        // sanity bound checks
        // at most double the number of delta pluses
        // there are two to start with, so this adds at least two
        double max_add = bend.numDeltaPlus;
        if (num_dp_added > max_add)
          num_dp_added = max_add;
        if (bend.numDeltaPlus > bend.num_deltas_max() - num_dp_added)
          num_dp_added = bend.num_deltas_max() - bend.numDeltaPlus;
        
        bend.numDeltaPlus += num_dp_added;
        
        if (bend.numDeltaPlus > num_delta_plus_old)
          new_bend = true;

        if (debugging)
        {
          const double xl = bend.xl; // relaxed solution
          const double g = iaData->I[i]; // goal
          printf("%d x (goal %f relaxed_floor %g) %f delta_plus[%d]=%f -> %g more delta pluses.\n", i, g, xl, iaSolution->x_solution[i], num_delta_plus_old-1, xp, num_dp_added );
        }
      }
    }
    if (bend.numDeltaMinus > 0)
    {
      const int di = bend.deltaIStart + num_delta_plus_old + num_delta_minus_old - 1;
      const double xm = iaSolution->x_solution[ di ]; 
      if (xm > 1.01 && bend.numDeltaMinus < bend.xl - 1)
      {
        double num_dm_added = floor(xm + 0.1);
        
        // sanity bound checks
        // at most double +1 the number of delta minuses
        // there is one to start with, so this adds at least two
        double max_add = bend.numDeltaMinus+1;
        if (num_dm_added > max_add)
          num_dm_added = max_add;
        if (bend.numDeltaMinus > bend.num_deltas_max() - num_dm_added)
          num_dm_added = bend.num_deltas_max() - bend.numDeltaMinus;

        // don't let x go below 1
        bend.numDeltaMinus += num_dm_added;
        if ( bend.numDeltaMinus > bend.xl - 1 )
        {
          bend.numDeltaMinus = bend.xl - 1;      
          num_dm_added = bend.numDeltaMinus - num_delta_minus_old;
        }

        if (bend.numDeltaMinus > num_delta_minus_old)
          new_bend = true;

        if (debugging)
        {
          const double xl = bend.xl; // relaxed solution
          const double g = iaData->I[i]; // goal
          printf("%d x (goal %f relaxed_floor %g) %f delta_minus[%d]=%f -> %g more delta minuses.\n", i, g, xl, iaSolution->x_solution[i], num_delta_minus_old-1, xm, num_dm_added );
        }
      }
    }
    bend.deltaIStart = d_start;
    
    d_start += bend.num_deltas();
  }
  
  // ======= new tilts
  // check for fractional deltas 
  // only do this if no new bends were added
  bool new_tilt = false; // was at least one new tilt added?
  if (!new_bend)
  {
    // the weight indices being current relies on there being no new bends added in the prior loop
    //IAWeights::iterator wi = weights.begin();

    int num_new_tilts = 0;
    for (int i = 0; i < iaData->num_variables(); ++i)
    {

      const double x = iaSolution->x_solution[i]; // current solution
      if (!is_integer(x))
      {
        IPBend &bend = bendData.bendVec[i]; // shorthand

        const double g = iaData->I[i]; // goal
        
        const int xf = floor(x); // int below x
        const int xc = xf+1.;  // int above x
        
        IPBend::IPTilt tilt;

        double r = ((double) rand() / RAND_MAX); // in 0,1
        tilt.second =  1.8 + r/2.;
        
        // if we are on a very flat part of the curve, straddling a goal,
        // increase the slope by a lot more
        if (fabs(x-g) < 1.1)
          tilt.second *= 3.756;
        
        // xc is farther from the goal, checks for case that the interval straddles the goal
        // tilt the positive branch
        if (xc - g > g - xf)
        {
          tilt.first = xc;
          tilt.second += x - xf;
          bend.plusTilts.push_back(tilt);
          
          merge_tilts(bend.plusTilts);
        }
        // tilt the negative branch
        else
        {
          tilt.first = xf;
          tilt.second += xc - x;
          
          bend.minusTilts.push_back(tilt);
          std::sort( bend.minusTilts.begin(), bend.minusTilts.end() ); // sorts by .first
          merge_tilts(bend.minusTilts);
        }
        ++num_new_tilts;
      }
    } // for i
    new_tilt = (num_new_tilts > 0);
    if (debugging && num_new_tilts)
    {
      printf("%d stubborn non-integer delta in solution\n", num_new_tilts);
    }
  }
  
  // if nothing changed, no need to redo weights, unless randomizing
  if ( !(new_bend || new_tilt || randomized) )
    return false;
      
  // generate new deltas and weights
  weights.clear();
  for (int i = 0; i < iaData->num_variables(); ++i)
  {
    add_bend_weights(i);
  }
  
    // if delta non-integer, apply tilts
    // randomize weight w/ excluded middle
//    for (unsigned int j = 0; j < dp_non_int.size(); ++j)
//    {
//      int k = bend.deltaIStart - iaData->num_variables() + dp_non_int[j];
//      // this might make weights tend to zero. Revisit later
//      weights[ k ] *= 1. + 0.2 * IAWeights::rand_excluded_middle();
//    }
//    for (unsigned int j = 0; j < dm_non_int.size(); ++j)
//    {
//      int k = bend.deltaIStart + bend.numDeltaPlus - iaData->num_variables() + dm_non_int[j];
//      // this might make weights tend to zero. Revisit later
//      weights[ k ] *= 1. + 0.2 * IAWeights::rand_excluded_middle();
//    }
//    
    // todo: check that weights are still increasing with increasing k
  
   weights.uniquify(1., 1e6); // 1e4?

  // return if something changed
  return new_bend || new_tilt || randomized;

}
  
bool IASolverBend::solve()
{
  if (debugging)
  {
  }

  myianlp = new IABendNlp(iaData, ipData, &bendData, iaSolution, &weights, silent);
  Ipopt::SmartPtr<Ipopt::TNLP> mynlp = myianlp; // Ipopt requires the use of smartptrs!

  // set initial ip bends from relaxed solution
  initialize_ip_bends();

  int iter = 0, bend_updates = 0;
  bool try_again = true;
  bool success = false;
  evenConstraintsActive = false;
  const int max_last_iter = 4 * ( 2 + iaData->num_variables() );
  const int max_first_iter = max_last_iter / 2;
  do 
  {
  
    // call the nlp solver
    bool solved = solve_nlp();
    
    try_again = false;
    if (solved)
    {
      
      // update bends based on solution
      bool changed = update_ip_bends();
      if (changed)
      {
        ++bend_updates;
        try_again = true;
      }
      // try again if sum-evens not already satisfied
      if (!even_constraints())
        try_again = true;
      // if nothing changed, or we've had enough initial iterations,
      // then activate the sum-even parabola constraints
      if (!changed || (iter > max_first_iter))
      {
        evenConstraintsActive = true;
        // zzyk switch over to the augmented problem, 
        // with linear constraints and standard goals for the sum-even variables.
        
      }
    }
    // avoid infinite loop if the method isn't working
    ++iter;
    if ( iter > max_last_iter )
      try_again = false;
    
  }
  while (try_again);

  //zzyk debug performance
  std::cout << iter << " bend iterations, " << bend_updates << " bend updates" << std::endl;

  cleanup();
  
  success = solution_is_integer() && all_constraints();
  if (success)
  {
    if (!silent)
      printf("IASolverBend produced integer and even solution\n");
  }
  else
  {
    return false; 
  }
  return success;
}

} // namespace MeshKit