PuLP print decision variable results that are Non-Zero only? - pulp

Currently I am using PuLP for a retail distribution problem. CZe represents a customer, where the indexed DC or S1,S2 etc. is a location that services the demand of that customer. I want to find a way to print only decision variables that have a non-zero allocation amount... if possible.
Service_1_('CZe_0004',_'DC_CA_FRESNO') = 0.0
Service_1_('CZe_0004',_'DC_IN_GREENWOOD') = 0.0
Service_1_('CZe_0004',_'DC_PA_CHAMBERSBURG') = 0.01
Service_1_('CZe_0004',_'DC_TX_DALLAS') = 0.0
Service_1_('CZe_0004',_'MFC_FL_JACKSONVILLE') = 0.0
Service_1_('CZe_0004',_'MFC_IL_ROMEOVILLE') = 0.0
Service_1_('CZe_0004',_'MFC_SC_GREER') = 0.0
Service_1_('CZe_0004',_'S1') = 0.0
Service_1_('CZe_0004',_'S10') = 0.0
Service_1_('CZe_0004',_'S11') = 0.0
Service_1_('CZe_0004',_'S12') = 0.0
Service_1_('CZe_0004',_'S13') = 0.0
Service_1_('CZe_0004',_'S14') = 0.0
Service_1_('CZe_0004',_'S15') = 0.0
Service_1_('CZe_0004',_'S16') = 0.0
Service_1_('CZe_0004',_'S17') = 0.0
Service_1_('CZe_0004',_'S18') = 0.0
Service_1_('CZe_0004',_'S19') = 0.0
Service_1_('CZe_0004',_'S2') = 0.0
Service_1_('CZe_0004',_'S20') = 0.0
Service_1_('CZe_0004',_'S21') = 0.0
Service_1_('CZe_0004',_'S22') = 0.0
Service_1_('CZe_0004',_'S23') = 0.0
Service_1_('CZe_0004',_'S24') = 0.0
Service_1_('CZe_0004',_'S25') = 0.0
Service_1_('CZe_0004',_'S26') = 0.0
Service_1_('CZe_0004',_'S27') = 0.0
Service_1_('CZe_0004',_'S3') = 0.0
Service_1_('CZe_0004',_'S4') = 0.0
Service_1_('CZe_0004',_'S5') = 0.0
Service_1_('CZe_0004',_'S6') = 0.0
Service_1_('CZe_0004',_'S7') = 0.0
Service_1_('CZe_0004',_'S8') = 0.0
Service_1_('CZe_0004',_'S9') = 0.0
Service_1_('CZe_0005',_'DC_CA_FRESNO') = 0.0
Service_1_('CZe_0005',_'DC_IN_GREENWOOD') = 0.0
Service_1_('CZe_0005',_'DC_PA_CHAMBERSBURG') = 0.01
Service_1_('CZe_0005',_'DC_TX_DALLAS') = 0.0
Service_1_('CZe_0005',_'MFC_FL_JACKSONVILLE') = 0.0
Service_1_('CZe_0005',_'MFC_IL_ROMEOVILLE') = 0.0
Service_1_('CZe_0005',_'MFC_SC_GREER') = 0.0
Service_1_('CZe_0005',_'S1') = 0.0
Service_1_('CZe_0005',_'S10') = 0.0
Service_1_('CZe_0005',_'S11') = 0.0
Service_1_('CZe_0005',_'S12') = 0.0
Service_1_('CZe_0005',_'S13') = 0.0
Service_1_('CZe_0005',_'S14') = 0.0
Service_1_('CZe_0005',_'S15') = 0.0
Service_1_('CZe_0005',_'S16') = 0.0
Service_1_('CZe_0005',_'S17') = 0.0
Service_1_('CZe_0005',_'S18') = 0.0
Service_1_('CZe_0005',_'S19') = 0.0
Service_1_('CZe_0005',_'S2') = 0.0
Service_1_('CZe_0005',_'S20') = 0.0
Service_1_('CZe_0005',_'S21') = 0.0
Service_1_('CZe_0005',_'S22') = 0.0
Service_1_('CZe_0005',_'S23') = 0.0
Service_1_('CZe_0005',_'S24') = 0.0
Service_1_('CZe_0005',_'S25') = 0.0
Service_1_('CZe_0005',_'S26') = 0.0
Service_1_('CZe_0005',_'S27') = 0.0
Service_1_('CZe_0005',_'S3') = 0.0
Service_1_('CZe_0005',_'S4') = 0.0
Service_1_('CZe_0005',_'S5') = 0.0
Service_1_('CZe_0005',_'S6') = 0.0
Service_1_('CZe_0005',_'S7') = 0.0
Service_1_('CZe_0005',_'S8') = 0.0
Service_1_('CZe_0005',_'S9') = 0.0
below is the line of code I am currently using to print the results of this optimization problem.
for v in problem.variables():
print(v.name,"=",v.varValue)
#Print Optimal Solution
print("The cost of allocating demand equals",value(problem.objective))

