File: float-expr.cpp

package info (click to toggle)
gecode-snapshot 6.2.0%2Bgit20240207-2
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid, trixie
  • size: 35,308 kB
  • sloc: cpp: 475,516; perl: 2,077; makefile: 1,816; sh: 198
file content (568 lines) | stat: -rwxr-xr-x 15,094 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
/* -*- mode: C++; c-basic-offset: 2; indent-tabs-mode: nil -*- */
/*
 *  Main authors:
 *     Vincent Barichard <Vincent.Barichard@univ-angers.fr>
 *
 *  Copyright:
 *     Vincent Barichard, 2012
 *
 *  This file is part of Gecode, the generic constraint
 *  development environment:
 *     http://www.gecode.org
 *
 *  Permission is hereby granted, free of charge, to any person obtaining
 *  a copy of this software and associated documentation files (the
 *  "Software"), to deal in the Software without restriction, including
 *  without limitation the rights to use, copy, modify, merge, publish,
 *  distribute, sublicense, and/or sell copies of the Software, and to
 *  permit persons to whom the Software is furnished to do so, subject to
 *  the following conditions:
 *
 *  The above copyright notice and this permission notice shall be
 *  included in all copies or substantial portions of the Software.
 *
 *  THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
 *  EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
 *  MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
 *  NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
 *  LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
 *  OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
 *  WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
 *
 */

#include <gecode/minimodel.hh>

#ifdef GECODE_HAS_FLOAT_VARS

#include <gecode/float/linear.hh>

namespace Gecode {

  /// Nodes for linear expressions
  class LinFloatExpr::Node {
  public:
    /// Nodes are reference counted
    unsigned int use;
    /// Float variables in tree
    int n_float;
    /// Type of expression
    NodeType t;
    /// Subexpressions
    Node *l, *r;
    /// Sum of integer or Boolean variables, or non-linear expression
    union {
      /// Integer views and coefficients
      Float::Linear::Term* tf;
      /// Non-linear expression
      NonLinFloatExpr* ne;
    } sum;
    /// Coefficient and offset
    FloatVal a, c;
    /// Float variable (potentially)
    FloatVar x_float;
    /// Default constructor
    Node(void);
    /// Generate linear terms from expression
    GECODE_MINIMODEL_EXPORT
    void fill(Home home, Float::Linear::Term*& tf,
              FloatVal m, FloatVal& d) const;
    /// Generate linear terms for expressions
    FloatVal fill(Home home, Float::Linear::Term* tf) const;
    /// Decrement reference count and possibly free memory
    bool decrement(void);
    /// Destructor
    ~Node(void);
    /// Memory management
    static void* operator new(size_t size);
    /// Memory management
    static void  operator delete(void* p,size_t size);

  };

  /*
   * Operations for nodes
   *
   */
  forceinline
  LinFloatExpr::Node::Node(void) : use(1) {
  }

  forceinline
  LinFloatExpr::Node::~Node(void) {
    switch (t) {
    case NT_SUM:
      if (n_float > 0)
        heap.free<Float::Linear::Term>(sum.tf,n_float);
      break;
    case NT_NONLIN:
      delete sum.ne;
      break;
    default: ;
    }
  }

  forceinline void*
  LinFloatExpr::Node::operator new(size_t size) {
    return heap.ralloc(size);
  }

  forceinline void
  LinFloatExpr::Node::operator delete(void* p, size_t) {
    heap.rfree(p);
  }

  bool
  LinFloatExpr::Node::decrement(void) {
    if (--use == 0) {
      if ((l != nullptr) && l->decrement())
        delete l;
      if ((r != nullptr) && r->decrement())
        delete r;
      return true;
    }
    return false;
  }

  /*
   * Operations for float expressions
   *
   */

  LinFloatExpr::LinFloatExpr(const LinFloatExpr& e)
    : n(e.n) {
    n->use++;
  }

  NonLinFloatExpr*
  LinFloatExpr::nlfe(void) const {
    return n->t == NT_NONLIN ? n->sum.ne : nullptr;
  }

  FloatVal
  LinFloatExpr::Node::fill(Home home,
                           Float::Linear::Term* tf) const {
    FloatVal d=0;
    fill(home,tf,1.0,d);
    Float::Limits::check(d,"MiniModel::LinFloatExpr");
    return d;
  }

