1- import optuna
2- import requests
3- import time
4- import pandas as pd
5- import os
61import json
2+ import os
3+ import time
4+
75import numpy as np
6+ import optuna
7+ import pandas as pd
8+ import requests
89from rich .console import Console
9- from rich .prompt import Prompt , IntPrompt , Confirm
10- from rich .table import Table
1110from rich .progress import (
11+ BarColumn ,
1212 Progress ,
1313 SpinnerColumn ,
14- TimeElapsedColumn ,
15- BarColumn ,
1614 TextColumn ,
15+ TimeElapsedColumn ,
1716)
17+ from rich .prompt import Confirm , IntPrompt , Prompt
18+ from rich .table import Table
19+
1820
1921# Initialize rich console
2022console = Console ()
2527device_ip = Prompt .ask ("Enter Bitaxe device URI" , default = "192.168.1.4" )
2628study_name = Prompt .ask ("Enter trial name" , default = "espmineroptim" )
2729n_trials = IntPrompt .ask ("Enter number of trials" , default = 10 , show_default = True )
28- trial_length_s = (
29- IntPrompt .ask ("Enter trial duration (min.)" , default = 1 , show_default = True ) * 60
30- )
30+ trial_length_s = IntPrompt .ask ("Enter trial duration (min.)" , default = 1 , show_default = True ) * 60
3131
3232# Frequency bounds
33- min_frequency_MHz = IntPrompt .ask (
34- "Enter minimum frequency (MHz)" , default = 400 , show_default = True
35- )
36- max_frequency_MHz = IntPrompt .ask (
37- "Enter maximum frequency (MHz)" , default = 625 , show_default = True
38- )
33+ min_frequency_MHz = IntPrompt .ask ("Enter minimum frequency (MHz)" , default = 400 , show_default = True )
34+ max_frequency_MHz = IntPrompt .ask ("Enter maximum frequency (MHz)" , default = 625 , show_default = True )
3935
4036# Voltage bounds
41- min_coreVoltage_mV = IntPrompt .ask (
42- "Enter minimum coreVoltage (mV)" , default = 1000 , show_default = True
43- )
44- max_coreVoltage_mV = IntPrompt .ask (
45- "Enter maximum coreVoltage (mV)" , default = 1250 , show_default = True
46- )
37+ min_coreVoltage_mV = IntPrompt .ask ("Enter minimum coreVoltage (mV)" , default = 1000 , show_default = True )
38+ max_coreVoltage_mV = IntPrompt .ask ("Enter maximum coreVoltage (mV)" , default = 1250 , show_default = True )
4739
4840limit_temp_degC = IntPrompt .ask ("Enter temp limit (°C)" , default = 68 , show_default = True )
49- limit_vrTemp_degC = IntPrompt .ask (
50- "Enter voltage regulator temp limit coreVoltage (°C)" , default = 78 , show_default = True
51- )
41+ limit_vrTemp_degC = IntPrompt .ask ("Enter voltage regulator temp limit coreVoltage (°C)" , default = 78 , show_default = True )
5242confirmed = Confirm .ask ("Check your inputs above. Start optimizing?" )
5343if not confirmed :
5444 exit (0 )
@@ -98,19 +88,15 @@ def run_trial(frequency_MHz, coreVoltage_mV, trial_number):
9888 headers = {"Content-Type" : "application/json" }
9989 payload = {"frequency" : int (frequency_MHz ), "coreVoltage" : int (coreVoltage_mV )}
10090
101- response = requests .patch (
102- SETTINGS_URL , headers = headers , data = json .dumps (payload ), timeout = 10
103- )
91+ response = requests .patch (SETTINGS_URL , headers = headers , data = json .dumps (payload ), timeout = 10 )
10492 response .raise_for_status ()
10593 time .sleep (1 )
10694
10795 console .print ("[cyan]→ Restarting device...[/cyan]" )
10896 restart_response = requests .post (RESET_URL , timeout = 10 )
10997 restart_response .raise_for_status ()
11098
111- console .print (
112- "[yellow]⏳ Waiting 30 seconds for system stabilization...[/yellow]"
113- )
99+ console .print ("[yellow]⏳ Waiting 30 seconds for system stabilization...[/yellow]" )
114100 time .sleep (30 )
115101
116102 hashRates_THps = []
@@ -123,9 +109,7 @@ def run_trial(frequency_MHz, coreVoltage_mV, trial_number):
123109 TimeElapsedColumn (),
124110 transient = True ,
125111 ) as progress :
126- task = progress .add_task (
127- "[green]Collecting system stats..." , total = trial_length_s // 10
128- )
112+ task = progress .add_task ("[green]Collecting system stats..." , total = trial_length_s // 10 )
129113
130114 for _ in range (trial_length_s // 10 ):
131115 stats_response = requests .get (STATS_URL , timeout = 10 )
@@ -139,17 +123,11 @@ def run_trial(frequency_MHz, coreVoltage_mV, trial_number):
139123 actual_coreVoltage_mV = stats .get ("coreVoltage" , 0 )
140124
141125 try :
142- np .testing .assert_allclose (
143- actual_frequency_MHz , frequency_MHz , rtol = 1e-3
144- )
145- np .testing .assert_allclose (
146- actual_coreVoltage_mV , coreVoltage_mV , rtol = 1e-3
147- )
126+ np .testing .assert_allclose (actual_frequency_MHz , frequency_MHz , rtol = 1e-3 )
127+ np .testing .assert_allclose (actual_coreVoltage_mV , coreVoltage_mV , rtol = 1e-3 )
148128 except AssertionError :
149129 console .print_exception ()
150- console .print (
151- "[bold red]Real parameter not set within tolerance of 1%[/bold red]"
152- )
130+ console .print ("[bold red]Real parameter not set within tolerance of 1%[/bold red]" )
153131 console .print ()
154132 return
155133
@@ -159,9 +137,7 @@ def run_trial(frequency_MHz, coreVoltage_mV, trial_number):
159137 )
160138
161139 if temp_degC > limit_temp_degC or vrTemp_degC > limit_vrTemp_degC :
162- console .print (
163- "[bold red]❌ Temperature too high! Aborting.[/bold red]"
164- )
140+ console .print ("[bold red]❌ Temperature too high! Aborting.[/bold red]" )
165141 return
166142
167143 hashRates_THps .append (hashRate_THps )
@@ -175,9 +151,7 @@ def run_trial(frequency_MHz, coreVoltage_mV, trial_number):
175151
176152 avg_hashRate_THps = sum (hashRates_THps ) / len (hashRates_THps )
177153 avg_power_W = sum (powers_W ) / len (powers_W )
178- avg_efficiency_JpTH = (
179- avg_power_W / avg_hashRate_THps if avg_hashRate_THps != 0 else 0
180- )
154+ avg_efficiency_JpTH = avg_power_W / avg_hashRate_THps if avg_hashRate_THps != 0 else 0
181155 # scoring = (
182156 # hashRate_factor * avg_hashRate_THps
183157 # - efficiency_factor * avg_efficiency_JpTH
@@ -220,20 +194,16 @@ def run_trial(frequency_MHz, coreVoltage_mV, trial_number):
220194
221195
222196def run_study (trial ):
223- frequency_MHz = trial .suggest_float (
224- "frequency" , float (min_frequency_MHz ), float (max_frequency_MHz )
225- )
226- coreVoltage_mV = trial .suggest_float (
227- "coreVoltage" , float (min_coreVoltage_mV ), float (max_coreVoltage_mV )
228- )
197+ frequency_MHz = trial .suggest_float ("frequency" , float (min_frequency_MHz ), float (max_frequency_MHz ))
198+ coreVoltage_mV = trial .suggest_float ("coreVoltage" , float (min_coreVoltage_mV ), float (max_coreVoltage_mV ))
229199 return run_trial (frequency_MHz , coreVoltage_mV , trial .number )
230200
231201
232202if __name__ == "__main__" :
233203 console .print ("[bold green]Starting Bitaxe Optimization...[/bold green]" )
234204 study = optuna .create_study (
235205 directions = ["maximize" , "minimize" ],
236- storage = "sqlite:///db.sqlite3" , # Specify the storage URL here.
206+ storage = "sqlite:///espminer-optim- db.sqlite3" , # Specify the storage URL here.
237207 study_name = study_name ,
238208 load_if_exists = True ,
239209 )
0 commit comments