You should be able to just insert a little logical test in your print loop.... I would suggest a small "epsilon" value for floating point values.
eps = 1e-4
for v in problem.variables():
if v.varValue > eps:
print(v.name,"=",v.varValue)

Related

How to simulate aging effect on back to back inverters in HSPICE

I am trying to simulate the effect of aging in the back-to-back inverters (SRAM PUF). I want to see the starting up of the back-to-back inverter's start-up voltage with transistor aging effect, wherein in one case the output initial from the 2 inverters is 1 0 and in another case, the output case will be 0 1.
How to do this in hspice. I know a little about the MOSRA test, but I am not sure how to use that to get the intended aging effect.
I have written a back-to-back inverter hspice netlist. but the output is always same with the simulation. How to flip the output with aging simulation. Any help is appreciated.
* PUF
vdd vdd gnd dc 1v
Mp1 out2 out1 vdd vdd pmos w= 90nm l= 45nm
Mn1 out2 out gnd gnd nmos w=45nm l= 45nm
Mp2 out1 out2 vdd vdd pmos w= 90nm l= 45nm
Mn2 out1 out2 gnd gnd nmos w=45nm l= 45nm
*transient analysis
.model p1_ra mosra level=1
+tit0 = 5e-8 titfd = 7.5e-10 tittd = 1.45e-20
+tn = 0.25
.appendmodel p1_ra mosra p1 pmos
.mosra reltotaltime= 1e8
.tran .1ps 5ns
.options post
.print V(out2) V(out1)
* PTM Low Power 45nm Metal Gate / High-K / Strained-Si
* nominal Vdd = 1.1V
.model nmos nmos level = 54
+version = 4.0 binunit = 1 paramchk= 1 mobmod = 0
+capmod = 2 igcmod = 1 igbmod = 1 geomod = 1
+diomod = 1 rdsmod = 0 rbodymod= 1 rgatemod= 1
+permod = 1 acnqsmod= 0 trnqsmod= 0
+tnom = 27 toxe = 1.8e-009 toxp = 1.5e-009 toxm = 1.8e-009
+dtox = 3e-010 epsrox = 3.9 wint = 5e-009 lint = 0
+ll = 0 wl = 0 lln = 1 wln = 1
+lw = 0 ww = 0 lwn = 1 wwn = 1
+lwl = 0 wwl = 0 xpart = 0 toxref = 1.8e-009
+vth0 = 0.62261 k1 = 0.4 k2 = 0 k3 = 0
+k3b = 0 w0 = 2.5e-006 dvt0 = 1 dvt1 = 2
+dvt2 = 0 dvt0w = 0 dvt1w = 0 dvt2w = 0
+dsub = 0.1 minv = 0.05 voffl = 0 dvtp0 = 1e-010
+dvtp1 = 0.1 lpe0 = 0 lpeb = 0 xj = 1.4e-008
+ngate = 1e+023 ndep = 3.24e+018 nsd = 2e+020 phin = 0
+cdsc = 0 cdscb = 0 cdscd = 0 cit = 0
+voff = -0.13 nfactor = 1.6 eta0 = 0.0125 etab = 0
+vfb = -0.55 u0 = 0.049 ua = 6e-010 ub = 1.2e-018
+uc = 0 vsat = 130000 a0 = 1 ags = 0
+a1 = 0 a2 = 1 b0 = 0 b1 = 0
+keta = 0.04 dwg = 0 dwb = 0 pclm = 0.02
+pdiblc1 = 0.001 pdiblc2 = 0.001 pdiblcb = -0.005 drout = 0.5
+pvag = 1e-020 delta = 0.01 pscbe1 = 8.14e+008 pscbe2 = 1e-007
+fprout = 0.2 pdits = 0.08 pditsd = 0.23 pditsl = 2300000
+rsh = 5 rdsw = 210 rsw = 80 rdw = 80
+rdswmin = 0 rdwmin = 0 rswmin = 0 prwg = 0
+prwb = 0 wr = 1 alpha0 = 0.074 alpha1 = 0.005
+beta0 = 30 agidl = 0.0002 bgidl = 2.1e+009 cgidl = 0.0002
+egidl = 0.8 aigbacc = 0.012 bigbacc = 0.0028 cigbacc = 0.002
+nigbacc = 1 aigbinv = 0.014 bigbinv = 0.004 cigbinv = 0.004
+eigbinv = 1.1 nigbinv = 3 aigc = 0.015211 bigc = 0.0027432
+cigc = 0.002 aigsd = 0.015211 bigsd = 0.0027432 cigsd = 0.002
+nigc = 1 poxedge = 1 pigcd = 1 ntox = 1
+xrcrg1 = 12 xrcrg2 = 5
+cgso = 1.1e-010 cgdo = 1.1e-010 cgbo = 2.56e-011 cgdl = 2.653e-010
+cgsl = 2.653e-010 ckappas = 0.03 ckappad = 0.03 acde = 1
+moin = 15 noff = 0.9 voffcv = 0.02
+kt1 = -0.11 kt1l = 0 kt2 = 0.022 ute = -1.5
+ua1 = 4.31e-009 ub1 = 7.61e-018 uc1 = -5.6e-011 prt = 0
+at = 33000
+fnoimod = 1 tnoimod = 0
+jss = 0.0001 jsws = 1e-011 jswgs = 1e-010 njs = 1
+ijthsfwd= 0.01 ijthsrev= 0.001 bvs = 10 xjbvs = 1
+jsd = 0.0001 jswd = 1e-011 jswgd = 1e-010 njd = 1
+ijthdfwd= 0.01 ijthdrev= 0.001 bvd = 10 xjbvd = 1
+pbs = 1 cjs = 0.0005 mjs = 0.5 pbsws = 1
+cjsws = 5e-010 mjsws = 0.33 pbswgs = 1 cjswgs = 3e-010
+mjswgs = 0.33 pbd = 1 cjd = 0.0005 mjd = 0.5
+pbswd = 1 cjswd = 5e-010 mjswd = 0.33 pbswgd = 1
+cjswgd = 5e-010 mjswgd = 0.33 tpb = 0.005 tcj = 0.001
+tpbsw = 0.005 tcjsw = 0.001 tpbswg = 0.005 tcjswg = 0.001
+xtis = 3 xtid = 3
+dmcg = 0 dmci = 0 dmdg = 0 dmcgt = 0
+dwj = 0 xgw = 0 xgl = 0
+rshg = 0.4 gbmin = 1e-010 rbpb = 5 rbpd = 15
+rbps = 15 rbdb = 15 rbsb = 15 ngcon = 1
.model pmos pmos level = 54
+version = 4.0 binunit = 1 paramchk= 1 mobmod = 0
+capmod = 2 igcmod = 1 igbmod = 1 geomod = 1
+diomod = 1 rdsmod = 0 rbodymod= 1 rgatemod= 1
+permod = 1 acnqsmod= 0 trnqsmod= 0
+tnom = 27 toxe = 1.82e-009 toxp = 1.5e-009 toxm = 1.82e-009
+dtox = 3.2e-010 epsrox = 3.9 wint = 5e-009 lint = 0
+ll = 0 wl = 0 lln = 1 wln = 1
+lw = 0 ww = 0 lwn = 1 wwn = 1
+lwl = 0 wwl = 0 xpart = 0 toxref = 1.82e-009
+vth0 = -0.587 k1 = 0.4 k2 = -0.01 k3 = 0
+k3b = 0 w0 = 2.5e-006 dvt0 = 1 dvt1 = 2
+dvt2 = -0.032 dvt0w = 0 dvt1w = 0 dvt2w = 0
+dsub = 0.1 minv = 0.05 voffl = 0 dvtp0 = 1e-011
+dvtp1 = 0.05 lpe0 = 0 lpeb = 0 xj = 1.4e-008
+ngate = 1e+023 ndep = 2.44e+018 nsd = 2e+020 phin = 0
+cdsc = 0 cdscb = 0 cdscd = 0 cit = 0
+voff = -0.126 nfactor = 1.8 eta0 = 0.0125 etab = 0
+vfb = 0.55 u0 = 0.021 ua = 2e-009 ub = 5e-019
+uc = 0 vsat = 90000 a0 = 1 ags = 1e-020
+a1 = 0 a2 = 1 b0 = 0 b1 = 0
+keta = -0.047 dwg = 0 dwb = 0 pclm = 0.12
+pdiblc1 = 0.001 pdiblc2 = 0.001 pdiblcb = 3.4e-008 drout = 0.56
+pvag = 1e-020 delta = 0.01 pscbe1 = 8.14e+008 pscbe2 = 9.58e-007
+fprout = 0.2 pdits = 0.08 pditsd = 0.23 pditsl = 2300000
+rsh = 5 rdsw = 250 rsw = 75 rdw = 75
+rdswmin = 0 rdwmin = 0 rswmin = 0 prwg = 0
+prwb = 0 wr = 1 alpha0 = 0.074 alpha1 = 0.005
+beta0 = 30 agidl = 0.0002 bgidl = 2.1e+009 cgidl = 0.0002
+egidl = 0.8 aigbacc = 0.012 bigbacc = 0.0028 cigbacc = 0.002
+nigbacc = 1 aigbinv = 0.014 bigbinv = 0.004 cigbinv = 0.004
+eigbinv = 1.1 nigbinv = 3 aigc = 0.0097 bigc = 0.00125
+cigc = 0.0008 aigsd = 0.0097 bigsd = 0.00125 cigsd = 0.0008
+nigc = 1 poxedge = 1 pigcd = 1 ntox = 1
+xrcrg1 = 12 xrcrg2 = 5
+cgso = 1.1e-010 cgdo = 1.1e-010 cgbo = 2.56e-011 cgdl = 2.653e-010
+cgsl = 2.653e-010 ckappas = 0.03 ckappad = 0.03 acde = 1
+moin = 15 noff = 0.9 voffcv = 0.02
+kt1 = -0.11 kt1l = 0 kt2 = 0.022 ute = -1.5
+ua1 = 4.31e-009 ub1 = 7.61e-018 uc1 = -5.6e-011 prt = 0
+at = 33000
+fnoimod = 1 tnoimod = 0
+jss = 0.0001 jsws = 1e-011 jswgs = 1e-010 njs = 1
+ijthsfwd= 0.01 ijthsrev= 0.001 bvs = 10 xjbvs = 1
+jsd = 0.0001 jswd = 1e-011 jswgd = 1e-010 njd = 1
+ijthdfwd= 0.01 ijthdrev= 0.001 bvd = 10 xjbvd = 1
+pbs = 1 cjs = 0.0005 mjs = 0.5 pbsws = 1
+cjsws = 5e-010 mjsws = 0.33 pbswgs = 1 cjswgs = 3e-010
+mjswgs = 0.33 pbd = 1 cjd = 0.0005 mjd = 0.5
+pbswd = 1 cjswd = 5e-010 mjswd = 0.33 pbswgd = 1
+cjswgd = 5e-010 mjswgd = 0.33 tpb = 0.005 tcj = 0.001
+tpbsw = 0.005 tcjsw = 0.001 tpbswg = 0.005 tcjswg = 0.001
+xtis = 3 xtid = 3
+dmcg = 0 dmci = 0 dmdg = 0 dmcgt = 0
+dwj = 0 xgw = 0 xgl = 0
+rshg = 0.4 gbmin = 1e-010 rbpb = 5 rbpd = 15
+rbps = 15 rbdb = 15 rbsb = 15 ngcon = 1
.end

