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
// IAInterfaceStub.cpp stubbed version, without most of meshkit
// Interval Assignment Data for Meshkit
// Interface to the rest of Meshkit

#include "meshkit/IAInterface.hpp" // the one in stubs = this dir
#include "meshkit/IASolver.hpp"
#include "meshkit/IADataBuilder.hpp"
#include "meshkit/IASolution.hpp"

// stubbed #include "meshkit/meshkit/ModelEnt.hpp" // from MeshKit

#include <vector>
#include <iterator>
#include <set>
#include <cstdio>
#include <assert.h>
#include <math.h>

// zzyk
#include <iostream.h>

namespace MeshKit 
{
  
int ModelEnt::max_stub_id = 0;   // stubbed
  
const bool IAInterface::debugging = false;
  
//static IAVariable counter
unsigned int IAVariable::numVariables(0);

// stubbed  
//moab::EntityType IAInterface_types[] = {moab::MBMAXTYPE};
//const moab::EntityType* IAInterface::output_types() 
//  { return IAInterface_types; }
    
void IAInterface::setup_this()
{
  ; //nothing for now
}
    
IAVariable *IAInterface::get_variable( ModelEnt* model_entity, bool create_if_missing )
{
  // already exists?
  if (model_entity && model_entity->ia_var())
  {
    // return without setting values
    return model_entity->ia_var();
  }
  
  if (model_entity && create_if_missing) 
  {
    // default values
    double goal(1.);
    Firmness firm(DEFAULT);
    
    const Firmness me_firm( model_entity->interval_firmness() );
    const double me_edge_size( model_entity->mesh_interval_size() );
    if ( me_firm == DEFAULT || me_edge_size <= 0.) 
    {
      // ModelEnt was unset, so use default values
      ;
    }
    else
    {
      const double me_size = model_entity->measure();
      assert(me_edge_size > 0.);
      goal = me_size / me_edge_size;
      firm = model_entity->interval_firmness(); 
    }
    return create_variable( model_entity, firm, goal);  
  }
  return NULL;
}


IAVariable *IAInterface::create_variable( ModelEnt* model_entity, IAVariable::Firmness set_firm, double goal_value)
{
  IAVariable *ia_var = NULL;

  // already exists?
  if (model_entity && model_entity->ia_var())
  {
    // set new values and return
    ia_var = model_entity->ia_var();
    ia_var->set_firmness(set_firm);
    ia_var->set_goal(goal_value);
    return ia_var;
  }
      
  ia_var = new IAVariable(model_entity, set_firm, goal_value);
  assert( ia_var->uniqueId + 1 == IAVariable::numVariables );
  variables.push_back(ia_var);
  assert( IAVariable::numVariables == variables.size() );

  return ia_var;
}

IAInterface::IAVariableVec IAInterface::make_constraint_group( const MEntVector &model_entity_vec )
{
  IAVariableVec result( model_entity_vec.size() );
  for (unsigned int i = 0; i < model_entity_vec.size(); ++i)
  {
    assert(model_entity_vec[i]);
    IAVariable *v = get_variable( model_entity_vec[i], true );
    result[i] = v;
  }
  return result;
}

void IAInterface::destroy_variable( IAVariable* ia_variable )
{
  if (!ia_variable)
    return;
  // model_entity shouldn't point to ia_variable anymore
  ModelEnt *me = ia_variable->get_model_ent();
  if (me && me->ia_var() == ia_variable)
    me->ia_var(NULL);
  
  variables[ ia_variable->uniqueId ] = NULL;
  delete ia_variable;    
}


void IAInterface::destroy_data()
{
  // destroy remaining variables
  for (unsigned int i = 0; i < variables.size(); ++i)
  {
    if ( variables[i] )
    {
      assert( variables[i]->uniqueId == i);
      destroy_variable( variables[i] );
    }
  }
  variables.clear();

  // reset global variable id? Yes if this is a singleton class, which it is right now.
  IAVariable::numVariables = 0;

  // clear constraints
  sumEqualConstraints1.clear();
  sumEqualConstraints2.clear();
  sumEvenConstraints.clear();
  // ... additional types of constraints ...
}

IAInterface::~IAInterface()
{
  destroy_data();
}

void IAInterface::constrain_sum_even( const IAVariableVec &sum_even_vars )
{
  sumEvenConstraints.push_back( sum_even_vars ); // vector copy
}

void IAInterface::constrain_sum_equal( const IAVariableVec &side_one, const IAVariableVec &side_two )
{
  sumEqualConstraints1.push_back(side_one); //vector copy of side_one
  sumEqualConstraints2.push_back(side_two); //vector copy of side_two
}


// for global vector of variables
int IAInterface::variable_to_index(const IAVariable* var) const
{
  return var->uniqueId;
}

IAVariable *IAInterface::index_to_variable(int ind) const
{
  assert(ind < (int) variables.size());
  return variables[ind];
}

void IAInterface::make_set_0_to_nm1( IndexSet &index_set, const size_t k )
{
  std::pair< IndexSet::iterator, bool> prior =  index_set.insert( 0 );
  assert(prior.second);
  for ( size_t i = 0; i < k; ++i)
  {
    prior.first = index_set.insert( prior.first, (int) i );
    // assert(prior.second); // second = insert-success is not assigned by this version of insert!
  }
}

void IAInterface::make_vec_0_to_nm1( IndexVec &index_vec, const size_t k )
{
  index_vec.clear();
  index_vec.reserve(k);
  for (size_t i = 0; i < k; ++i)
    index_vec.push_back( (int) i);
}

void IAInterface::make_vec_unset( IndexVec &index_vec, const size_t k )
{
  index_vec.clear();
  index_vec.reserve(k);
  for (int i = 0; i < (int) k; ++i)
    index_vec.push_back(-1);
}

void IAInterface::set_variable_constraint_indices(
            VariableConstraintDependencies &var_con_dep, 
            const int i_start, 
            const VariableVecVec &variable_vec_vec )
{
  unsigned int j;
  int i;
  for (j = 0, i = i_start; j < variable_vec_vec.size(); ++j, ++i)
  {
    for (unsigned int k = 0; k < variable_vec_vec[j].size(); ++k )
    {
      const IAVariable *var = variable_vec_vec[j][k];
      const int v = variable_to_index( var ); 
      var_con_dep.constraintVariables[i].push_back( v );        
      assert( v < (int) var_con_dep.variableConstraints.size() );
      var_con_dep.variableConstraints[v].push_back( i );
    }
  }
}
   
void IAInterface::set_variable_constraint_indices(VariableConstraintDependencies &var_con_dep)
{
  // create vectors of the length of constraints and variables, but containing of empty vectors
  var_con_dep.constraintVariables.resize( sumEqualConstraints1.size() + sumEvenConstraints.size() );
  var_con_dep.variableConstraints.resize( variables.size() );

  assert( sumEqualConstraints1.size() == sumEqualConstraints2.size() );
  const int sum_even_start = (int) sumEqualConstraints1.size();
  //zzyk hardsets
  // build vector map of variable-to-constraints
  // build vector map of constraint-to-variables
  set_variable_constraint_indices( var_con_dep, 0, sumEqualConstraints1 );
  set_variable_constraint_indices( var_con_dep, 0, sumEqualConstraints2 );
  set_variable_constraint_indices( var_con_dep, sum_even_start, sumEvenConstraints );
}

void IAInterface::ProblemSets::find_variable_dependent_set( 
         const int variable_j, 
         const VariableConstraintDependencies &var_con_dep,
         ProblemSets &subsets )
{
  // return if we've already added this variable to the subset = removed it from this set
  IndexSet::iterator j = variableSet.find(variable_j);
  if ( j == variableSet.end() )
    return;
  
  // add the variable to the sub-problem
  subsets.variableSet.insert(*j);
  
  // remove the variable from the big problem
  variableSet.erase(j);
  
  // recursively find the dependent constraints
  IndexVec::const_iterator i;
  assert( variable_j < (int) var_con_dep.variableConstraints.size() ); 
  const IndexVec &v = var_con_dep.variableConstraints[variable_j];
  for ( i = v.begin(); i != v.end(); ++i )
    find_constraint_dependent_set( *i, var_con_dep, subsets );
}


void IAInterface::ProblemSets::find_constraint_dependent_set( 
         const int constraint_i, 
         const VariableConstraintDependencies &var_con_dep,
         ProblemSets &subsets )
{
  // return if we've already added this constraint to the subset = removed it from this set
  // todo, could speed this up using some sort of mark on the entity
  IndexSet::iterator i = constraintSet.find(constraint_i);
  if ( i == constraintSet.end() )
    return;
  
  // add the constraint to the sub-problem
  subsets.constraintSet.insert(*i);

  // remove the constraint from the global problem
  constraintSet.erase(i);

  // recursively find the dependent variables
  IndexVec::const_iterator j;
  assert( constraint_i < (int) var_con_dep.constraintVariables.size() );
  const IndexVec &v = var_con_dep.constraintVariables[constraint_i];
  for ( j = v.begin(); j != v.end(); ++j )
    find_variable_dependent_set( *j, var_con_dep, subsets );
}

void IAInterface::ProblemSets::find_dependent_set( 
  const VariableConstraintDependencies &var_con_dep,
  ProblemSets &subsets )
{
  if ( !constraintSet.empty() )
  {
    find_constraint_dependent_set( *constraintSet.begin(), var_con_dep, subsets );
  }
  else if ( !variableSet.empty() )
  {
    find_variable_dependent_set( *variableSet.begin(), var_con_dep, subsets );
  }
}
   
void IAInterface::make_global_set(ProblemSets &problem_sets)
{
  make_set_0_to_nm1( problem_sets.constraintSet, sumEqualConstraints1.size() + sumEvenConstraints.size() );
  make_set_0_to_nm1( problem_sets.variableSet, variables.size() ); 
  make_vec_unset( problem_sets.varMap, variables.size() );
  // todo hardsets, 
  // move variables from variableSet to hard_variableSet if the variable is hard-set
}
  
void IAInterface::global_to_sub_side( 
  const VariableVec &global_constraint, 
  IndexVec &global_var_map,
  IndexVec &local_constraint, int &rhs ) const
{
  for (unsigned int j = 0; j < global_constraint.size(); ++j )
  {
    IAVariable *v = global_constraint[j];
    if (v->get_firmness() == MeshKit::HARD )
      rhs -= floor( v->get_goal() + 0.5 ); // goal should be integer already for hardsets
    else
    {
      int local_index = global_var_map[ v->uniqueId ];
      assert( local_index >= 0 );
      // assert( local_index < variableSet.size() );
      local_constraint.push_back( local_index ); //variable_to_index( v, sub_variables ) );
    }
  }
}
  
void IAInterface::ProblemSets::print() const
{
  printf("ProblemSets %p:\n", this);
  if (!empty())
    printf("non-");
  printf("empty. %lu variables, %lu hardsets, %lu constraints\n", variableSet.size(), hardVariableSet.size(), constraintSet.size());
  printf("Variables: ");
  for ( IndexSet::const_iterator i = variableSet.begin(); i != variableSet.end(); ++i )
  {
    printf("%d ", *i);
  }
  printf("\n");
  printf("HardSet Variables: ");
  for ( IndexSet::const_iterator i = hardVariableSet.begin(); i != hardVariableSet.end(); ++i )
  {
    printf("%d ", *i);
  }
  printf("\n");  
  printf("Constraints: ");
  for ( IndexSet::const_iterator i = constraintSet.begin(); i != constraintSet.end(); ++i )
  {
    printf("%d ", *i);
  }
  printf("\n\n");
}

void IAInterface::VariableConstraintDependencies::print() const
{
  printf("VariableConstraintDependencies %p:\n",this);
  printf("%lu variables, %lu constraints\n", variableConstraints.size(), constraintVariables.size() );
  for (size_t i = 0; i < variableConstraints.size(); ++i)
  {
    printf("Variable %lu depends on constraints ", i);<--- %lu in format string (no. 1) requires 'unsigned long' but the argument type is 'size_t {aka unsigned long}'.
    for (size_t j = 0; j < variableConstraints[i].size(); ++j)
    {
      printf("%d ", variableConstraints[i][j]);
    }
    printf("\n");
  }
  
  for (size_t i = 0; i < constraintVariables.size(); ++i)
  {
    printf("Constraint %lu depends on variables ", i);<--- %lu in format string (no. 1) requires 'unsigned long' but the argument type is 'size_t {aka unsigned long}'.
    for (size_t j = 0; j < constraintVariables[i].size(); ++j)
    {
      printf("%d ", constraintVariables[i][j]);
    }
    printf("\n");
  }
  printf("\n");
}

  
void IAInterface::fill_problem( ProblemSets &sub_sets, 
  SubProblem *sub_problem, IndexVec &global_var_map ) const
{
  IADataBuilder data_builder( sub_problem->data ); 
  // number the variables from 0..n, by setting ia_index
  sub_problem->data->I.reserve(sub_sets.variableSet.size());
  sub_sets.varMap.reserve(sub_sets.variableSet.size());
  for (IndexSet::const_iterator i = sub_sets.variableSet.begin(); i != sub_sets.variableSet.end(); ++i)
  {
    size_t index = *i; // global index
    IAVariable *v = index_to_variable( (int) index );
    if ( v->get_firmness() != MeshKit::HARD )
    {
      // index maps
      global_var_map[index] = (int) sub_sets.varMap.size();
      sub_sets.varMap.push_back( (int) index );

      // add the goals
      data_builder.add_variable( v->goal );
    }
    else
    {
     // nothing to do, it is a constant, not a variable in the sub problem
    }
  }

  // convert constraints to IASolver format
  for (IndexSet::const_iterator i = sub_sets.constraintSet.begin(); i != sub_sets.constraintSet.end(); ++i )
  {
    unsigned int c = *i;
    if (c < sumEqualConstraints1.size()) // equal
    {
      IndexVec side_1, side_2;
      int rhs_1(0), rhs_2(0);
        
      global_to_sub_side( sumEqualConstraints1[c], global_var_map, side_1, rhs_1 );
      global_to_sub_side( sumEqualConstraints2[c], global_var_map, side_2, rhs_2 );
      
      data_builder.constrain_opposite_side_equal(side_1, side_2, rhs_1 - rhs_2 );   
      // todo check should be rhs_2 - rhs_1 ?
    }
    else // even 
    {
      c -= sumEqualConstraints1.size();
      assert( c >= 0 ); // not useful because it is unsigned<--- Unsigned variable 'c' can't be negative so it is unnecessary to test it.
      assert( c < sumEvenConstraints.size() );

      IndexVec side;
      int rhs(0);
      global_to_sub_side( sumEvenConstraints[c], global_var_map, side, rhs ); // todo check if should be -rhs?
      data_builder.constrain_sum_even(side, rhs );   
    }
  }
}

void IAInterface::subdivide_problem(SubProblemVec &subproblems)
{
  // find independent subproblems, by index of constraint and variable
  ProblemSets global_prob_set;
  make_global_set(global_prob_set);
  if (debugging)
    global_prob_set.print(); 
  VariableConstraintDependencies var_con_dep;
  set_variable_constraint_indices(var_con_dep);
  if (debugging)
    var_con_dep.print(); 
  
  while ( !global_prob_set.empty() )
  {
    // define a sub-problem by indices
    SubProblem *subproblem = new SubProblem;
    subproblems.push_back( subproblem );
    global_prob_set.find_dependent_set(var_con_dep, subproblem->problemSets);
    
    if (debugging)
    {
      printf("subproblem peeled out:\n");
      subproblem->print();
      printf("\nremaining global problem:\n");
      global_prob_set.print();
    }
    
    // Fill in solver with data from those indices    
    // convert the set into an IASolver, add data
    fill_problem( subproblem->problemSets, subproblem, global_prob_set.varMap );
    if (debugging)
      subproblem->print();
  }    
}

void IAInterface::subdivide_problem_one(std::vector<IAData*> &subproblems)
{
   // placeholder - just make one subproblem
  IAData *sub_problem = new IAData();
  subproblems.push_back(sub_problem);
  IADataBuilder data_builder( sub_problem ); // data  

  // this grabs the whole problem and converts it
  // variables numbered 0..n
  for(VariableVec::const_iterator i = variables.begin(); i != variables.end(); ++i)
  {
    IAVariable *v = *i;
    data_builder.add_variable( v->goal );
  }
  // equal constraints
  for (unsigned int i = 0; i < sumEqualConstraints1.size(); ++i)
  {
    std::vector<int> side_1, side_2;
    for (unsigned int j = 0; j < sumEqualConstraints1[i].size(); ++j)
    {
      IAVariable *v = sumEqualConstraints1[i][j];
      int index = variable_to_index(v);
      // sanity check that the indexing was correct
      assert( sub_problem->I[index] == v->goal ); 
      side_1.push_back(index);
    }
    for (unsigned int j = 0; j < sumEqualConstraints2[i].size(); ++j)
    {
      IAVariable *v = sumEqualConstraints2[i][j];
      int index = variable_to_index(v);
      side_2.push_back(index);
    }
    data_builder.constrain_opposite_side_equal(side_1, side_2, 0.);
  }
  // even constraints
  for (unsigned int i = 0; i < sumEvenConstraints.size(); ++i)
  {
    std::vector<int> side;
    for (unsigned int j = 0; j < sumEvenConstraints[i].size(); ++j)
    {
      IAVariable *v = sumEvenConstraints[i][j];
      int index = variable_to_index(v);
      side.push_back(index);
    }
    data_builder.constrain_sum_even(side, 0.);
  }

}

void IAInterface::print_problem() const
{
  // describe variables
  // describe constraints
}

void IAInterface::SubProblem::print() const
{
  printf("subproblem %d:\n", id);
  problemSets.print();
  IASolverTool solver_tool(data,solution);
  solver_tool.print();
  // describe variables
  // describe constraints
}

bool IAInterface::solve_subproblem( SubProblem *subproblem )
{
  IASolver solver( subproblem->data, subproblem->solution );
  return solver.solve();
}

void IAInterface::assign_solution( SubProblem *subproblem )
{
  // assign solution value from subproblem to model entities
  std::vector<double> &x_solution = subproblem->solution->x_solution; // shorthand
  for (unsigned int i = 0; i < x_solution.size(); ++i)
  {
    const double x = x_solution[i];
    // map index i to IAVariable, which will be different when we have non-full subproblems
    IAVariable *v = index_to_variable( subproblem->problemSets.varMap[i] );
    assert(v);
    assert( x > 0. );
    const int x_int = (int) floor( x + 0.5 );
    v->solution = x_int;
    if (v->get_model_ent())
    {
      v->get_model_ent()->mesh_intervals(x_int);
    }
  }
}

void IAInterface::execute_this()
{
  //zzyk
  clock_t t = clock();

  SubProblemVec subproblems;
  subdivide_problem(subproblems);
  for (unsigned int i = 0; i < subproblems.size(); ++i)
  {
    SubProblem *p = subproblems[i];
    if (debugging)
      printf("Solving subproblem %d\n", p->id);
    if ( solve_subproblem(p) )
      assign_solution(p);
    else
    {
      ; // some error statement
    }
    delete p;
  }
  subproblems.clear();

  //zzyk
  t = clock()-t;
  float seconds = ((float)t)/CLOCKS_PER_SEC;
  std::cout << "total IA time " << seconds << std::endl;

  
  return;
}


int IAInterface::SubProblem::max_id = 0;

IAInterface::SubProblem::SubProblem()
  : id( max_id++ )
{
  data = new IAData;
  solution = new IASolution;
}
  
IAInterface::SubProblem::~SubProblem()
{
  delete data;
  data = NULL;
  delete solution;
  solution = NULL;
}

} // namespace MeshKit