forked from dynexcoin/Dynex-Neuromorphic-Chip
-
Notifications
You must be signed in to change notification settings - Fork 0
/
dynex.cc
2887 lines (2578 loc) · 142 KB
/
dynex.cc
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
// Copyright (c) 2021-2022, The DYNEX Project
//
// All rights reserved.
//
// Redistribution and use in source and binary forms, with or without modification, are
// permitted provided that the following conditions are met:
//
// 1. Redistributions of source code must retain the above copyright notice, this list of
// conditions and the following disclaimer.
//
// 2. Redistributions in binary form must reproduce the above copyright notice, this list
// of conditions and the following disclaimer in the documentation and/or other
// materials provided with the distribution.
//
// 3. Neither the name of the copyright holder nor the names of its contributors may be
// used to endorse or promote products derived from this software without specific
// prior written permission.
//
// THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND ANY
// EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF
// MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL
// THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
// SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,
// PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
// INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT,
// STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF
// THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
//
// REQUIREMENTS:
// brew install boost (macos)
//
// COMPILE:
// g++ dynex.cc -o dynex -std=c++17 -Ofast -lpthread -fpermissive
// macos: g++ dynex.cc -o dynex -std=c++17 -Ofast -I /opt/homebrew/cellar/boost/1.78.0/include -L /opt/homebrew/cellar/boost/1.78.0/lib
//
// RUN:
// ./dynex -i cnf/transformed_barthel_n_100_r_8.000_p0_0.080_instance_001.cnf
// ./dynex -i cnf/transformed_barthel_n_200_r_8.000_p0_0.080_instance_001.cnf
// ./dynex -i cnf/transformed_barthel_n_500_r_8.000_p0_0.080_instance_001.cnf
// ./dynex -i cnf/transformed_barthel_n_1000_r_8.000_p0_0.080_instance_001.cnf
// ./dynex -i cnf/transformed_barthel_n_10000_r_8.000_p0_0.080_instance_001.cnf
// ./dynex -i cnf/transformed_barthel_n_100000_r_8.000_p0_0.080_instance_001.cnf
#include <memory.h>
#include <stdio.h>
#include <stdlib.h>
#include <pthread.h>
#include <signal.h>
#include <math.h>
#include <stdbool.h>
#include <locale.h>
#include <random>
#include <iostream>
/* BOOST */ ///
#include <iostream>
#include <vector> //?
#include <boost/array.hpp>
#include <boost/numeric/odeint.hpp>
#include <boost/algorithm/clamp.hpp>
#include <boost/date_time/posix_time/posix_time.hpp>
using namespace std;
using namespace boost::numeric::odeint;
namespace pt = boost::posix_time;
//--------------------------------------------------------------------------------------------------------------
#define MAX_LITS_SYSTEM 25 //10
#define PARAM_COUNT 9 //number of parameters
#define LBH 0 //lower bound hard
#define HBH 1 //higher bound hard
#define LBS 2 //lower bound soft
#define HBS 3 //higher bound soft
#define ALPHA 0
#define BETA 1
#define GAMMA 2
#define DELTA 3
#define EPSILON 4
#define ZETA 5
#define ERR1 6
#define ERR2 7
#define INITDT 8
#define ODE_CONSTANT 1
#define ODE_CUSTOM_ADAPTIVE 2
#define ODE_RUNGEKUTTA 3
#define ODE_IMPLICIT 4
#define ODE_ONE_STEP 5
#define ODE_CONSTANT_NOBOOST 6
#define ODE_LOGFILE "log.csv"
#define TUNE_LOGFILE "tuninglog.csv"
#define PARTABLE_FILE "partable.txt"
#define FLOWVECTOR_FILE "flowvector.csv"
char LOC_FILE[256];
char SOLUTION_FILE[256];
char TUNING_FILE[256];
char ASSIGNMENT_FILE[256];
//--------------------------------------------------------------------------------------------------------------
int THREAD_COUNT = 8;
volatile int running_threads = 0;
//--------------------------------------------------------------------------------------------------------------
// PRECISION OF ODE INTEGRATION
//--------------------------------------------------------------------------------------------------------------
//typedef float TFloat;
typedef double TFloat;
//typedef long double TFloat;
#define USEFPRINTF
int digits = 15; // precision for ourputting numbers: 7 for float; 15 for double
//--------------------------------------------------------------------------------------------------------------
typedef TFloat value_type;
typedef std::vector< value_type > state_type; //we use a vector which has dynamic size
//--------------------------------------------------------------------------------------------------------------
/* SETTINGS & PARAMETERS */
bool quiet = false; //no screen output
bool flowvector_log = false; //output entire FLOWVECTOR_FILE (only thread 0)
bool load_partable = false; //load partable.txt
bool writelogfile = false; //write logfile ODE_LOGFILE
bool writelocfile = false; //write all LOC to file
bool writesolution = false; //write solution to file during solving
bool load_solution = false; //true; //load solution.txt at startup
bool load_assignment = false; //load .assignemnt.txt at startup (V,Xs,Xl,t_init)
bool massive = false; // multiple runs, each with different initial Xs, Xl
bool constand_adaptive = true; // use adaptive timestep in ODE_CONSTANT_NOBOOST
// Equations - constants:
TFloat dmm_alpha = TFloat(5.0); // growth rate for long term memory Xl
TFloat * dmm_alpha_cm; // alpha for each clause - used for heuristics
TFloat dmm_beta = TFloat(20.0); // growth rate for short term memory Xs
TFloat dmm_gamma = TFloat(TFloat(25)/TFloat(100)); // TFloat(0.25); // restriction for Cm in short term memory Xs
TFloat dmm_delta = TFloat(TFloat(5)/TFloat(100)); //TFloat(0.05); // restriction for Cm in long term memory Xl
TFloat dmm_epsilon = TFloat(TFloat(1)/TFloat(10)); //TFloat(0.1); // remove spurious solution X,s,m = 0
TFloat dmm_zeta = TFloat(TFloat(1)/TFloat(10)); //TFloat(0.1); // reduction factor of rigidity G (learning rate) 10^-3; for ratio>=6: 10^-1 (0.1)
int seed = 0; // random seed value (for initial assignment); defaults to 0,1,-1,rand,rand,rand...
int xl_max = 10000; //10^4 M (x count clauses will be applied automatically - ODE should NEVER reach this value)
// ODE settings:
int INTEGRATION_MODE = ODE_CONSTANT_NOBOOST; // ODE_CONSTANT; // ODE_RUNGEKUTTA;
bool forcebounds = true; // update vector x: apply boundaries after every step
// Constant integration params:
TFloat timeout = TFloat(INT_MAX); //max simulated time; stops at reaching it
double walltime_timeout = INT_MAX; //max walltime time; stops at reaching it
// Runge-Kutta Adaptive params:
TFloat rk_errorrate_1 = TFloat(TFloat(52)/TFloat(100)); //TFloat(0.52);
TFloat rk_errorrate_2 = TFloat(TFloat(10)/TFloat(100)); //TFloat(0.10);
TFloat init_dt = TFloat(TFloat(15)/TFloat(100)); // 0.15 TFloat(TFloat(78125)/TFloat(10000000)); //TFloat(0.0078125);; //2^-7
TFloat maxsteps = TFloat(INT_MAX);
// massive parallel runs:
int massive_global;
int massive_energy;
// tuning options:
bool tune = false;
double switchfraction = 0.0001;
int tune_mode = 0; // 0 = always from initial; 1 = from v_best, ...
int tune_mode_params = 2; // 0 = alpha..zeta, 1=ODE params, 2=all params
int N_ITERATIONS = INT_MAX;
// Additional ODE heuristics:
bool heuristics = false;
bool apply_heuristics = false; // apply heuristics at the current timestep?
//-------------------------------------------------------------------------------------------------------------------
/* VARIABLES */
bool debug = false;
void INThandler(int); // ctrl-c handler
int * x_rem; // to calculate which vars flipped; not necessary for production
int * cls; //stores the clauses (MAX_LITS_SYSTEM columns)
int * unit_clause_vars; //vars which are in unit clauses (one literal)
bool unit_clauses = false; //unit clauses existing?
int * occurrence; //occurences of each litint * occurenceCounter; //counter for allocation
int * numOccurrenceT; //number of occurence of a lit
int * clauseSizes; //number of literals of a clause
int * occurenceCounter;
int maxNumOccurences = 0; //max #occurence in clauses of a var
int n; //number of variables
int m; //number of clauses
int solved; //is formula solved? UNSAT = 0; SAT = 1;
int global; //current lowest loc over all threads
TFloat global_energy; //current lowest energy over all threads
TFloat * v_best; //x assignment of global local minima for each thread
char input_filename[256];
/* thread specific vars */
int * loc_thread; //current local minima of thread (currently)
TFloat * energy_thread; //current energy of all clauses (sum of Cm)
TFloat * energy_thread_min; //minimum energy of thread
int * global_thread; //global local minima of thread
int * global_all_runs_thread; //global local minimum of thread over all runs
TFloat * time_thread; //simtime of thread (better loc)
TFloat * time_thread_actual; //simtime of thread (currently)
double * walltime_thread; //walltime of thread (better loc)
TFloat * initial_assignments; //initial assignment for each thread
TFloat * thread_params; //parameters for each thread
double * t_begin_thread; //starting time of thread
double * t_end_thread; //current time of thread
int global_best_thread; //thread# which has currently best global;
double * partable; //if partable.txt is provided, here are the bounds
double * defaults; //if partable.txt is provided, here are the default values (max 128 threads)
bool partable_loaded = false;
int * stepcounter;
TFloat * t_init; // starting time of integration
/* vars for parameters in CNF: */
char c_key[256];
char c_plain[256];
char c_cipher[256];
int *c_key_bin;
int *c_plain_bin;
int *c_cipher_bin;
bool c_key_loaded = false;
bool c_plain_loaded = false;
bool c_cipher_loaded = false;
int c_key_best = 0;
int c_key_best_all_runs = 0;
int key_best_thread = 0;
int * c_key_thread;
struct node {
int id; //thread-id
int *model; //current assignment
int *temporal; //temp assignment for oracle (not used in production)
int *optimal; //best assignment and solution afterwards
};
double t_begin;
double t_end;
//-------------------------------------------------------------------------------------------------------------------
/* color definitions */
#define TEXT_DEFAULT "\033[0m"
#define TEXT_YELLOW "\033[1;33m"
#define TEXT_GREEN "\033[1;32m"
#define TEXT_RED "\033[1;31m"
#define TEXT_BLUE "\033[1;34m"
#define TEXT_CYAN "\033[1;36m"
#define TEXT_WHITE "\033[1;37m"
#define TEXT_SILVER "\033[1;315m"
// test against solution:
int solmax = 0;
//-------------------------------------------------------------------------------------------------------------------
// p-Bit functions
//-------------------------------------------------------------------------------------------------------------------
// generate a random floating point number from min to max
double randfrom(double min, double max)
{
double range = (max - min);
double div = RAND_MAX / range;
return min + (rand() / div);
}
//-------------------------------------------------------------------------------------------------------------------
//main p-bit function:
//input: weight (f.e. -4, -2, 0, 0.5, etc.)
//output: -1 / +1
std::mt19937 pbit_generator(seed);
std::uniform_real_distribution<double> rand_pbit(-1.0, 1.0);
TFloat pbit(double p_in) {
TFloat p_out = tanh(p_in)+rand_pbit(pbit_generator);
TFloat res = 0;
if (p_out>0) res = 1;
if (p_out<0) res = -1;
return res; //p_out
}
//-------------------------------------------------------------------------------------------------------------------
// -- / p-Bit
//-------------------------------------------------------------------------------------------------------------------
//-------------------------------------------------------------------------------------------------------------------
/* threading */
pthread_mutex_t mut = PTHREAD_MUTEX_INITIALIZER;
//----------------------------------------------------------------------------------------------------------------
// handler for CTRL-C
void INThandler(int sig)
{
signal(sig, SIG_IGN);
solved=-1; //this stops all the threads
signal(SIGINT, INThandler);
}
// ---------------------------------------------------------------------------------------------------------------
// manual ode function (NO BOOST) :
// ---------------------------------------------------------------------------------------------------------------
std::vector<TFloat> dmm_dostep(int m_nodeid, std::vector<TFloat> _x, std::vector<TFloat> dxdt, TFloat h) {
//update V:
for (int i=0; i<n; i++) {
_x[i] = _x[i] + h * dxdt[i];
if (unit_clause_vars[i+1]!=0) _x[i] = unit_clause_vars[i+1]; //TODO: assign +1 or -1
if (_x[i]<-1.0) _x[i]=-1.0;
if (_x[i]>1.0) _x[i]=1.0;
}
//update XS:
for (int i=n; i<n+m; i++) {
_x[i] = _x[i] + h * dxdt[i];
if (_x[i]<0.0) _x[i]=0.0;
if (_x[i]>1.0) _x[i]=1.0;
}
//update Xl:
for (int i=n+m; i<n+m*2; i++) {
_x[i] = _x[i] + h * dxdt[i];
if (_x[i]<1.0) _x[i]=1.0;
if (_x[i]>xl_max) _x[i]=xl_max;
}
// increase stepcounter for this thread:
stepcounter[m_nodeid]++;
return _x;
}
// ---------------------------------------------------------------------------------------------------------------
// DXDT CALCULATION (WITHOUT BOOST):
// ---------------------------------------------------------------------------------------------------------------
std::vector<TFloat> dmm_generate_dxdt(int m_nodeid, std::vector<TFloat> x , TFloat t )
{
/* timer and stats */
t_end_thread[m_nodeid] = pt::microsec_clock::local_time().time_of_day().total_milliseconds();//clock();
double time_spent = (double)(t_end_thread[m_nodeid] - t_begin_thread[m_nodeid])/1000;// / CLOCKS_PER_SEC;
time_thread_actual[m_nodeid] = t;
// stop integration if solved or walltime timeout exceeded:
if (solved==1 || time_spent>walltime_timeout || (massive && time_spent < 0) || (tune && time_spent<0)) {
pthread_mutex_lock(&mut);
running_threads--;
pthread_mutex_unlock(&mut);
pthread_exit(&mut);
}
energy_thread[m_nodeid] = 0.0;
std::vector<TFloat> dxdt(n+m*2); // dxdt vector
std::vector< std::vector <TFloat >> dxdt_v(n); // vector for storing all voltages
/* main ODE routine */
if (solved!=1) {
int loc;
// Loop ---------------------------------------------------------------------------------------------------------------
loc = m;
// screen info variables:
TFloat C_rem, R_rem, G_rem;
int cnt_xl_pos = 0;
int cnt_xs_pos = 0;
// loop through each clause:
for (int clause = 0; clause < m; clause++) {
// number of literals in this clause:
int ksat = clauseSizes[clause];
// Xl & Xs:
TFloat Xs = x[clause+n]; if (Xs<0.0) Xs = TFloat(0.0); if (Xs>1.0) Xs = TFloat(1.0); //Xs bounds
TFloat Xl = x[clause+n+m]; if (Xl<1.0) Xl = TFloat(1.0); if (Xl>xl_max) Xl = TFloat(xl_max); //Xl bounds
TFloat C = TFloat(0.0);
TFloat Ri, Rj, Rk, Gi, Gj, Gk;
// 3-sat:
if (ksat==3) {
int Qi = (cls[clause*MAX_LITS_SYSTEM+0]>0)? 1:-1; // +1 if literal is >0, otherwise -1
int Qj = (cls[clause*MAX_LITS_SYSTEM+1]>0)? 1:-1; // +1 if literal is >0, otherwise -1
int Qk = (cls[clause*MAX_LITS_SYSTEM+2]>0)? 1:-1; // +1 if literal is >0, otherwise -1
int liti = abs(cls[clause*MAX_LITS_SYSTEM+0]);
int litj = abs(cls[clause*MAX_LITS_SYSTEM+1]);
int litk = abs(cls[clause*MAX_LITS_SYSTEM+2]);
TFloat Vi = x[liti-1]; if (Vi<-1.0) Vi = -1.0; if (Vi>1.0) Vi = 1.0; //V bounds
TFloat Vj = x[litj-1]; if (Vj<-1.0) Vj = -1.0; if (Vj>1.0) Vj = 1.0; //V bounds
TFloat Vk = x[litk-1]; if (Vk<-1.0) Vk = -1.0; if (Vk>1.0) Vk = 1.0; //V bounds
TFloat i = TFloat(1.0)-TFloat(Qi)*Vi;
TFloat j = TFloat(1.0)-TFloat(Qj)*Vj;
TFloat k = TFloat(1.0)-TFloat(Qk)*Vk;
C = TFloat(fmin(i, fmin(j, k)));
C = C / TFloat(2.0);
if (C<0.0) C=TFloat(0.0);
if (C>1.0) C=TFloat(1.0);
// equation Gn,m(vn,vj,vk)= 1/2 qn,mmin[(1−qj,mvj),(1−qk,mvk)] (5.x):
Gi = TFloat(Qi) * fmin(j,k) / TFloat(2.0);
Gj = TFloat(Qj) * fmin(i,k) / TFloat(2.0);
Gk = TFloat(Qk) * fmin(i,j) / TFloat(2.0);
// equation Rn,m (vn , vj , vk ) = 1/2(qn,m −vn), Cm(vn,vj,vk)= 1/2(1−qn,mvn), 0 otherwise (5.x):
if (C == TFloat(i/TFloat(2.0)) ) {Ri = (TFloat(Qi)-Vi)/2.0;} else {Ri = TFloat(0.0);} //Qi*i/2.0*-1;} //= 0.0
if (C == TFloat(j/TFloat(2.0)) ) {Rj = (TFloat(Qj)-Vj)/2.0;} else {Rj = TFloat(0.0);} //Qj*j/2.0*-1;} //= 0.0
if (C == TFloat(k/TFloat(2.0)) ) {Rk = (TFloat(Qk)-Vk)/2.0;} else {Rk = TFloat(0.0);} //Qk*k/2.0*-1;} //= 0.0
// equation Vn = SUM xl,mxs,mGn,m + (1 + ζxl,m)(1 − xs,m)Rn,m (5.x):
TFloat _Vi = Xl * Xs * Gi + (TFloat(1.0) + dmm_zeta * Xl) * (TFloat(1.0) - Xs) * Ri ;
TFloat _Vj = Xl * Xs * Gj + (TFloat(1.0) + dmm_zeta * Xl) * (TFloat(1.0) - Xs) * Rj ;
TFloat _Vk = Xl * Xs * Gk + (TFloat(1.0) + dmm_zeta * Xl) * (TFloat(1.0) - Xs) * Rk ;
/*dxdt[liti-1] = dxdt[liti-1] + _Vi;
dxdt[litj-1] = dxdt[litj-1] + _Vj;
dxdt[litk-1] = dxdt[litk-1] + _Vk; */
//sum of vectors method:
if (_Vi!=0.0) dxdt_v[liti-1].push_back(_Vi);
if (_Vj!=0.0) dxdt_v[litj-1].push_back(_Vj);
if (_Vk!=0.0) dxdt_v[litk-1].push_back(_Vk);
// do not change unit_clauses:
if (unit_clauses) {
if (unit_clause_vars[liti]!=0) dxdt[liti-1] = 0.0;
if (unit_clause_vars[litj]!=0) dxdt[litj-1] = 0.0;
if (unit_clause_vars[litk]!=0) dxdt[litk-1] = 0.0;
}
}
// 2-sat:
if (ksat==2) {
int Qi = (cls[clause*MAX_LITS_SYSTEM+0]>0)? 1:-1; // +1 if literal is >0, otherwise -1
int Qj = (cls[clause*MAX_LITS_SYSTEM+1]>0)? 1:-1; // +1 if literal is >0, otherwise -1
int liti = abs(cls[clause*MAX_LITS_SYSTEM+0]);
int litj = abs(cls[clause*MAX_LITS_SYSTEM+1]);
TFloat Vi = x[liti-1]; if (Vi<-1.0) Vi = -1.0; if (Vi>1.0) Vi = 1.0;
TFloat Vj = x[litj-1]; if (Vj<-1.0) Vj = -1.0; if (Vj>1.0) Vj = 1.0;
TFloat i = TFloat(1.0)-TFloat(Qi)*Vi;
TFloat j = TFloat(1.0)-TFloat(Qj)*Vj;
C = TFloat(fmin(i, j));
C = C / TFloat(2.0) ;
if (C<0.0) C=TFloat(0.0);
if (C>1.0) C=TFloat(1.0);
//voltage:
Gi = TFloat(Qi) * j / TFloat(2.0);
Gj = TFloat(Qj) * i / TFloat(2.0);
if (C == TFloat(i/TFloat(2.0)) ) {Ri = (TFloat(Qi)-Vi)/2.0;} else {Ri = TFloat(0.0);} //Qi*i/2.0*-1;} //= 0.0
if (C == TFloat(j/TFloat(2.0)) ) {Rj = (TFloat(Qj)-Vj)/2.0;} else {Rj = TFloat(0.0);} //Qj*j/2.0*-1;} //= 0.0
TFloat _Vi = Xl * Xs * Gi + (TFloat(1.0) + dmm_zeta * Xl) * (TFloat(1.0) - Xs) * Ri;
TFloat _Vj = Xl * Xs * Gj + (TFloat(1.0) + dmm_zeta * Xl) * (TFloat(1.0) - Xs) * Rj;
/*dxdt[liti-1] = dxdt[liti-1] + _Vi;
dxdt[litj-1] = dxdt[litj-1] + _Vj;*/
//sum of vectors method:
if (_Vi!=0.0) dxdt_v[liti-1].push_back(_Vi);
if (_Vj!=0.0) dxdt_v[litj-1].push_back(_Vj);
// do not change unit_clauses:
if (unit_clauses) {
if (unit_clause_vars[liti]!=0) dxdt[liti-1] = 0.0;
if (unit_clause_vars[litj]!=0) dxdt[litj-1] = 0.0;
}
}
// k-sat:
if (ksat!=1 && ksat!=2 && ksat!=3) {
int lit[MAX_LITS_SYSTEM], Q[MAX_LITS_SYSTEM];
TFloat V[MAX_LITS_SYSTEM], _i[MAX_LITS_SYSTEM], R[MAX_LITS_SYSTEM], G[MAX_LITS_SYSTEM];
TFloat c_min=INT_MAX;
for (int i=0; i<ksat; i++) {
Q[i] = (cls[clause*MAX_LITS_SYSTEM+i]>0)? 1:-1; // +1 if literal is >0, otherwise -1
lit[i] = abs(cls[clause*MAX_LITS_SYSTEM+i]);
V[i] = x[lit[i]-1]; if (V[i]<-1.0) V[i]=-1.0; if (V[i]>1.0) V[i]=1.0; //boundary for v € [-1,1]:
_i[i] = TFloat(1.0)-TFloat(Q[i])*V[i];
// find min:
if (_i[i]<c_min) c_min = _i[i];
}
C = c_min / TFloat(2.0);
if (C<0.0) printf("*\n");//C=0.0; // never triggered?
if (C>1.0) printf("*\n");//C=1.0; // never triggered?
for (int i=0; i<ksat; i++) {
//find min of others:
TFloat g_min = INT_MAX;
for (int ii=0; ii<ksat; ii++) {if (ii!=i && _i[ii]<g_min) g_min = _i[ii];}
G[i] = TFloat(Q[i]) * g_min / TFloat(2.0);
TFloat comp = _i[i]/TFloat(2.0);
if (comp<0.0) printf("*\n");//comp = 0.0; // never triggered?
if (comp>1.0) printf("*\n");//comp = 1.0; // never triggered?
if (C != comp) {R[i] = TFloat(0.0);} else {R[i] = (TFloat(Q[i])-V[i]) / TFloat(2.0);}
TFloat _V = Xl * Xs * G[i] + (TFloat(1.0) + dmm_zeta * Xl) * (TFloat(1.0) - Xs) * R[i];
//dxdt[lit[i]-1] = dxdt[lit[i]-1] + _V;
//sum of vectors method:
if (_V!=0.0) dxdt_v[lit[i]-1].push_back(_V);
// do not change unit_clauses:
if (unit_clauses) {
if (unit_clause_vars[lit[i]]!=0) dxdt[lit[i]-1] = 0.0;
}
}
}
//update energy:
energy_thread[m_nodeid] += C;
//update loc:
if (C<0.5) loc--; //this clause is sat, reduce loc
//if (C==0.0) loc--;
// Calculate new Xs:
dxdt[n+clause] = dmm_beta * (Xs + dmm_epsilon) * (C - dmm_gamma);
// Calculate new Xl:
dxdt[n+m+clause] = dmm_alpha_cm[m_nodeid*m+clause] * (C - dmm_delta);
// update info variables:
if (clause==0) {
C_rem = C;
if (ksat<=3) {
G_rem = Gi; R_rem = Ri;
} else {
G_rem = 0.0;
R_rem = 0.0;
}
if (ksat==1) {
G_rem = 0.0;
R_rem = 0.0;
}
}
if (x[n+m+clause]>1.0) cnt_xl_pos++;
if (x[n+clause]>0.0) cnt_xs_pos++;
} //---clause calculation loop
// summation of voltages SUM dxdt_v[n] => dxdt[n]: ------------------------------------------------------
for (int i=0; i<n; i++) {
std::sort(dxdt_v[i].begin(), dxdt_v[i].end()); //summing with smallest first increases accuracy
dxdt[i] = accumulate(dxdt_v[i].begin(), dxdt_v[i].end(), (TFloat) 0.0);
}
// -------------------------------------------------------------------------------------------------------
// test against solution:
int corr = 0;
//update energy:
if (tune) energy_thread[m_nodeid] = loc; //tuning on loc...
// screen output: ----------------------------------------------------------------------------------------
if (!c_key_loaded && !quiet && m_nodeid==global_best_thread) {
int showglobal = global_thread[m_nodeid];
if (massive) showglobal = massive_global;
std::cout << std::setprecision(2) << std::fixed << TEXT_DEFAULT
<< "\rc [" << m_nodeid << "] " << time_spent << "s "
<< "T=" << t
<< " GLOBAL=" << global
<< " (" << showglobal << ")"
<< " (LOC=" << loc << ")"
<< " (" << stepcounter[m_nodeid] << ")"
//<< " α=" << m_alpha
<< " α=" << dmm_alpha_cm[m_nodeid*m+0]
<< " β=" << dmm_beta
<< " C=" << C_rem
<< " (" << cls[0] << "=" << x[abs(cls[0])-1]
<< ", " << cls[1] << "=" << x[abs(cls[1])-1]
<< ", " << cls[2] << "=" << x[abs(cls[2])-1] << ")"
<< " Xs=" << x[n]
<< " Xl=" << x[n+m]
//<< " R=" << R_rem
//<< " G=" << G_rem
<< " #Xs>0:" << cnt_xs_pos
<< " #Xl>1:" << cnt_xl_pos
<< " Σe=" << energy_thread[m_nodeid] << " "
// test against solution:
<< (n-corr) << "/" << n << " (best: " << (n-solmax) << ") ";
fflush(stdout);
}
// update global_all_runs_thread & update v_best: --------------------------------------------------------
if (loc <= global_all_runs_thread[m_nodeid]) {
global_all_runs_thread[m_nodeid] = loc;
t_init[m_nodeid] = t;
pthread_mutex_lock(&mut);
//update v_best array:
for (int i=0; i<n+m*2; i++) v_best[m_nodeid*(n+m*2)+i] = x[i];
pthread_mutex_unlock(&mut);
}
// update loc of thread: ---------------------------------------------------------------------------------
loc_thread[m_nodeid] = loc;
time_thread_actual[m_nodeid] = t;
//new lower lock (global)? or lower energy (global)? -----------------------------------------------------
if (loc<global || energy_thread[m_nodeid]<global_energy || energy_thread[m_nodeid]<energy_thread_min[m_nodeid] || loc<global_thread[m_nodeid]) {
if (loc<global) {
pthread_mutex_lock(&mut);
global = loc;
global_best_thread = m_nodeid;
pthread_mutex_unlock(&mut);
//if (!tune) t_begin_thread[m_nodeid] = t_end_thread[m_nodeid];
}
if (energy_thread[m_nodeid]<global_energy) {
pthread_mutex_lock(&mut);
global_energy = energy_thread[m_nodeid];
global_best_thread = m_nodeid;
pthread_mutex_unlock(&mut);
}
if (energy_thread[m_nodeid]<energy_thread_min[m_nodeid]) {
pthread_mutex_lock(&mut);
global_best_thread = m_nodeid;
pthread_mutex_unlock(&mut);
}
if (loc<global_thread[m_nodeid]) {
pthread_mutex_lock(&mut);
global_best_thread = m_nodeid;
pthread_mutex_unlock(&mut);
}
if (!quiet) {
int showglobal = global_thread[m_nodeid];
if (massive) showglobal = massive_global;
std::cout << std::setprecision(2) << std::fixed << TEXT_DEFAULT
<< "\rc [" << m_nodeid << "] " << time_spent << "s "
<< "T=" << t
<< " GLOBAL=" << global
<< " (" << showglobal << ")"
<< " (LOC=" << loc << ")"
<< " (" << stepcounter[m_nodeid] << ")"
//<< " α=" << m_alpha
<< " α=" << dmm_alpha_cm[m_nodeid*m+0]
<< " β=" << dmm_beta
<< " C=" << C_rem
<< " (" << cls[0] << "=" << x[abs(cls[0])-1]
<< ", " << cls[1] << "=" << x[abs(cls[1])-1]
<< ", " << cls[2] << "=" << x[abs(cls[2])-1] << ")"
<< " Xs=" << x[n]
<< " Xl=" << x[n+m]
//<< " R=" << R_rem
//<< " G=" << G_rem
<< " #Xs>0:" << cnt_xs_pos
<< " #Xl>1:" << cnt_xl_pos
<< " Σe=" << energy_thread[m_nodeid] << " "
// test against solution:
<< (n-corr) << "/" << n << " (best: " << (n-solmax) << ") " << std::endl;
fflush(stdout);
} else {std::cout << std::endl;}
}
// update energy of thread: ------------------------------------------------------------------------------
if (energy_thread[m_nodeid] < energy_thread_min[m_nodeid]) {
energy_thread_min[m_nodeid] = energy_thread[m_nodeid];
}
//new thread global? --------------------------------------------------------------------------------------
if (loc<global_thread[m_nodeid]) {
// update global_thread, time_thread and walltime_thread:
global_thread[m_nodeid] = loc;
time_thread[m_nodeid] = t;
walltime_thread[m_nodeid] = time_spent;
}
// loc file? (used for dashboard) --------------------------------------------------------------------------
if (writelocfile && m_nodeid==global_best_thread && stepcounter[global_best_thread] % 100 == 0) {
#ifdef USEFPRINTF
FILE *floc = fopen(LOC_FILE, "a");
fprintf(floc,"%d;%.5f;%.5f;%d;%.2f\n",global_best_thread,time_spent,t,global,global_energy);
fclose(floc);
#endif
}
//solved? -------------------------------------------------------------------------------------------------
//if (energy_thread[m_nodeid]<0.1) {
if (loc==0) {
quiet=1;
pthread_mutex_lock(&mut);
printf(TEXT_YELLOW);
printf("\nc [%d] T=",m_nodeid); std::cout << t << " SOLUTION FOUND" << std::endl;
for (int i=0; i<n; i++) {
if (x[i]>0) printf("%d ",(i+1));
if (x[i]<0) printf("%d ",(i+1)*-1);
}
fflush(stdout);
printf("\nc [%d] VERIFYING...\n",m_nodeid); if (!quiet) printf(TEXT_DEFAULT);
bool sat = true; bool clausesat;
for (int i=0; i<m; i++) {
for (int j=0; j<clauseSizes[i]; j++) {
clausesat = false;
int lit = abs(cls[i*MAX_LITS_SYSTEM+j]);
if ( (x[lit-1]>0 && cls[i*MAX_LITS_SYSTEM+j]>0) || (x[lit-1]<0 && cls[i*MAX_LITS_SYSTEM+j]<0) ) {
clausesat = true;
break;
}
}
if (!clausesat) {
sat = false;
break;
}
}
printf(TEXT_YELLOW);
if (sat) {
printf(TEXT_YELLOW); printf("c [%d] SAT (VERIFIED)\n",m_nodeid); solved = 1;
//write solution to file:
#ifdef USEFPRINTF
FILE *fs = fopen(SOLUTION_FILE, "w");
for (int i=0; i<n; i++) {
if (x[i]>=0) fprintf(fs,"%d, ",i+1);
if (x[i]<0) fprintf(fs,"%d, ",(i+1)*-1);
//fprintf(fs,"%.5f, ",x[i]); // current G_field = solution
}
fclose(fs);
#endif
}
if (!sat) {printf(TEXT_RED); printf("c [%d] UNSAT (VERIFIED)\n",m_nodeid);}
printf(TEXT_DEFAULT);
pthread_mutex_unlock(&mut);
// update locfile:
if (writelocfile) {
#ifdef USEFPRINTF
FILE *floc = fopen(LOC_FILE, "a");
fprintf(floc,"%d;%.5f;%.5f;%d;%.2f\n",global_best_thread,time_spent,t,global,global_energy);
fclose(floc);
#endif
}
quiet=0;
} // ---output
}
return dxdt;
}
//-------------------------------------------------------------------------------------------------------------------
// ode function as structure: (boost version)
struct odeS
{
int m_nodeid;
TFloat m_alpha; // unused; we are using dmm_alpha_cm[], one alpha for each clause
TFloat m_beta;
TFloat m_gamma;
TFloat m_delta;
TFloat m_epsilon;
TFloat m_zeta;
odeS(int t_nodeid=0, TFloat t_alpha=5.0, TFloat t_beta=20.0, TFloat t_gamma=0.23, TFloat t_delta=0.04, TFloat t_epsilon=0.11, TFloat t_zeta=0.11)
: m_nodeid(t_nodeid), m_alpha(t_alpha), m_beta(t_beta), m_gamma(t_gamma), m_delta(t_delta), m_epsilon(t_epsilon), m_zeta(t_zeta) {}
void operator()(const state_type &x , state_type &dxdt , TFloat t ) const
{
/* timer and stats */
t_end_thread[m_nodeid] = pt::microsec_clock::local_time().time_of_day().total_milliseconds();//clock();
double time_spent = (double)(t_end_thread[m_nodeid] - t_begin_thread[m_nodeid])/1000;// / CLOCKS_PER_SEC;
time_thread_actual[m_nodeid] = t;
// stop integration if solved or walltime timeout exceeded:
if (solved==1 || time_spent>walltime_timeout || (tune && time_spent<0)) {
pthread_mutex_lock(&mut);
running_threads--;
pthread_mutex_unlock(&mut);
pthread_exit(&mut);
}
energy_thread[m_nodeid] = 0.0;
/* main ODE routine */
/* This bounding procedure effectively mimics the numerical technique that we use to bound the dynamics, where any outward pointing component of the flow field on the boundary is simply ignored.*/
if (solved!=1) {
int loc;
// Loop ---------------------------------------------------------------------------------------------------------------
loc = m;
//set all V to 0.00:
//fill( dxdt.begin() , dxdt.begin()+n , TFloat(0.0) ); //not required?
// screen info variables:
TFloat C_rem, R_rem, G_rem;
int cnt_xl_pos = 0;
int cnt_xs_pos = 0;
// loop through each clause:
for (int clause = 0; clause < m; clause++) {
//for (int clause = m-1; clause >= 0; clause--) {
// number of literals in this clause:
int ksat = clauseSizes[clause];
// Xl & Xs:
TFloat Xs = x[clause+n]; if (Xs<0.0) Xs = TFloat(0.0); if (Xs>1.0) Xs = TFloat(1.0); //Xs bounds
TFloat Xl = x[clause+n+m]; if (Xl<1.0) Xl = TFloat(1.0); if (Xl>xl_max) Xl = TFloat(xl_max); //Xl bounds
TFloat C = TFloat(0.0);
TFloat Ri, Rj, Rk, Gi, Gj, Gk;
// 3-sat:
if (ksat==3) {
int Qi = (cls[clause*MAX_LITS_SYSTEM+0]>0)? 1:-1; // +1 if literal is >0, otherwise -1
int Qj = (cls[clause*MAX_LITS_SYSTEM+1]>0)? 1:-1; // +1 if literal is >0, otherwise -1
int Qk = (cls[clause*MAX_LITS_SYSTEM+2]>0)? 1:-1; // +1 if literal is >0, otherwise -1
int liti = abs(cls[clause*MAX_LITS_SYSTEM+0]);
int litj = abs(cls[clause*MAX_LITS_SYSTEM+1]);
int litk = abs(cls[clause*MAX_LITS_SYSTEM+2]);
TFloat Vi = x[liti-1]; if (Vi<-1.0) Vi = -1.0; if (Vi>1.0) Vi = 1.0; //V bounds
TFloat Vj = x[litj-1]; if (Vj<-1.0) Vj = -1.0; if (Vj>1.0) Vj = 1.0; //V bounds
TFloat Vk = x[litk-1]; if (Vk<-1.0) Vk = -1.0; if (Vk>1.0) Vk = 1.0; //V bounds
TFloat i = TFloat(1.0)-TFloat(Qi)*Vi;
TFloat j = TFloat(1.0)-TFloat(Qj)*Vj;
TFloat k = TFloat(1.0)-TFloat(Qk)*Vk;
C = TFloat(fmin(i, fmin(j, k)));
C = C / TFloat(2.0);
if (C<0.0) C=TFloat(0.0);
if (C>1.0) C=TFloat(1.0);
// equation Gn,m(vn,vj,vk)= 1/2 qn,mmin[(1−qj,mvj),(1−qk,mvk)] (5.x):
Gi = TFloat(Qi) * fmin(j,k) / TFloat(2.0);
Gj = TFloat(Qj) * fmin(i,k) / TFloat(2.0);
Gk = TFloat(Qk) * fmin(i,j) / TFloat(2.0);
// equation Rn,m (vn , vj , vk ) = 1/2(qn,m −vn), Cm(vn,vj,vk)= 12(1−qn,mvn), 0 otherwise (5.x):
if (C != i/TFloat(2.0) ) {Ri = TFloat(0.0);} else {Ri = (TFloat(Qi) - Vi) / TFloat(2.0);}
if (C != j/TFloat(2.0) ) {Rj = TFloat(0.0);} else {Rj = (TFloat(Qj) - Vj) / TFloat(2.0);}
if (C != k/TFloat(2.0) ) {Rk = TFloat(0.0);} else {Rk = (TFloat(Qk) - Vk) / TFloat(2.0);}
// equation Vn = SUM xl,mxs,mGn,m + (1 + ζxl,m)(1 − xs,m)Rn,m (5.x):
TFloat _Vi = Xl * Xs * Gi + (TFloat(1.0) + m_zeta * Xl) * (TFloat(1.0) - Xs) * Ri ;
TFloat _Vj = Xl * Xs * Gj + (TFloat(1.0) + m_zeta * Xl) * (TFloat(1.0) - Xs) * Rj ;
TFloat _Vk = Xl * Xs * Gk + (TFloat(1.0) + m_zeta * Xl) * (TFloat(1.0) - Xs) * Rk ;
dxdt[liti-1] = dxdt[liti-1] + _Vi;
dxdt[litj-1] = dxdt[litj-1] + _Vj;
dxdt[litk-1] = dxdt[litk-1] + _Vk;
// do not change unit_clauses:
if (unit_clauses) {
if (unit_clause_vars[liti]!=0) dxdt[liti-1] = 0.0;
if (unit_clause_vars[litj]!=0) dxdt[litj-1] = 0.0;
if (unit_clause_vars[litk]!=0) dxdt[litk-1] = 0.0;
}
}
// 2-sat:
if (ksat==2) {
int Qi = (cls[clause*MAX_LITS_SYSTEM+0]>0)? 1:-1; // +1 if literal is >0, otherwise -1
int Qj = (cls[clause*MAX_LITS_SYSTEM+1]>0)? 1:-1; // +1 if literal is >0, otherwise -1
int liti = abs(cls[clause*MAX_LITS_SYSTEM+0]);
int litj = abs(cls[clause*MAX_LITS_SYSTEM+1]);
TFloat Vi = x[liti-1]; if (Vi<-1.0) Vi = -1.0; if (Vi>1.0) Vi = 1.0;
TFloat Vj = x[litj-1]; if (Vj<-1.0) Vj = -1.0; if (Vj>1.0) Vj = 1.0;
TFloat i = TFloat(1.0)-TFloat(Qi)*Vi;
TFloat j = TFloat(1.0)-TFloat(Qj)*Vj;
C = TFloat(fmin(i, j));
C = C / TFloat(2.0) ;
if (C<0.0) C=TFloat(0.0);
if (C>1.0) C=TFloat(1.0);
//voltage:
Gi = TFloat(Qi) * j / TFloat(2.0);
Gj = TFloat(Qj) * i / TFloat(2.0);
if (C != i/TFloat(2.0) ) {Ri = TFloat(0.0);} else {Ri = (TFloat(Qi) - Vi) / TFloat(2.0);}
if (C != j/TFloat(2.0) ) {Rj = TFloat(0.0);} else {Rj = (TFloat(Qj) - Vj) / TFloat(2.0);}
TFloat _Vi = Xl * Xs * Gi + (TFloat(1.0) + m_zeta * Xl) * (TFloat(1.0) - Xs) * Ri;
TFloat _Vj = Xl * Xs * Gj + (TFloat(1.0) + m_zeta * Xl) * (TFloat(1.0) - Xs) * Rj;
dxdt[liti-1] = dxdt[liti-1] + _Vi;
dxdt[litj-1] = dxdt[litj-1] + _Vj;
// do not change unit_clauses:
if (unit_clauses) {
if (unit_clause_vars[liti]!=0) dxdt[liti-1] = 0.0;
if (unit_clause_vars[litj]!=0) dxdt[litj-1] = 0.0;
}
}
// k-sat:
if (ksat!=1 && ksat!=2 && ksat!=3) {
int lit[MAX_LITS_SYSTEM], Q[MAX_LITS_SYSTEM];
TFloat V[MAX_LITS_SYSTEM], _i[MAX_LITS_SYSTEM], R[MAX_LITS_SYSTEM], G[MAX_LITS_SYSTEM];
TFloat c_min=INT_MAX;
for (int i=0; i<ksat; i++) {
Q[i] = (cls[clause*MAX_LITS_SYSTEM+i]>0)? 1:-1; // +1 if literal is >0, otherwise -1
lit[i] = abs(cls[clause*MAX_LITS_SYSTEM+i]);
V[i] = x[lit[i]-1]; if (V[i]<-1.0) V[i]=-1.0; if (V[i]>1.0) V[i]=1.0; //boundary for v € [-1,1]:
_i[i] = 1.0-Q[i]*V[i];
// find min:
if (_i[i]<c_min) c_min = _i[i];
}
C = c_min / TFloat(2.0);
if (C<0.0) printf("*\n");//C=0.0; // never triggered?
if (C>1.0) printf("*\n");//C=1.0; // never triggered?
for (int i=0; i<ksat; i++) {
//find min of others:
TFloat g_min = INT_MAX;
for (int ii=0; ii<ksat; ii++) {if (ii!=i && _i[ii]<g_min) g_min = _i[ii];}
G[i] = Q[i] * g_min / TFloat(2.0);
TFloat comp = (1.0-Q[i]*V[i])/TFloat(2.0);
if (comp<0.0) printf("*\n");//comp = 0.0; // never triggered?
if (comp>1.0) printf("*\n");//comp = 1.0; // never triggered?
if (C != comp) {R[i] = 0.0;} else {R[i] = (Q[i] - V[i]) / TFloat(2.0);}
TFloat _V = Xl * Xs * G[i] + (TFloat(1.0) + m_zeta * Xl) * (TFloat(1.0) - Xs) * R[i];
dxdt[lit[i]-1] = dxdt[lit[i]-1] + _V;
// do not change unit_clauses:
if (unit_clauses) {
if (unit_clause_vars[lit[i]]!=0) dxdt[lit[i]-1] = 0.0;
}
}
}
//update energy:
energy_thread[m_nodeid] += C;
//update loc:
if (C<0.5) loc--; //this clause is sat, reduce loc
//if (C==0.0) loc--;
// Calculate new Xs:
dxdt[n+clause] = m_beta * (Xs + m_epsilon) * (C - m_gamma);
// Calculate new Xl:
dxdt[n+m+clause] = dmm_alpha_cm[m_nodeid*m+clause] * (C - m_delta);
// update info variables:
if (clause==0) {
C_rem = C;
if (ksat<=3) {
G_rem = Gi; R_rem = Ri;
} else {
G_rem = 0.0;
R_rem = 0.0;
}
if (ksat==1) {
G_rem = 0.0;
R_rem = 0.0;
}
}
if (x[n+m+clause]>1.0) cnt_xl_pos++;
if (x[n+clause]>0.0) cnt_xs_pos++;
}
// test against solution:
int corr = 0;
//update energy:
if (tune) energy_thread[m_nodeid] = loc;
// screen output: ----------------------------------------------------------------------------------------
if (!c_key_loaded && !quiet && m_nodeid==global_best_thread) {
std::cout << std::setprecision(2) << std::fixed << TEXT_DEFAULT
<< "\rc [" << m_nodeid << "] " << time_spent << "s "
<< "T=" << t
<< " GLOBAL=" << global
<< " (THREAD=" << global_thread[m_nodeid] << ")"
<< " (LOC=" << loc << ")"
<< " (" << stepcounter[m_nodeid] << ")"
//<< " α=" << m_alpha
<< " α=" << dmm_alpha_cm[m_nodeid*m+0]
<< " β=" << m_beta
<< " C=" << C_rem
<< " (" << cls[0] << "=" << x[abs(cls[0])-1]
<< ", " << cls[1] << "=" << x[abs(cls[1])-1]
<< ", " << cls[2] << "=" << x[abs(cls[2])-1] << ")"
<< " Xs=" << x[n]
<< " Xl=" << x[n+m]
//<< " R=" << R_rem
//<< " G=" << G_rem
<< " #Xs>0:" << cnt_xs_pos
<< " #Xl>1:" << cnt_xl_pos
<< " Σe=" << energy_thread[m_nodeid] << " ";
fflush(stdout);
// test against solution:
std::cout << (n-corr) << "/" << n << " (best: " << (n-solmax) << ") ";
}
//flowvector output: -------------------------------------------------------------------------------------
/*
if (m_nodeid==0 && flowvector_log) {
FILE *fvec = fopen(FLOWVECTOR_FILE, "a");
fprintf(fvec,"%.15f;",t);
for (int i=0; i<n+m*2;i++) fprintf(fvec,"%.15f;",x[i]);
fprintf(fvec,"\n");
fclose(fvec);
}
*/
// update global_all_runs_thread & update v_best: --------------------------------------------------------
if (loc <= global_all_runs_thread[m_nodeid]) {
global_all_runs_thread[m_nodeid] = loc;
t_init[m_nodeid] = t;
pthread_mutex_lock(&mut);
//update v_best array:
for (int i=0; i<n+m*2; i++) v_best[m_nodeid*(n+m*2)+i] = x[i]; //G_field[i];
pthread_mutex_unlock(&mut);
}
// update loc of thread: ---------------------------------------------------------------------------------
loc_thread[m_nodeid] = loc;
time_thread_actual[m_nodeid] = t;
// update energy of thread: ------------------------------------------------------------------------------
if (energy_thread[m_nodeid] < energy_thread_min[m_nodeid]) {
energy_thread_min[m_nodeid] = energy_thread[m_nodeid];
}
//new lower lock (global)? or lower energy (global)? -----------------------------------------------------
if (loc<global || energy_thread[m_nodeid]<global_energy) {
if (loc<global) {
pthread_mutex_lock(&mut);
global = loc;
global_best_thread = m_nodeid;
pthread_mutex_unlock(&mut);
}
if (energy_thread[m_nodeid]<global_energy) {
pthread_mutex_lock(&mut);
global_energy = energy_thread[m_nodeid];
global_best_thread = m_nodeid;
pthread_mutex_unlock(&mut);
}