Why do I get a view error when enumerating a Dataframe

Why do I get a "view" error:
ndf = pd.DataFrame()
ndf['Signals'] = [1,1,1,1,1,0,0,0,0,0]
signals_diff = ndf.Signals.diff()
ndf['Revals'] = [101,102,105,104,105,106,107,108,109,109]
ndf['Entry'] = 0
for i,element in enumerate(signals_diff):
if (i==0):
ndf.iloc[i]['Entry'] = ndf.iloc[i]['Revals']
elif (element == 0):
ndf.iloc[i]['Entry'] = ndf.iloc[i - 1]['Entry']
else:
ndf.iloc[i]['Entry'] = ndf.iloc[i]['Revals']
A value is trying to be set on a copy of a slice from a DataFrame
See the caveats in the documentation:
https://pandas.pydata.org/pandas-docs/stable/user_guide/indexing.html#returning-a-view-versus-a-copy
ndf.iloc[i]['Entry'] = ndf.iloc[i]['Revals']
instead of iloc use loc:
ndf = pd.DataFrame()
ndf['Signals'] = [1,1,1,1,1,0,0,0,0,0]
signals_diff = ndf.Signals.diff()
ndf['Revals'] = [101,102,105,104,105,106,107,108,109,109]
ndf['Entry'] = 0
for i,element in enumerate(signals_diff):
if (i==0):
ndf.loc[i,'Entry'] = ndf.loc[i,'Revals']
elif (element == 0):
ndf.loc[i,'Entry'] = ndf.loc[i - 1,'Entry']
else:
ndf.loc[i,'Entry'] = ndf.loc[i,'Revals']
This will solve the problem but when assigning, the index should be same. So because of the index thing you might not be able to get the expected result.
Do not chain indexes like ndf.iloc[i]['Entry'] when trying to assign something. See why does that not work.
That said, your code can be rewrite as:
ndf['Entry'] = ndf['Revals'].where(signals_diff != 0).ffill()
Output:
Signals Revals Entry
0 1 101 101.0
1 1 102 101.0
2 1 105 101.0
3 1 104 101.0
4 1 105 101.0
5 0 106 106.0
6 0 107 106.0
7 0 108 106.0
8 0 109 106.0
9 0 109 106.0
Let us keep using the index position slice with get_indexer
for i,element in enumerate(signals_diff):
if (i==0):
ndf.iloc[i,ndf.columns.get_indexer(['Entry'])] = ndf.iloc[i,ndf.columns.get_indexer(['Revals'])]
elif (element == 0):
ndf.iloc[i,ndf.columns.get_indexer(['Entry'])] = ndf.iloc[i - 1,ndf.columns.get_indexer(['Entry'])]
else:
ndf.iloc[i,ndf.columns.get_indexer(['Entry'])] = ndf.iloc[i,ndf.columns.get_indexer(['Revals'])]

