func doEnergy(cmd string, args []string) { fs := flag.NewFlagSet("energy", flag.ExitOnError) t0 := fs.Int("t1", 0, "beginning of time interval (default is beginning of simulation)") t1 := fs.Int("t2", -1, "end of time interval (default if end of simulation)") fs.Usage = func() { log.Print("Usage: energy") fs.PrintDefaults() } fs.Parse(args) e, err := query.EnergyProduced(db, simid, *t0, *t1) fatalif(err) fmt.Println(e) }
func ObjANS2014(scen *Scenario, db *sql.DB, simid []byte) (float64, error) { s := &ANSScenario{} err := s.Load(scen.File) if err != nil { return math.Inf(1), err } // add up overnight and operating costs converted to PV(t=0) q1 := ` SELECT tl.Time FROM TimeList AS tl INNER JOIN Agents As a ON a.EnterTime <= tl.Time AND (a.ExitTime >= tl.Time OR a.ExitTime IS NULL) WHERE a.SimId = tl.SimId AND a.SimId = ? AND a.Prototype = ?; ` q2 := `SELECT EnterTime FROM Agents WHERE SimId = ? AND Prototype = ?` totcost := 0.0 for _, fac := range s.Facs { // calc total operating cost rows, err := db.Query(q1, simid, fac.Proto) if err != nil { return math.Inf(1), err } for rows.Next() { var t int if err := rows.Scan(&t); err != nil { return math.Inf(1), err } totcost += PV(fac.OpCost, t, s.Discount) } if err := rows.Err(); err != nil { return math.Inf(1), err } // calc overnight capital cost rows, err = db.Query(q2, simid, fac.Proto) if err != nil { return math.Inf(1), err } for rows.Next() { var t int if err := rows.Scan(&t); err != nil { return math.Inf(1), err } totcost += PV(fac.CapitalCost, t, s.Discount) } if err := rows.Err(); err != nil { return math.Inf(1), err } // add in waste penalty ags, err := query.AllAgents(db, simid, fac.Proto) if err != nil { return math.Inf(1), err } // InvAt uses all agents if no ids are passed - so we need to skip // from here if len(ags) == 0 { continue } ids := make([]int, len(ags)) for i, a := range ags { ids[i] = a.Id } for t := 0; t < s.SimDur; t++ { mat, err := query.InvAt(db, simid, t, ids...) if err != nil { return math.Inf(1), err } for nuc, qty := range mat { nucstr := fmt.Sprint(nuc) totcost += PV(s.NuclideCost[nucstr]*float64(qty)*(1-fac.WasteDiscount), t, s.Discount) } } } // normalize to energy produced joules, err := query.EnergyProduced(db, simid, 0, s.SimDur) if err != nil { return math.Inf(1), err } mwh := joules / nuc.MWh mult := 1e6 // to get the objective around 0.1 same magnitude as constraint penalties return totcost / (mwh + 1e-30) * mult, nil }