Mercurial > ~darius > hgwebdir.cgi > pa
changeset 11:8d34a9eec184
Split out cost function so we can re-run it on DB data to allow faster adjustment.
author | Daniel O'Connor <darius@dons.net.au> |
---|---|
date | Sat, 18 Nov 2023 10:37:35 +1030 |
parents | 2832aefd442c |
children | 49939e179c86 |
files | sim.py |
diffstat | 1 files changed, 34 insertions(+), 27 deletions(-) [+] |
line wrap: on
line diff
--- a/sim.py Fri Nov 17 23:47:05 2023 +1030 +++ b/sim.py Sat Nov 18 10:37:35 2023 +1030 @@ -52,38 +52,15 @@ return then, taken, power, eff, thd, ipeak - -def fn(v, cur, simexe, simflags, rundir, circ, ctr): - '''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''' - - # Get parameters for run - duty, c1, c2, l1, l2 = v - - cur.execute('SELECT cost, 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: - cost, run, power, eff, thd, ipeak = tmp - print(f'Found 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}') - return cost - - # Get next run number - run = next(ctr) - - # Run the simulation - # Need to convert units to suit - runname = pathlib.Path(circ).stem + '-' + str(run) + '.net' - then, taken, power, eff, thd, ipeak = simulate(circ, simexe, simflags, rundir, runname, - {'dutypct' : duty, 'C1' : c1 * 1e-12, 'C2' : c2 * 1e-12, 'L1' : l1 * 1e-6, 'L2' : l2 * 1e-9}) - +def calccost(power, eff, thd, ipeak): # Calculate the cost - tpwr = 2000 + tpwr = 1500 tthd = 2 teff = 90 imax = 11 cost = 0 - if power < tpwr / 2: + if power < tpwr * 0.80: cost += 100 elif power < tpwr: cost += (tpwr - power) / tpwr / 100 @@ -99,7 +76,37 @@ cost += (teff - eff) / teff if ipeak > imax: - cost += (ipeak - imax) * 2 + cost += (ipeak - imax) * 5 + + return cost + +def fn(v, cur, simexe, simflags, rundir, circ, ctr): + '''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''' + + # Get parameters for run + duty, c1, c2, l1, l2 = v + + # Check if this combination has already been tried + 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: + run, power, eff, thd, ipeak = tmp + # Recalculate the cost since it is cheap and might have been tweaked since the original run + cost = calccost(power, eff, thd, ipeak) + print(f'Found 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}') + return cost + + # Get next run number + run = next(ctr) + + # Run the simulation + # Need to convert units to suit + runname = pathlib.Path(circ).stem + '-' + str(run) + '.net' + then, taken, power, eff, thd, ipeak = simulate(circ, simexe, simflags, rundir, runname, + {'dutypct' : duty, 'C1' : c1 * 1e-12, 'C2' : c2 * 1e-12, 'L1' : l1 * 1e-6, 'L2' : l2 * 1e-9}) + # Calculate the cost + cost = calccost(power, eff, thd, ipeak) # 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}')