  void
  LinFloatExpr::post(Home home, FloatRelType frt) const {
    if (home.failed()) return;
    Region r;
    if (n->t==NT_ADD && n->l == nullptr && n->r->t==NT_NONLIN) {
      n->r->sum.ne->post(home,frt,-n->c);
    } else if (n->t==NT_SUB && n->r->t==NT_NONLIN && n->l==nullptr) {
      switch (frt) {
      case FRT_LQ: frt=FRT_GQ; break;
      case FRT_LE: frt=FRT_GR; break;
      case FRT_GQ: frt=FRT_LQ; break;
      case FRT_GR: frt=FRT_LE; break;
      default: break;
      }
      n->r->sum.ne->post(home,frt,n->c);
    } else if (frt==FRT_EQ &&
               n->t==NT_SUB && n->r->t==NT_NONLIN &&
               n->l != nullptr && n->l->t==NT_VAR
               && n->l->a==1) {
      (void) n->r->sum.ne->post(home,&n->l->x_float);
    } else if (frt==FRT_EQ &&
               n->t==NT_SUB && n->r->t==NT_VAR &&
               n->l != nullptr && n->l->t==NT_NONLIN
               && n->r->a==1) {
      (void) n->l->sum.ne->post(home,&n->r->x_float);
    } else {
      Float::Linear::Term* fts =
        r.alloc<Float::Linear::Term>(n->n_float);
      FloatVal c = n->fill(home,fts);
      Float::Linear::post(home, fts, n->n_float, frt, -c);
    }
  }

  void
  LinFloatExpr::post(Home home, FloatRelType frt, const BoolVar& b) const {
    if (home.failed()) return;
    Region r;
    if (n->t==NT_ADD && n->l==nullptr && n->r->t==NT_NONLIN) {
      n->r->sum.ne->post(home,frt,-n->c,b);
    } else if (n->t==NT_SUB && n->l==nullptr && n->r->t==NT_NONLIN) {
      switch (frt) {
      case FRT_LQ: frt=FRT_GQ; break;
      case FRT_LE: frt=FRT_GR; break;
      case FRT_GQ: frt=FRT_LQ; break;
      case FRT_GR: frt=FRT_LE; break;
      default: break;
      }
      n->r->sum.ne->post(home,frt,n->c,b);
    } else {
      Float::Linear::Term* fts =
        r.alloc<Float::Linear::Term>(n->n_float);
      FloatVal c = n->fill(home,fts);
      Float::Linear::post(home, fts, n->n_float, frt, -c, b);
    }

  }

  FloatVar
  LinFloatExpr::post(Home home) const {
    if (home.failed()) return FloatVar(home,0,0);
    Region r;
    Float::Linear::Term* fts =
      r.alloc<Float::Linear::Term>(n->n_float+1);
    FloatVal c = n->fill(home,fts);
    if ((n->n_float == 1) && (c == 0) && (fts[0].a == 1))
      return fts[0].x;
    FloatNum min, max;
    Float::Linear::estimate(&fts[0],n->n_float,c,min,max);
    FloatVar x(home, min, max);
    fts[n->n_float].x = x; fts[n->n_float].a = -1;
    Float::Linear::post(home, fts, n->n_float+1, FRT_EQ, -c);
    return x;

  }

  LinFloatExpr::LinFloatExpr(void) :
    n(new Node) {
    n->n_float = 0;
    n->t = NT_VAR;
    n->l = n->r = nullptr;
    n->a = 0;
  }

  LinFloatExpr::LinFloatExpr(const FloatVal& c) :
    n(new Node) {
    n->n_float = 0;
    n->t = NT_CONST;
    n->l = n->r = nullptr;
    n->a = 0;
    Float::Limits::check(c,"MiniModel::LinFloatExpr");
    n->c = c;
  }

  LinFloatExpr::LinFloatExpr(const FloatVar& x) :
    n(new Node) {
    n->n_float = 1;
    n->t = NT_VAR;
    n->l = n->r = nullptr;
    n->a = 1.0;
    n->x_float = x;
  }

  LinFloatExpr::LinFloatExpr(const FloatVar& x, FloatVal a) :
    n(new Node) {
    n->n_float = 1;
    n->t = NT_VAR;
    n->l = n->r = nullptr;
    n->a = a;
    n->x_float = x;
  }

  LinFloatExpr::LinFloatExpr(const FloatVarArgs& x) :
    n(new Node) {
    n->n_float = x.size();
    n->t = NT_SUM;
    n->l = n->r = nullptr;
    if (x.size() > 0) {
      n->sum.tf = heap.alloc<Float::Linear::Term>(x.size());
      for (int i=x.size(); i--; ) {
        n->sum.tf[i].x = x[i];
        n->sum.tf[i].a = 1.0;
      }
    }
  }

  LinFloatExpr::LinFloatExpr(const FloatValArgs& a, const FloatVarArgs& x) :
    n(new Node) {
    if (a.size() != x.size())
      throw Float::ArgumentSizeMismatch("MiniModel::LinFloatExpr");
    n->n_float = x.size();
    n->t = NT_SUM;
    n->l = n->r = nullptr;
    if (x.size() > 0) {
      n->sum.tf = heap.alloc<Float::Linear::Term>(x.size());
      for (int i=x.size(); i--; ) {
        n->sum.tf[i].x = x[i];
        n->sum.tf[i].a = a[i];
      }
    }
  }

