changeset 12:49939e179c86

Run 4 simulations at once. Since this requires the function arguments to be pickleable we pass the DB filename around.
author Daniel O'Connor <darius@dons.net.au>
date Sat, 18 Nov 2023 12:03:13 +1030
parents 8d34a9eec184
children 7cdfaa0b5ad2
files sim.py
diffstat 1 files changed, 19 insertions(+), 11 deletions(-) [+]
line wrap: on
line diff
--- a/sim.py	Sat Nov 18 10:37:35 2023 +1030
+++ b/sim.py	Sat Nov 18 12:03:13 2023 +1030
@@ -30,6 +30,7 @@
     cmd.append(runname.name)
     #print(' '.join(map(shlex.quote, cmd)))
     then = datetime.datetime.now()
+    print(f'Starting run {runname} at {then}')
     p = subprocess.Popen(cmd, cwd = rundir)
     p.communicate()
     now = datetime.datetime.now()
@@ -80,7 +81,7 @@
 
     return cost
 
-def fn(v, cur, simexe, simflags, rundir, circ, ctr):
+def fn(v, dsn, simexe, simflags, rundir, circ):
     '''Called by differential_evolution, v contains the evolved parameters, the rest are passed in from the args parameter'.
     Returns the cost value which is being minimised'''
 
@@ -88,6 +89,8 @@
     duty, c1, c2, l1, l2 = v
 
     # Check if this combination has already been tried
+    dbh = sqlite3.connect(dsn)
+    cur = dbh.cursor()
     cur.execute('SELECT run, power, efficiency, thd, ipeak FROM GAN190 WHERE duty = ? AND c1 = ? AND c2 = ? AND l1 = ? AND l2 = ?', (duty, c1, c2, l1, l2))
     tmp = cur.fetchone()
     if tmp is not None:
@@ -98,7 +101,10 @@
         return cost
 
     # Get next run number
-    run = next(ctr)
+    cur.execute('BEGIN DEFERRED')
+    cur.execute('INSERT INTO GAN190 DEFAULT VALUES RETURNING rowid')
+    run = cur.fetchone()[0]
+    cur.execute('COMMIT')
 
     # Run the simulation
     # Need to convert units to suit
@@ -111,7 +117,8 @@
     # Log & save the results
     print(f'Run {run:3d}: Duty {duty:3.0f}%, C1 {c1:3.0f}pF, C2 {c2:3.0f}pF, L1 {l1:3.0f}uH, L2 {l2:4.0f}nH -> Power: {power:6.1f}W Efficiency: {eff:5.1f}% THD: {thd:5.1f}% IPeak: {ipeak:4.1f}A Cost: {cost:6.2f}')
     taken = taken.seconds + taken.microseconds / 1e6
-    cur.execute('INSERT INTO GAN190 (name, run, start, duration, duty, c1, c2, l1, l2, power, efficiency, thd, ipeak, cost) VALUES (?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?)',
+    cur.execute('BEGIN DEFERRED')
+    cur.execute('REPLACE INTO GAN190 (name, run, start, duration, duty, c1, c2, l1, l2, power, efficiency, thd, ipeak, cost) VALUES (?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?)',
                  (runname, run, then, taken, duty, c1, c2, l1, l2, power, eff, thd, ipeak, cost))
     cur.execute('COMMIT')
 
@@ -119,14 +126,19 @@
 
 def ev():
     # Bounds for parameters
+    # Note that the parameters are also constrained to be integral
     bounds = [(10, 80),
               (1, 20),
               (10, 300),
               (1, 20),
               (10, 500)]
+
     # Initial solution
     x0 = [36, 10, 155, 5, 140]
-    dbh = sqlite3.connect('results.db')
+
+    # Where to save results
+    dsn = 'results.db'
+    dbh = sqlite3.connect(dsn)
     cur = dbh.cursor()
     cur.execute('''
 CREATE TABLE IF NOT EXISTS GAN190 (
@@ -145,11 +157,7 @@
     ipeak	REAL,		-- Peak drain current (A)
     cost	REAL		-- Calculated cost metric
 );''')
-    def ctr():
-        i = 0
-        while True:
-            yield i
-            i += 1
+
     return differential_evolution(fn, bounds, x0 = x0,
-                                  args = (cur, '/Users/oconnd1/bin/runltspice', ['-alt'], 'tmp', 'pa-GAN190-PP-nodriver.net', ctr()),
-                                  integrality = True, disp = True, seed = 12345)
+                                  args = (dsn, '/Users/oconnd1/bin/runltspice', [], 'tmp', 'pa-GAN190-PP-nodriver.net'),
+                                  integrality = True, disp = True, seed = 12345, updating = 'deferred', workers = 4)