How to find node voltage by VDR

how to determine the node voltage Vab using VDR.
Circuit diagram
You should ask questions like this on the electrical engineering Stack Exchange site here: https://electronics.stackexchange.com/
But anyway, since the resistors are all in series you can use the simple series resistance formula to find that the total series resistance is:
Rt = 2.5 + 1.5 + 0.6 + 0.9 + 0.5 = 6 Ohms
This means the current through the circuit is:
I = V/R = 0.36/6 = 0.06 A (60 mA)
The voltage across Vab is Vab = Va - Vb so:
Va = IR = 0.06 * 0.5 = 0.03 V (30 mV)
Vb = IR = 0.06 * (1.5 + 0.6 + 0.9 + 0.5) = 0.06 * 3.5 = 0.21 V (210 mV)
Vab = 0.21 - 0.03 = 0.18 V (180 mV)
This can be shortened to:
Vab = IR = 0.06 * (1.5 + 0.6 + 0.9) = 0.18 (180 mV)
To further your learning I would suggest learning about Ohm's law

Condition indexing numpy array of floats

import numpy as np
n = 10
xmin = 0
xmax = 1
dx = 1/n
x = np.arange(xmin-dx, xmax + 2*dx, dx)
print(x)
print(x <= 0.3)
The output of this code is following :
[-0.1 0. 0.1 0.2 0.3 0.4 0.5 0.6 0.7 0.8 0.9 1. 1.1]
[ True True True True False False False False False False False False
False]
Why the element in array with value 0.3 is not smaller or equal than 0.3?
​I tried the same with other comparasions and i saw that -0.1 <= -0.1 and 0.1 <= 0.1 while 0.2 is not less or equal 0.2.
Really do not understand what is happening here.
I got it. Never compare float numbers, because of round-off error.
This will work:
print(x <= 0.3 + np.finfo(np.float64).eps)

Nim: How to check if a float is nan or inf?

How can I check if a float variable is NaN or inf. Searching the standard library for isNaN or isInf does not reveal anything. There is a blog post suggesting to use
proc cIsNaN(x: float): int {.importc: "isnan", header: "<math.h>".}
but is there really nothing in the standard library?
The standard library does provide checking for NaN/inf, but under the generalized concept of classify, which is why it is easy to miss. To illustrate:
import math
# Inf and NaN literals are defined in system.nim as
# Inf* {.magic: "Inf".} = 1.0 / 0.0
# NaN* {.magic: "NaN".} = 0.0 / 0.0
let floatsToTest = [0.0, Inf, NaN]
for x in floatsToTest:
echo x, " is NaN ", x.classify == fcNaN
echo x, " is inf ", x.classify == fcInf
Output:
0.0 is NaN false
0.0 is inf false
inf is NaN false
inf is inf true
nan is NaN true
nan is inf false
classify can also test for other properties like fcSubnormal, fcNegZero, or fcNegInf (see FloatClass).
You can add your own function
import std/math
func is_number*(n: float): bool =
let ntype = n.classify
ntype == fc_normal or ntype == fc_zero or ntype == fc_neg_zero

Resources