  LinFloatExpr::LinFloatExpr(const LinFloatExpr& e0, NodeType t, const LinFloatExpr& e1) :
    n(new Node) {
    n->n_float = e0.n->n_float + e1.n->n_float;
    n->t = t;
    n->l = e0.n; n->l->use++;
    n->r = e1.n; n->r->use++;
  }

  LinFloatExpr::LinFloatExpr(const LinFloatExpr& e, NodeType t, const FloatVal& c) :
    n(new Node) {
    n->n_float = e.n->n_float;
    n->t = t;
    n->l = nullptr;
    n->r = e.n; n->r->use++;
    n->c = c;
  }

  LinFloatExpr::LinFloatExpr(FloatVal a, const LinFloatExpr& e) :
    n(new Node) {
    n->n_float = e.n->n_float;
    n->t = NT_MUL;
    n->l = e.n; n->l->use++;
    n->r = nullptr;
    n->a = a;
  }

  LinFloatExpr::LinFloatExpr(NonLinFloatExpr* e) :
    n(new Node) {
    n->n_float = 1;
    n->t = NT_NONLIN;
    n->l = n->r = nullptr;
    n->a = 0;
    n->sum.ne = e;
  }

  const LinFloatExpr&
  LinFloatExpr::operator =(const LinFloatExpr& e) {
    if (this != &e) {
      if (n->decrement())
        delete n;
      n = e.n; n->use++;
    }
    return *this;
  }

  LinFloatExpr::~LinFloatExpr(void) {
    if (n->decrement())
      delete n;
  }


  void
  LinFloatExpr::Node::fill(Home home,
                           Float::Linear::Term*& tf,
                           FloatVal m, FloatVal& d) const {
    switch (this->t) {
    case NT_CONST:
      Float::Limits::check(m*c,"MiniModel::LinFloatExpr");
      d += m*c;
      break;
    case NT_VAR:
      Float::Limits::check(m*a,"MiniModel::LinFloatExpr");
      tf->a=m*a; tf->x=x_float; tf++;
      break;
    case NT_NONLIN:
      tf->a=m; tf->x=sum.ne->post(home, nullptr); tf++;
      break;
    case NT_SUM:
      for (int i=n_float; i--; ) {
        Float::Limits::check(m*sum.tf[i].a,"MiniModel::LinFloatExpr");
        tf[i].x = sum.tf[i].x; tf[i].a = m*sum.tf[i].a;
      }
      tf += n_float;
      break;
    case NT_ADD:
      if (l == nullptr) {
        Float::Limits::check(m*c,"MiniModel::LinFloatExpr");
        d += m*c;
      } else {
        l->fill(home,tf,m,d);
      }
      r->fill(home,tf,m,d);
      break;
    case NT_SUB:
      if (l == nullptr) {
        Float::Limits::check(m*c,"MiniModel::LinFloatExpr");
        d += m*c;
      } else {
        l->fill(home,tf,m,d);
      }
      r->fill(home,tf,-m,d);
      break;
    case NT_MUL:
      Float::Limits::check(m*a,"MiniModel::LinFloatExpr");
      l->fill(home,tf,m*a,d);
      break;
    default:
      GECODE_NEVER;
    }
  }


  /*
   * Operators
   *
   */
  LinFloatExpr
  operator +(const FloatVal& c, const FloatVar& x) {
    if (x.assigned() && Float::Limits::valid(c+x.val()))
      return LinFloatExpr(c+x.val());
    else
      return LinFloatExpr(x,LinFloatExpr::NT_ADD,c);
  }
  LinFloatExpr
  operator +(const FloatVal& c, const LinFloatExpr& e) {
    return LinFloatExpr(e,LinFloatExpr::NT_ADD,c);
  }
  LinFloatExpr
  operator +(const FloatVar& x, const FloatVal& c) {
    if (x.assigned() && Float::Limits::valid(c+x.val()))
      return LinFloatExpr(c+x.val());
    else
      return LinFloatExpr(x,LinFloatExpr::NT_ADD,c);
  }
  LinFloatExpr
  operator +(const LinFloatExpr& e, const FloatVal& c) {
    return LinFloatExpr(e,LinFloatExpr::NT_ADD,c);
  }
  LinFloatExpr
  operator +(const FloatVar& x, const FloatVar& y) {
    if (x.assigned())
      return x.val() + y;
    else if (y.assigned())
      return x + y.val();
    else
      return LinFloatExpr(x,LinFloatExpr::NT_ADD,(const LinFloatExpr&)y);
  }
  LinFloatExpr
  operator +(const FloatVar& x, const LinFloatExpr& e) {
    if (x.assigned())
      return x.val() + e;
    else
      return LinFloatExpr(x,LinFloatExpr::NT_ADD,e);
  }
  LinFloatExpr
  operator +(const LinFloatExpr& e, const FloatVar& x) {
    if (x.assigned())
      return e + x.val();
    else
      return LinFloatExpr(e,LinFloatExpr::NT_ADD,(const LinFloatExpr&)x);
  }
  LinFloatExpr
  operator +(const LinFloatExpr& e1, const LinFloatExpr& e2) {
    return LinFloatExpr(e1,LinFloatExpr::NT_ADD,e2);
  }

