-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathCalculateNetworkCost.py
1728 lines (1489 loc) · 83.1 KB
/
CalculateNetworkCost.py
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
#!/usr/bin/env python3
import argparse
import datetime
import hashlib
import json
import logging
import os
import pathlib
import re
import subprocess
import sys
import time
import traceback
from typing import List, Tuple, Union, Dict, Optional
from rich.logging import RichHandler as rich_RichHandler
g_logger = logging.getLogger('CNC')
# ---
# NOTE
# 1. • The prefix 'g_' denotes that it is a global variable.
# • The prefix 'fn_' denotes that the variable stores a function.
# • The prefix 'mas_' denotes that the function will do Monitoring and Stopping of running
# solver instances depending on the conditions/parameters mentioned after this prefix.
# • The prefix 'r_' denotes that the variable is for some system resource (CPU, RAM, time, ...)
# 2. 'pid_' and '.txt' are the prefix and suffix respectively
# for text file having PID of the bash running inside tmux.
# 3. 'std_out_err_' and '.txt' are the prefix and suffix respectively for the text file
# having the merged content of `stdout` and `stderr` stream of the tmux session which
# runs the solver.
# 4. Tmux session prefix is 'AR_NC_'.
# 5. For naming files, always try to just use alpha-numeric letters and underscore ('_') only.
# Assumptions
# 1. Linux OS is used for execution
# 2. `bash`, `which`, `nproc`, `tmux` are installed
# 3. Python lib `rich` is installed
# 4. AMPL, Baron, Octeract are installed and properly configured
# (Execution is done from "mtp" directory or any other directory with the same directory structure)
# 5. Model files are present at the right place
# 6. There is no limitation on the amount of available RAM (This assumption make the program simpler.
# However, in future, it may be removed during deployment to make sure the solvers runs at optimal
# speed - accordingly changes need to be done in this program)
# 7. Satisfy RegEx r'(a-zA-Z0-9_ )+' -> Absolute path of this Python script, and
# absolute path to graph/network (i.e. data/testcase file)
# ---
# True if STDOUT and STDERR are connected to terminal (and NOT "a file or a pipe")
# REFER: https://github.com/alttch/neotermcolor
# REFER: https://stackoverflow.com/questions/1077113/how-do-i-detect-whether-sys-stdout-is-attached-to-terminal-or-not
# NOTE: This checking of whether stdout and stderr are mapped to terminal or not is required to prevent some garbage
# values from inadvertently replacing starting lines of the log file to which stdout and stderr are redirected
g_STD_OUT_ERR_TO_TERMINAL = (sys.stdout.isatty() and sys.stderr.isatty())
# REFER: https://stackoverflow.com/questions/3172470/actual-meaning-of-shell-true-in-subprocess
g_BASH_PATH = subprocess.check_output(['which', 'bash'], shell=False).decode().strip()
def run_command(cmd: str, default_result: str = '', debug_print: bool = False) -> Tuple[bool, str]:
"""Execute `cmd` using bash
`stderr` is merged with `stdout`
Returns:
Tuple of [ok, output]
"""
if debug_print:
g_logger.debug(f'COMMAND:\n`{cmd}`')
try:
# NOTE: Not using the below lines of code because they use "sh" shell,
# and it does not seem to properly parse the command.
# >>> status_code, output = subprocess.getstatusoutput(cmd)
# >>> os.system(...) # Has the same issue because it uses `sh`
# Example 1: `$ kill -s SIGINT 12345` did not work and gave the following error:
# '/bin/sh: 1: kill: invalid signal number or name: SIGINT'
# The error logs of testing has been put in "REPO/logs/2022-01-22_ssh_kill_errors.txt"
# Example 2: Run the below command in `bash`, `zsh` and `sh` to see the difference in behaviour
# `$ echo cool 1>> /dev/stderr &> /dev/null`
# bash --> no output
# zsh --> output="cool", unexpected behaviour is seen when redirecting stdout to stderr, and
# unexpected behaviour has been seen with few other commands as well
# sh --> output="cool", the command is launched asynchronously due of misinterpretation of &>
output = subprocess.check_output(
[g_BASH_PATH, '-c', cmd],
stderr=subprocess.STDOUT,
shell=False
).decode().strip()
if debug_print:
g_logger.debug(f'OUTPUT:\n{output}')
return True, output
except subprocess.CalledProcessError as e:
g_logger.info(f'EXCEPTION OCCURRED, will return default_result ("{default_result}") as the output')
g_logger.info(f'CalledProcessError = {str(e).splitlines()[-1]}')
g_logger.info(f'Output = {e.output}')
# g_logger.warning(e)
# g_logger.warning(traceback.format_exc())
if debug_print:
g_logger.debug(default_result)
return False, default_result
def run_command_get_output(cmd: str, default_result: str = '0', debug_print: bool = False) -> str:
"""Execute `cmd` using bash
The return value in case of unsuccessful execution of command `cmd` is '0', because sometimes we have used
this method to get PID of some process and used kill command to send some signal (SIGINT in most cases) to
that PID. If the command `cmd` which is used to find the PID of the target process fails, then in that case
we return '0' so that kill command does not send the signal to any random process, instead it sends the
signal to itself. Thus saving us from having to write `if` conditions which verify whether the PID is valid
or not before executing the `kill` command.
The `kill` commands differs with situation:
1. kill --help
(dev) ➜ ~ which kill
kill: shell built-in command
2. /bin/kill --help
(dev) ➜ ~ env which kill
/bin/kill
3. bash -c 'kill --help' (This has been used in this script)
(dev) ➜ ~ bash -c 'which kill'
/bin/kill
Returns:
The return value is `default_result` (or '0') if the command `cmd` exits with a non-zero exit code.
If command `cmd` executes successfully, then stdout and stderr are merged and returned as one string
"""
return run_command(cmd, default_result, debug_print)[1]
# ---
def delete_last_lines(n=1):
"""Delete `n` number of lines from stdout. NOTE: This works only if stdout is mapped to a TTY."""
# REFER: https://www.quora.com/How-can-I-delete-the-last-printed-line-in-Python-language
for _ in range(n):
sys.stdout.write('\x1b[1A') # Cursor up one line
sys.stdout.write('\x1b[2K') # Erase line
sys.stdout.flush()
def get_free_ram() -> float:
"""Returns: free RAM in GiB"""
# REFER: https://stackoverflow.com/questions/34937580/get-available-memory-in-gb-using-single-bash-shell-command/34938001
return float(run_command_get_output(r'''awk '/MemFree/ { printf "%.3f\n", $2/1024/1024 }' /proc/meminfo'''))
def get_free_swap() -> float:
"""Returns: free Swap in GiB"""
# REFER: https://stackoverflow.com/questions/34937580/get-available-memory-in-gb-using-single-bash-shell-command/34938001
return float(run_command_get_output(r'''awk '/SwapFree/ { printf "%.3f\n", $2/1024/1024 }' /proc/meminfo'''))
def get_execution_time(pid: Union[int, str]) -> int:
"""Returns: wall clock based execution time in seconds"""
# NOTE: etime measures "wall clock time", i.e. difference between now and the moment the process was started
# REFER: https://stackoverflow.com/questions/17737531/linux-ps-command-get-process-running-time-different-between-etime-and-time-p
# REFER: https://unix.stackexchange.com/questions/7870/how-to-check-how-long-a-process-has-been-running
return int(run_command(f'ps -o etimes= -p "{pid}"', str(10 ** 15))[1]) # 10**15 seconds == ~3.17 crore years
def get_process_running_status(pid: Union[int, str]) -> bool:
"""Returns: Whether the process with PID=pid is running or not"""
return run_command(f'ps -p {pid}')[0]
def file_hash_sha256(file_path) -> str:
"""
This is same as `shasum -a 256 FilePath`. REFER: https://en.wikipedia.org/wiki/Secure_Hash_Algorithms
It is assumed that the file will exist
"""
# REFER: https://stackoverflow.com/questions/16874598/how-do-i-calculate-the-md5-checksum-of-a-file-in-python
total_bytes = os.path.getsize(file_path)
with open(file_path, "rb") as f:
file_hash = hashlib.sha256()
chunk = f.read(8192)
total_progress = min(len(chunk), total_bytes)
last_progress_printed = -1
if g_STD_OUT_ERR_TO_TERMINAL:
g_logger.info('--- blank link to handle first use of `delete_last_lines()` '
'that happens before first progress logging ---')
while chunk:
file_hash.update(chunk)
new_progress = (100 * total_progress) // total_bytes
if new_progress != last_progress_printed:
if g_logger.getEffectiveLevel() <= logging.INFO:
if g_STD_OUT_ERR_TO_TERMINAL:
delete_last_lines()
g_logger.info(f'Hash calculation {new_progress}% done')
last_progress_printed = new_progress
chunk = f.read(8192)
total_progress = min(total_progress + len(chunk), total_bytes)
return file_hash.hexdigest()
# ---
class SolverOutputAnalyzerParent:
def __init__(self, engine_path: str, engine_options: str, process_name_to_stop_using_ctrl_c: str):
"""
Perform output analysis (i.e. extract solution, error messages and other necessary information) for various solvers
Args:
engine_path: Path to the solver that will be used by AMPL
engine_options: Solver specific parameters in AMPL format
process_name_to_stop_using_ctrl_c: Name of the process that is to be stopped using
Ctrl+C (i.e. SIGINT signal) such that solver smartly
gives us the best solution found till that moment
"""
self.engine_path = engine_path
self.engine_options = engine_options
self.process_name_to_stop_using_ctrl_c = process_name_to_stop_using_ctrl_c
def check_solution_found(self, exec_info: 'NetworkExecutionInformation') -> bool:
"""
Parses the output (stdout and stderr) of the solver and tells us
whether the solver has found any feasible solution or not
Args:
exec_info: NetworkExecutionInformation object having all information regarding the execution of the solver
Returns:
A boolean value telling whether the solver found any feasible solution or not
"""
g_logger.error(f"`self.check_solution_found` is 'Not Implemented' for {self.engine_path=}")
return True
def extract_best_solution(self, exec_info: 'NetworkExecutionInformation') -> Tuple[bool, float]:
"""
Parses the output (stdout and stderr) of the solver and tells us
whether the solver has found any feasible solution or not, and if
it has, then return its value as well.
Args:
exec_info: NetworkExecutionInformation object having all information regarding the execution of the solver
Returns:
A boolean value telling whether the solver found any feasible solution or not
A float value which is the optimal solution found till that moment
"""
g_logger.error(f"`self.extract_best_solution` is 'Not Implemented' for {self.engine_path=}")
return True, 0.0
def check_errors(self, exec_info: 'NetworkExecutionInformation') -> Tuple[bool, str]:
"""By default, it is assumed that everything is ok (i.e. error free)"""
g_logger.error(f"`self.check_errors` is 'Not Implemented' for {self.engine_path=}")
return True, '?'
def extract_solution_vector(self, exec_info: 'NetworkExecutionInformation') -> Tuple[bool, str, float, str]:
"""Returns: Tuple(status, file_to_parse, objective_value, solution_vector)"""
g_logger.error(f"`self.extract_solution_vector` is 'Not Implemented' for {self.engine_path=}")
return False, '?', float('nan'), '?'
@staticmethod
def ampl_check_errors(file_txt: str) -> Tuple[bool, str]:
"""
Check if any error has occurred due to AMPL
Args:
file_txt: std_out_err.txt file content
Returns:
A boolean value telling whether everything is ok (True) or not (False)
A string value containing the error message
"""
# AMPL binary does not exist
try:
err_idx = file_txt.index('no such file or directory: ./ampl.linux-intel64/ampl')
err_msg = file_txt.strip()
g_logger.debug(err_msg)
return False, err_msg
except ValueError:
pass # substring not found
# Execute permission not available for AMPL
try:
err_idx = file_txt.index('permission denied: ./ampl.linux-intel64/ampl')
err_msg = file_txt.strip()
g_logger.debug(err_msg)
return False, err_msg
except ValueError:
pass # substring not found
# Execute permission not available for Solver
try:
if 'Cannot invoke' in file_txt and 'Permission denied' in file_txt:
err_idx = file_txt.index('Permission denied')
err_msg = file_txt[:err_idx + len('Permission denied') + 1].strip()
g_logger.debug(err_msg)
return False, err_msg
except Exception as e:
g_logger.error(f'FIXME: {type(e)}:\n{e}')
# AMPL demo licence limitation
err_idx = None
try:
err_idx = file_txt.index('Sorry, a demo license for AMPL is limited to')
err_msg = file_txt[err_idx:file_txt.index('ampl:', err_idx)].replace('\n', ' ').strip()
g_logger.debug(err_msg)
return False, err_msg
except ValueError:
if err_idx is not None:
g_logger.debug(file_txt[err_idx:])
return False, file_txt[err_idx:]
pass # substring not found
# Probable explanation: some error occurred before the presolve phase
err_idx = None
try:
err_idx = file_txt.index('Error executing "solve" command:')
err_msg = file_txt[err_idx:file_txt.index('<BREAK>', err_idx)].replace('\n', ' ').strip()
g_logger.debug(err_msg)
return False, err_msg
except ValueError:
if err_idx is not None:
g_logger.debug(file_txt[err_idx:])
return False, file_txt[err_idx:]
pass # substring not found
del err_idx
# Solver did not proceed due to some impossible situations, e.g. X should be <= 3 and >= 7
try:
if '_total_solve_time = 0' in file_txt:
err_idx = file_txt.index('presolve:')
err_msg = file_txt[err_idx: file_txt.index('_total_solve_time')]
g_logger.debug(err_msg)
return False, err_msg
except ValueError:
pass # substring not found
return True, 'No Errors'
class SolverOutputAnalyzerBaron(SolverOutputAnalyzerParent):
pass
r"""
### Baron - v21.1.13
```sh
console
# Refer the analysis of `sum.lst` for things before execution of Control-C
BARON: Cntrl-C Abort
BARON 21.1.13 (2021.01.13): Interrupted by Control-C; objective (numberRegex)
Retaining scratch directory "/tmp/baron_tmp15378".
/tmp/baron_tmp15378/sum.lst - Exact copy of console output (output after ctrl+c is not present in this file)
67: Doing local search
68: Preprocessing found feasible solution with value (numberRegex)
71: Estimated remaining time for local search is [0-9]+ secs
72: Estimated remaining time for local search is [0-9]+ secs
73: Done with local search
75: Iteration Open nodes Time (s) Lower bound Upper bound
/tmp/baron_tmp15378/res.lst - Detailed output / logging of the execution and its status
67: Doing local search
68: >>> Preprocessing found feasible solution
69: >>> Objective value is: (numberRegex)
6718:The best solution found is:
9659:The above solution has an objective value of: ([0-9]+|([0-9]+)?\.[0-9]|[0-9]+(e|E)(\+)?[0-9]+)
/tmp/baron_tmp15378/amplmodel.bar - lower bounds, upper bounds, constraints and objective (minimize/maximize) value/expression/equation/inequality with values substituted
/tmp/baron_tmp15378/dictionary.txt - I did not understand much
```
"""
def __init__(self, engine_path: str, engine_options: str, threads: int):
process_name_to_stop_using_ctrl_c = 'baron' # For 1 core and multi core, same process is to be stopped
super().__init__(engine_path, engine_options, process_name_to_stop_using_ctrl_c)
def __baron_extract_output_table(self, std_out_err_file_path: str) -> str:
return run_command_get_output(f"bash output_table_extractor_baron.sh '{std_out_err_file_path}'", '')
def extract_best_solution(self, exec_info: 'NetworkExecutionInformation') -> Tuple[bool, float]:
# Extract the solution from the std_out_err file using the value printed by the AMPL commands:
# option display_precision 0;
# display total_cost;
try:
file_txt = open(exec_info.uniq_std_out_err_file_path, 'r').read()
best_solution = re.search(r'^total_cost\s*=\s*(.*)$', file_txt, re.M).group(1)
best_solution = float(best_solution)
ok = True
if best_solution == 0 or best_solution > 1e40:
g_logger.warning(f"Probably an infeasible solution found by Baron: '{best_solution}'")
g_logger.info(f'Instance={exec_info}')
ok = False
return ok, best_solution
except Exception as e:
g_logger.error(f'CHECKME: {type(e)}, error:\n{e}')
g_logger.debug('Probably, Baron did not terminate immediately even after receiving the appropriate signal')
g_logger.info('Using fallback mechanism to extract the best solution')
csv = self.__baron_extract_output_table(exec_info.uniq_std_out_err_file_path)
if csv == '':
return False, 0.0
lines = csv.split('\n')
lines = [line for line in lines if line != ',' and (not line.startswith('Processing file'))]
g_logger.debug(f'{lines=}')
ok = len(lines) > 0
best_solution = 0.0
if len(lines) > 0:
best_solution = float(lines[-1].split(',')[1])
if best_solution > 1e40:
g_logger.warning(f"Probably an infeasible solution found by Baron: '{lines[-1]}'")
g_logger.info(f'Instance={exec_info}')
ok = False
return ok, best_solution
def check_solution_found(self, exec_info: 'NetworkExecutionInformation') -> bool:
return self.extract_best_solution(exec_info)[0]
def check_errors(self, exec_info: 'NetworkExecutionInformation') -> Tuple[bool, str]:
file_txt = open(exec_info.uniq_std_out_err_file_path, 'r').read()
ok, err_msg = self.ampl_check_errors(file_txt)
if not ok:
return ok, err_msg
try:
err_idx = file_txt.index('Sorry, a demo license is limited to 10 variables')
err_msg = file_txt[err_idx:file_txt.index('exit value 1', err_idx)].replace('\n', ' ').strip()
g_logger.debug(err_msg)
return False, err_msg
except ValueError:
pass # substring not found
try:
err_idx = re.search(r'''Can't\s+find\s+file\s+['"]?.+['"]?''', file_txt).start()
g_logger.debug(file_txt[err_idx:])
return False, file_txt[err_idx:]
except AttributeError as e:
# re.search returned None
g_logger.debug(f'{type(e)}: {e}')
g_logger.debug(f'{exec_info.uniq_std_out_err_file_path=}')
except Exception as e:
g_logger.error(f'FIXME: {type(e)}:\n{e}')
if 'No feasible solution was found' in file_txt:
return False, 'No feasible solution was found'
return True, 'No Errors'
def __baron_extract_solution_file_path(self, exec_info: 'NetworkExecutionInformation') -> Tuple[bool, str]:
file_txt = open(exec_info.uniq_std_out_err_file_path, 'r').read()
solution_dir_name = re.search(r'Retaining scratch directory "/tmp/(.+)"\.', file_txt).group(1)
if solution_dir_name == '':
return False, 'RegEx search failed'
return True, (pathlib.Path(exec_info.aes.OUTPUT_DIR_LEVEL_1_DATA) / solution_dir_name / 'res.lst').resolve()
def extract_solution_vector(self, exec_info: 'NetworkExecutionInformation') -> Tuple[bool, str, float, str]:
ok, file_to_parse = self.__baron_extract_solution_file_path(exec_info)
if not ok:
return False, file_to_parse, float('nan'), 'Failed to extract solution file path'
file_txt = open(file_to_parse, 'r').read()
objective_value = 'NotDefined'
try:
objective_value = re.search(r'The above solution has an objective value of:(.+)', file_txt).group(1).strip()
objective_value = float(objective_value)
except Exception as e:
g_logger.error(f'Exception e:\n{e}')
return False, file_to_parse, float('nan'), f"Objective value (='{objective_value}') RegEx search failed " \
f"(Probably: no feasible solution was found)"
try:
# The lines from '^The best solution found is.+' till the End Of File
# idx = file_txt.index('The best solution found is:')
idx_start = re.search(r'variable\s*xlo\s*xbest\s*xup', file_txt).end()
idx_end = re.search(r'The above solution has an objective value of', file_txt).start()
solution_vector_list = list()
for line in file_txt[idx_start:idx_end].strip().splitlines(keepends=False):
vals = line.strip().split()
solution_vector_list.append(f'{vals[0]}: {vals[2]}') # Variable Name, Best Value
return True, file_to_parse, objective_value, '\n'.join(solution_vector_list)
except AttributeError as e:
# re.search returned None
g_logger.debug(f'{type(e)}: {e}')
g_logger.debug(f'{exec_info.uniq_std_out_err_file_path=}')
return False, file_to_parse, objective_value, 'Solution vector RegEx search failed'
except Exception as e:
g_logger.error(f'FIXME: {type(e)}:\n{e}')
# noinspection PyUnreachableCode
return False, file_to_parse, objective_value, 'FIXME: Unhandled unknown case'
class SolverOutputAnalyzerOcteract(SolverOutputAnalyzerParent):
"""
Perform output analysis (i.e. extract solution, error messages and other necessary information) for Octeract solver
Please refer to Octeract documentation for details:
https://docs.octeract.com/
https://docs.octeract.com/#solver_options
https://docs.octeract.com/so1001-general_solver_settings
https://docplayer.net/187454093-Octeract-engine-user-manual-june-12-2020.html
"""
def __init__(self, engine_path: str, engine_options: str, threads: int):
# For 1 core, process with name 'octeract-engine' is the be stopped using Control+C
# For multi core, process with name 'mpirun' is the be stopped using Control+C
process_name_to_stop_using_ctrl_c = 'octeract-engine'
if threads > 1:
process_name_to_stop_using_ctrl_c = 'mpirun'
super().__init__(engine_path, engine_options, process_name_to_stop_using_ctrl_c)
def __octeract_extract_output_table(self, std_out_err_file_path: str) -> str:
return run_command_get_output(f"bash output_table_extractor_octeract.sh '{std_out_err_file_path}'", '')
def extract_best_solution(self, exec_info: 'NetworkExecutionInformation') -> Tuple[bool, float]:
# Extract the solution from the std_out_err file using the value printed by the AMPL commands:
# option display_precision 0;
# display total_cost;
try:
file_txt = open(exec_info.uniq_std_out_err_file_path, 'r').read()
best_solution = re.search(r'^total_cost\s*=\s*(.*)$', file_txt, re.M).group(1)
best_solution = float(best_solution)
ok = True
if best_solution == 0 or best_solution > 1e40:
g_logger.warning(f"Probably an infeasible solution found by Octeract: '{best_solution}'")
g_logger.info(f'Instance={exec_info}')
ok = False
return ok, best_solution
except Exception as e:
g_logger.error(f'CHECKME: {type(e)}, error:\n{e}')
g_logger.debug('Probably, Octeract did not terminate immediately '
'even after receiving the appropriate signal')
g_logger.info('Using fallback mechanism to extract the best solution')
# Custom checking for exceptional situations
file_txt = open(exec_info.uniq_std_out_err_file_path, 'r').read()
if 'Found solution during preprocessing' in file_txt:
try:
best_solution = float(re.search(r'Objective value at global solution:\s*(.*)\s*', file_txt).group(1))
g_logger.debug(f'Solver found the solution during preprocessing, {best_solution=}')
return True, best_solution
except:
g_logger.error(f'FIXME: Found solution during preprocessing. But, failed to extract the objective '
f'function value. Few lines from {exec_info.uniq_std_out_err_file_path=}:')
idx_found_solution = file_txt.find('Found solution during preprocessing')
g_logger.debug(file_txt[file_txt.rfind('\n', 0, idx_found_solution - 50) + 1:
file_txt.find('\n', idx_found_solution + 120)])
pass
# Use bash script to extract the values from the table printed to output by Octeract
csv = self.__octeract_extract_output_table(exec_info.uniq_std_out_err_file_path)
if csv == '':
return False, 0.0
lines = csv.split('\n')
lines = [line for line in lines if line != ',' and (not line.startswith('Processing file'))]
g_logger.debug(f'{lines=}')
status = len(lines) > 0
best_solution = 0.0
g_logger.info(f'Instance={exec_info}')
if len(lines) > 0:
last_line_splitted = lines[-1].split(',')
if len(last_line_splitted) > 2:
if last_line_splitted[-1] == '(I)':
g_logger.warning(f"Infeasible solution found by Octeract: '{lines[-1]}'")
status = False
else:
g_logger.warning(f"FIXME: Unhandled unknown case, probably an infeasible "
f"solution found by Octeract: '{lines[-1]}'")
else:
best_solution = float(last_line_splitted[1])
g_logger.debug((status, best_solution))
return status, best_solution
def check_solution_found(self, exec_info: 'NetworkExecutionInformation') -> bool:
return self.extract_best_solution(exec_info)[0]
def check_errors(self, exec_info: 'NetworkExecutionInformation') -> Tuple[bool, str]:
file_txt = open(exec_info.uniq_std_out_err_file_path, 'r').read()
if 'Found solution during preprocessing' in file_txt:
return True, 'Solution found during preprocessing'
if 'Iteration GAP LLB BUB Pool Time Mem' in file_txt:
return True, 'Probably No Errors'
ok, err_msg = self.ampl_check_errors(file_txt)
if not ok:
return ok, err_msg
try:
err_idx = re.search(r'''Can't\s+find\s+file\s+['"]?.+['"]?''', file_txt).start()
g_logger.debug(file_txt[err_idx:])
return False, file_txt[err_idx:]
except IndexError as e:
g_logger.error(f'{type(e)}: {e}')
g_logger.debug(f'{exec_info.uniq_std_out_err_file_path=}')
except AttributeError as e:
# re.search failed, returned `None`
g_logger.debug(f'{type(e)}: {e}')
g_logger.debug(f'{exec_info.uniq_std_out_err_file_path=}')
except Exception as e:
g_logger.error(f'FIXME: {type(e)}:\n{e}')
err_idx = None
try:
err_idx = file_txt.index('Request_Error')
err_msg = file_txt[err_idx:file_txt.index('exit value 1', err_idx)].replace('\n', ' ').strip()
g_logger.debug(err_msg)
return False, err_msg
except ValueError:
if err_idx is not None:
g_logger.debug(file_txt[err_idx:])
return False, file_txt[err_idx:]
pass # substring not found
# Octeract solver failed to establish connection to their server. Hence, it does not proceed
# with the solving.
err_idx = None
try:
err_idx = file_txt.index('Error: Failed to establish connection to server.')
if ("----------------------------------------------------------------------"
"--------------------------" not in file_txt) or "can't open /tmp/at" in file_txt:
err_msg = file_txt[err_idx:file_txt.index('ampl:', err_idx)].replace('\n', ' ').strip()
g_logger.debug(err_msg)
return False, err_msg
else:
g_logger.debug(f'CHECKME: {file_txt[err_idx:]=}')
except ValueError:
if err_idx is not None:
g_logger.debug(file_txt[err_idx:])
return False, file_txt[err_idx:]
pass # substring not found
try:
err_idx = file_txt.index('presolve messages suppressed')
if '_total_solve_time' in file_txt:
err_msg = file_txt[:file_txt.index('_total_solve_time')]
else:
err_msg = file_txt[:err_idx + len('presolve messages suppressed') + 1]
err_msg = err_msg.strip()
g_logger.debug(err_msg)
return False, err_msg
except ValueError:
pass # substring not found
try:
if 'all variables eliminated, but lower bound' in file_txt:
err_idx = file_txt.index('_total_solve_time')
err_msg = file_txt[:err_idx]
g_logger.debug(err_msg)
return False, err_msg
except ValueError:
pass # substring not found
return True, 'No Errors'
def __octeract_extract_solution_file_path(self, exec_info: 'NetworkExecutionInformation') -> Tuple[bool, str]:
file_txt = open(exec_info.uniq_std_out_err_file_path, 'r').read()
solution_file_name = re.search(r'Solution file written to: /tmp/(.+)', file_txt).group(1)
if solution_file_name == '':
g_logger.debug(f"FIXME: log file path='{exec_info.uniq_std_out_err_file_path}', file_txt:\n{file_txt}")
return False, 'RegEx search failed'
return True, (pathlib.Path(exec_info.aes.OUTPUT_DIR_LEVEL_1_DATA) / solution_file_name).resolve()
def extract_solution_vector(self, exec_info: 'NetworkExecutionInformation') -> Tuple[bool, str, float, str]:
ok, file_to_parse = self.__octeract_extract_solution_file_path(exec_info)
if not ok:
return False, file_to_parse, float('nan'), ''
json_data = json.loads(open(file_to_parse, 'r').read())
if not json_data['feasible']:
return False, file_to_parse, json_data['objective_value'], json_data['statistics']['dgo_exit_status']
objective_value: float = json_data['objective_value']
solution_vector = '\n'.join(
[f'{i}: {j}' for i, j in sorted(json_data['solution_vector'].items(), key=lambda kv: (len(kv[0]), kv[0]))]
)
return True, file_to_parse, objective_value, solution_vector
# ---
class NetworkExecutionInformation:
def __init__(self, aes: 'AutoExecutorSettings', idx: int):
"""
This constructor will set all data members except `self.tmux_bash_pid`
"""
self.tmux_bash_pid: Union[str, None] = None # This has to be set manually
self.idx: int = idx
self.aes: 'AutoExecutorSettings' = aes
self.solver_name, self.model_name = aes.solver_model_combinations[idx]
self.solver_info: SolverOutputAnalyzerParent = aes.solvers[self.solver_name]
# REFER: https://stackoverflow.com/a/66771847
self.short_uniq_model_name: str = self.model_name[:self.model_name.find('_')]
# REFER: https://github.com/tmux/tmux/issues/3113
# Q. What is the maximum length of session name that can be set using
# the following command: `tmux new -s 'SessionName_12345'`
# A. There is no defined limit.
self.short_uniq_data_file_name: str = aes.data_file_hash
self.short_uniq_combination: str = f'{self.solver_name}_' \
f'{self.short_uniq_model_name}_{self.short_uniq_data_file_name}'
g_logger.debug((type(self.short_uniq_model_name), type(self.short_uniq_data_file_name),
type(self.short_uniq_combination)))
g_logger.debug((self.short_uniq_model_name, self.short_uniq_data_file_name, self.short_uniq_combination))
self.models_dir: str = aes.models_dir
self.data_file_path: str = aes.data_file_path
self.engine_path: str = aes.solvers[self.solver_name].engine_path
self.engine_options: str = aes.solvers[self.solver_name].engine_options
self.uniq_exec_output_dir: pathlib.Path = \
pathlib.Path(aes.output_dir_level_1_network_specific) / self.short_uniq_combination
self.uniq_tmux_session_name: str = f'{aes.TMUX_UNIQUE_PREFIX}{self.short_uniq_combination}'
self.uniq_pid_file_path: str = f'/tmp/pid_{self.short_uniq_combination}.txt'
self.uniq_std_out_err_file_path: str = f'{self.uniq_exec_output_dir.resolve()}/std_out_err.txt'
def __str__(self):
return f'NetworkExecutionInformation[pid={self.tmux_bash_pid}, idx={self.idx}, solver={self.solver_name}, ' \
f'model={self.short_uniq_model_name}]'
def __repr__(self):
# REFER: https://stackoverflow.com/questions/727761/python-str-and-lists
# REFER: https://docs.python.org/2/reference/datamodel.html#object.__repr__
return self.__str__()
# ---
class AutoExecutorSettings:
# Level 0 is main directory inside which everything will exist
OUTPUT_DIR_LEVEL_0 = './NetworkResults/'.rstrip('/') # Note: Do not put trailing forward slash ('/')
OUTPUT_DIR_LEVEL_1_DATA = f'{OUTPUT_DIR_LEVEL_0}/SolutionData'
# Please ensure that proper escaping of white spaces and other special characters
# is done because this will be executed in a fashion similar to `./a.out`
AMPL_PATH = './ampl.linux-intel64/ampl'
AVAILABLE_SOLVERS = ['baron', 'octeract'] # NOTE: Also look at `__update_solver_dict()` method when updating this
AVAILABLE_MODELS = {1: 'm1_basic.R', 2: 'm2_basic2_v2.R', 3: 'm3_descrete_segment.R', 4: 'm4_parallel_links.R'}
TMUX_UNIQUE_PREFIX = f'AR_NC_{os.getpid()}_' # AR = Auto Run, NC = Network Cost
def __init__(self):
self.debug = False
self.r_cpu_cores_per_solver = 1
# 48 core server is being used
self.r_max_parallel_solvers = 44
# Time is in seconds, set this to any value <= 0 to ignore this parameter
self.r_execution_time_limit = (0 * 60 * 60) + (5 * 60) + 0
self.r_min_free_ram = 2 # GiB
self.r_min_free_swap = 8 # GiB, usefulness of this variable depends on the swappiness of the system
self.models_dir = "./Files/Models" # m1, m3 => q , m2, m4 => q1, q2
self.solvers: Dict[str, SolverOutputAnalyzerParent] = {}
self.__update_solver_dict()
# Tuples of (Solver name & Model name) which are to be executed to
# find the cost of the given graph/network (i.e. data/testcase file)
self.solver_model_combinations: List[Tuple[str, str]] = list()
# Path to graph/network (i.e. data/testcase file)
self.data_file_path: str = ''
self.data_file_hash: str = ''
self.output_dir_level_1_network_specific: str = ''
self.output_network_specific_result: str = ''
self.output_result_summary_file: str = ''
def __update_solver_dict(self):
# NOTE: Update `AutoExecutorSettings.AVAILABLE_SOLVERS` if keys in below dictionary are updated
# NOTE: Use double quotes ONLY in the below variables
self.solvers = {
'baron': SolverOutputAnalyzerBaron(
engine_path='./ampl.linux-intel64/baron',
engine_options=f'option baron_options "threads={self.r_cpu_cores_per_solver} '
f'barstats keepsol lsolmsg outlev=1 prfreq=100 prtime=2 problem";',
threads=self.r_cpu_cores_per_solver
),
'octeract': SolverOutputAnalyzerOcteract(
engine_path='./octeract-engine-4.0.0/bin/octeract-engine',
engine_options=f'options octeract_options "num_cores={self.r_cpu_cores_per_solver}";',
threads=self.r_cpu_cores_per_solver
)
}
def set_execution_time_limit(self, hours: int = None, minutes: int = None, seconds: int = None) -> None:
if (hours, minutes, seconds).count(None) == 3:
g_logger.warning('At least one value should be non-None to update EXECUTION_TIME_LIMIT')
return
hours = 0 if hours is None else hours
minutes = 0 if minutes is None else minutes
seconds = 0 if seconds is None else seconds
self.r_execution_time_limit = (hours * 60 * 60) + (minutes * 60) + seconds
self.__update_solver_dict()
def set_cpu_cores_per_solver(self, n: int) -> None:
self.r_cpu_cores_per_solver = n
self.__update_solver_dict()
def set_data_file_path(self, data_file_path: str) -> None:
self.data_file_path = data_file_path
self.data_file_hash = file_hash_sha256(data_file_path)
self.output_dir_level_1_network_specific = f'{AutoExecutorSettings.OUTPUT_DIR_LEVEL_0}' \
f'/{self.data_file_hash}'
self.output_network_specific_result = self.output_dir_level_1_network_specific + '/0_result.txt'
self.output_result_summary_file = self.output_dir_level_1_network_specific + '/0_result_summary.txt'
def start_solver(self, idx: int) -> NetworkExecutionInformation:
"""
Launch the solver using `tmux` and `AMPL` in background (i.e. asynchronously / non-blocking)
Args:
idx: Index of `self.solver_model_combinations`
Returns:
`class NetworkExecutionInformation` object which has all the information regarding the execution
"""
info = NetworkExecutionInformation(self, idx)
info.uniq_exec_output_dir.mkdir(exist_ok=True)
if not info.uniq_exec_output_dir.exists():
g_logger.warning(f"Some directory(s) do not exist in the path: '{info.uniq_exec_output_dir.resolve()}'")
info.uniq_exec_output_dir.mkdir(parents=True, exist_ok=True)
# REFER: https://stackoverflow.com/questions/2500436/how-does-cat-eof-work-in-bash
# 📝 'EOF' should be the only word on the line without any space before and after it.
# NOTE: The statement `echo > /dev/null` is required to make the below command work. Without
# it, AMPL is not started. Probably, it has something to do with the `EOF` thing.
# NOTE: The order of > and 2>&1 matters in the below command
# REFER: https://github.com/fenilgmehta/Jaltantra-Code-and-Scripts/blob/main/Files/main.run
# For AMPL commands
run_command_get_output(rf'''
tmux new-session -d -s '{info.uniq_tmux_session_name}' '
echo $$ > "{info.uniq_pid_file_path}"
{self.AMPL_PATH} > "{info.uniq_std_out_err_file_path}" 2>&1 <<EOF
reset;
model "{info.models_dir}/{info.model_name}";
data "{info.data_file_path}";
option solver "{info.engine_path}";
option presolve_eps 1e-9;
{info.engine_options};
solve;
''' + r'''
display _total_solve_time;
option display_1col 9223372036854775807;
option display_precision 6;
display {i in nodes} h[i];
display {(i,j) in arcs} ''' + ("q[i,j]" if (info.short_uniq_model_name in ("m1", "m3")) else "(q1[i,j], q2[i,j])") + ''';
option display_eps 1e-4;
option omit_zero_rows 1;
display {(i,j) in arcs, k in pipes} l[i,j,k];
display _total_solve_time;
option display_precision 0;
display total_cost;
EOF
echo > /dev/null
'
''', debug_print=self.debug)
# At max we wait for 60 seconds
pid_file_wait_time = 0
while (not os.path.exists(info.uniq_pid_file_path)) and (pid_file_wait_time < 60):
g_logger.info('PID file not generated. Sleeping for 1 second...')
pid_file_wait_time += 1
time.sleep(1)
if os.path.exists(info.uniq_pid_file_path):
info.tmux_bash_pid = run_command_get_output(f'cat "{info.uniq_pid_file_path}"')
else:
g_logger.error(f'FIXME: CHECKME: PID file not created for {info=}')
info.tmux_bash_pid = 0
return info
# ---
class MonitorAndStopper:
@staticmethod
def mas_time(
tmux_monitor_list: List[NetworkExecutionInformation],
tmux_finished_list: List[NetworkExecutionInformation],
execution_time_limit: float,
blocking: bool
) -> None:
"""
Monitor and stop solver instances based on the time for which they have been running on the system
Args:
tmux_monitor_list: List of Solver instances which are to be monitored
tmux_finished_list: List of Solver instances which have been stopped in this iteration (initially this will be empty)
execution_time_limit: Time in seconds. (This should be > 0)
blocking: Wait until one of the solver instance in `tmux_monitor_list` is stopped
"""
if execution_time_limit <= 0.0:
g_logger.error(f'FIXME: `execution_time_limit` is not greater than 0')
return
# Index of elements of `tmux_monitor_list` which were/have stopped.
tmux_finished_list_idx: List[int] = list()
to_run_the_loop = True
while to_run_the_loop:
to_run_the_loop = blocking
for i, ne_info in enumerate(tmux_monitor_list):
if get_execution_time(ne_info.tmux_bash_pid) < execution_time_limit:
continue
# NOTE: only SIGINT signal (i.e. Ctrl+C) does proper termination of the octeract-engine
g_logger.debug(run_command_get_output(
f"pstree -ap {ne_info.tmux_bash_pid} # Time 1", debug_print=True
))
g_logger.debug(run_command_get_output(
f"pstree -ap {ne_info.tmux_bash_pid} | "
f"grep -oE '{ne_info.solver_info.process_name_to_stop_using_ctrl_c},[0-9]+' # Time 2"
))
g_logger.debug(run_command_get_output(
f"pstree -aps {ne_info.tmux_bash_pid} | "
f"grep -oE '{ne_info.solver_info.process_name_to_stop_using_ctrl_c},[0-9]+' # Time 3"
))
success, pid = run_command(
f"pstree -ap {ne_info.tmux_bash_pid} | "
f"grep -oE '{ne_info.solver_info.process_name_to_stop_using_ctrl_c},[0-9]+' | "
f"grep -oE '[0-9]+' # Time Monitor 4",
'0',
True
)
tmux_finished_list_idx.append(i)
tmux_finished_list.append(ne_info)
to_run_the_loop = False
if success:
g_logger.info(run_command_get_output(f'kill -s SIGINT {pid} # Time Monitor', debug_print=True))
else:
g_logger.info(f'TIME_LIMIT: tmux session (with bash PID={ne_info.tmux_bash_pid}) already finished')
time.sleep(2)
for i in tmux_finished_list_idx[::-1]:
tmux_monitor_list.pop(i)
time.sleep(2)
g_logger.debug(f'{tmux_finished_list_idx=}')
pass
def check_solution_status(tmux_monitor_list: List[NetworkExecutionInformation]) -> bool:
"""Return True if a feasible solution has been found by any one of the tmux session (i.e. solver-model combination)"""
for info in tmux_monitor_list:
if info.solver_info.check_solution_found(info):
return True
return False
def extract_best_solution(
tmux_monitor_list: List[NetworkExecutionInformation],
result_summary_file_path: str
) -> Tuple[bool, float, Optional[NetworkExecutionInformation]]:
"""
Args:
tmux_monitor_list: List of `NetworkExecutionInformation` which have finished their execution
Returns:
ok, best solution, context of solver and model which found the best solution
"""
all_results: List = list() # Only used for debugging
best_result_till_now, best_result_exec_info = float('inf'), None
for exec_info in tmux_monitor_list:
ok, curr_res = exec_info.solver_info.extract_best_solution(exec_info)
all_results.append((exec_info.solver_name, exec_info.short_uniq_model_name, ok, curr_res))
g_logger.info(f'solver={exec_info.solver_name}, model={exec_info.short_uniq_model_name}, {ok=}, {curr_res=}')
# `if` solution not found by this solver instance `or` a better solution is already known, then `continue`
if not ok or curr_res >= best_result_till_now:
continue
g_logger.debug(f'Update best result seen till now: {curr_res} < {best_result_till_now=}')
best_result_till_now = curr_res
best_result_exec_info = exec_info
for (solver_name, model_name, ok, res) in all_results:
g_logger.info(f'solver={solver_name}, model={model_name}, {ok=}, {res=}')
run_command(f"echo 'solver={solver_name}, model={model_name}, {ok=}, {res=}'"
f" >> '{result_summary_file_path}'")
return best_result_exec_info is not None, best_result_till_now, best_result_exec_info
# ---
def main(my_settings: AutoExecutorSettings) -> None:
"""Launch the solvers and monitor them based on CLI parameters"""
# Start initialization of tmux session monitoring lists
tmux_original_list: List[NetworkExecutionInformation] = list()
tmux_monitor_list: List[NetworkExecutionInformation] = list()
tmux_finished_list: List[NetworkExecutionInformation] = list()
main_initialize_directories(my_settings)
main_write_requests_metadata(my_settings)
# Decide how many solvers to start in the first batch
min_combination_parallel_solvers: int = min(
len(my_settings.solver_model_combinations),
my_settings.r_max_parallel_solvers
)
# Begin execution of first batch
main_start_first_batch(my_settings, tmux_original_list, tmux_monitor_list, min_combination_parallel_solvers)
# Error checking - Round 1
# This is done to find and log the errors that occurred just after launching the tmux sessions
main_error_checking_round_1(tmux_monitor_list, tmux_finished_list)
# If all (tmux) sessions of the first batch stopped because of some error, then exit the program
main_check_launch_errors(my_settings, tmux_monitor_list, tmux_finished_list)
main_busy_waiting(my_settings, tmux_monitor_list, tmux_finished_list)
# Error checking - Round 2
main_error_checking_round_2(tmux_original_list)