  LinFloatExpr
  operator -(const FloatVal& c, const FloatVar& x) {
    if (x.assigned() && Float::Limits::valid(c-x.val()))
      return LinFloatExpr(c-x.val());
    else
      return LinFloatExpr(x,LinFloatExpr::NT_SUB,c);
  }
  LinFloatExpr
  operator -(const FloatVal& c, const LinFloatExpr& e) {
    return LinFloatExpr(e,LinFloatExpr::NT_SUB,c);
  }
  LinFloatExpr
  operator -(const FloatVar& x, const FloatVal& c) {
    if (x.assigned() && Float::Limits::valid(x.val()-c))
      return LinFloatExpr(x.val()-c);
    else
      return LinFloatExpr(x,LinFloatExpr::NT_ADD,-c);
  }
  LinFloatExpr
  operator -(const LinFloatExpr& e, const FloatVal& c) {
    return LinFloatExpr(e,LinFloatExpr::NT_ADD,-c);
  }
  LinFloatExpr
  operator -(const FloatVar& x, const FloatVar& y) {
    if (x.assigned())
      return x.val() - y;
    else if (y.assigned())
      return x - y.val();
    else
      return LinFloatExpr(x,LinFloatExpr::NT_SUB,(const LinFloatExpr&)y);
  }
  LinFloatExpr
  operator -(const FloatVar& x, const LinFloatExpr& e) {
    if (x.assigned())
      return x.val() - e;
    else
      return LinFloatExpr(x,LinFloatExpr::NT_SUB,e);
  }
  LinFloatExpr
  operator -(const LinFloatExpr& e, const FloatVar& x) {
    if (x.assigned())
      return e - x.val();
    else
      return LinFloatExpr(e,LinFloatExpr::NT_SUB,(const LinFloatExpr&)x);
  }
  LinFloatExpr
  operator -(const LinFloatExpr& e1, const LinFloatExpr& e2) {
    return LinFloatExpr(e1,LinFloatExpr::NT_SUB,e2);
  }

  LinFloatExpr
  operator -(const FloatVar& x) {
    if (x.assigned())
      return LinFloatExpr(-x.val());
    else
      return LinFloatExpr(x,LinFloatExpr::NT_SUB,0);
  }
  LinFloatExpr
  operator -(const LinFloatExpr& e) {
    return LinFloatExpr(e,LinFloatExpr::NT_SUB,0);
  }

  LinFloatExpr
  operator *(const FloatVal& a, const FloatVar& x) {
    if (a == 0)
      return LinFloatExpr(0.0);
    else if (x.assigned() &&
             Float::Limits::valid(a*x.val()))
      return LinFloatExpr(a*x.val());
    else
      return LinFloatExpr(x,a);
  }
  LinFloatExpr
  operator *(const FloatVar& x, const FloatVal& a) {
    if (a == 0)
      return LinFloatExpr(0.0);
    else if (x.assigned() &&
             Float::Limits::valid(a*x.val()))
      return LinFloatExpr(a*x.val());
    else
      return LinFloatExpr(x,a);
  }
  LinFloatExpr
  operator *(const LinFloatExpr& e, const FloatVal& a) {
    if (a == 0)
      return LinFloatExpr(0.0);
    else
      return LinFloatExpr(a,e);
  }
  LinFloatExpr
  operator *(const FloatVal& a, const LinFloatExpr& e) {
    if (a == 0)
      return LinFloatExpr(0.0);
    else
      return LinFloatExpr(a,e);
  }

  LinFloatExpr
  sum(const FloatVarArgs& x) {
    return LinFloatExpr(x);
  }

  LinFloatExpr
  sum(const FloatValArgs& a, const FloatVarArgs& x) {
    return LinFloatExpr(a,x);
  }

  FloatVar
  expr(Home home, const LinFloatExpr& e) {
    PostInfo pi(home);
    if (!home.failed())
      return e.post(home);
    FloatVar x(home,Float::Limits::min,Float::Limits::max);
    return x;
  }

}

#endif

// STATISTICS: minimodel-any