func doAgents(cmd string, args []string) { fs := flag.NewFlagSet("agents", flag.ExitOnError) proto := fs.String("proto", "", "filter by prototype (default \"\" is all prototypes)") fs.Usage = func() { log.Print("Usage: agents") fs.PrintDefaults() } fs.Parse(args) ags, err := query.AllAgents(db, simid, *proto) fatalif(err) for _, a := range ags { fmt.Println(a) } }
func uploadInner(w http.ResponseWriter, r *http.Request, kill chan bool) { // parse database from multi part form data if err := r.ParseMultipartForm(MAX_MEMORY); err != nil { log.Println(err) http.Error(w, err.Error(), http.StatusForbidden) return } gotDb := false uid := r.URL.Path[len("/upload/"):] fname := uid + ".sqlite" for _, fileHeaders := range r.MultipartForm.File { for _, fileHeader := range fileHeaders { file, _ := fileHeader.Open() data, _ := ioutil.ReadAll(file) err := ioutil.WriteFile(fname, data, 0644) if err != nil { http.Error(w, err.Error(), http.StatusInternalServerError) log.Print(err) return } defer func() { os.Remove(fname) }() gotDb = true break } break } if !gotDb { http.Error(w, "No file provided", http.StatusBadRequest) log.Print("received request with no file") return } select { case <-kill: return default: } // post process the database db, err := sql.Open("sqlite3", fname) if err != nil { http.Error(w, err.Error(), http.StatusInternalServerError) log.Print(err) return } defer db.Close() err = post.Prepare(db) if err != nil { http.Error(w, err.Error(), http.StatusInternalServerError) log.Print(err) return } defer post.Finish(db) simids, err := post.GetSimIds(db) if err != nil { http.Error(w, err.Error(), http.StatusInternalServerError) log.Print(err) return } ctx := post.NewContext(db, simids[0]) if err := ctx.WalkAll(); err != nil { log.Println(err) } // get simid ids, err := query.SimIds(db) if err != nil { http.Error(w, err.Error(), http.StatusInternalServerError) log.Print(err) return } simid := ids[0] rs := &Results{} select { case <-kill: return default: } // create flow graph combineProto := false arcs, err := query.FlowGraph(db, simid, 0, -1, combineProto) if err != nil { http.Error(w, err.Error(), http.StatusInternalServerError) log.Print(err) return } dotname := fname + ".dot" dotf, err := os.Create(dotname) if err != nil { http.Error(w, err.Error(), http.StatusInternalServerError) log.Print(err) return } defer func() { os.Remove(dotname) }() fmt.Fprintln(dotf, "digraph ResourceFlows {") fmt.Fprintln(dotf, " overlap = false;") fmt.Fprintln(dotf, " nodesep=1.0;") fmt.Fprintln(dotf, " edge [fontsize=9];") for _, arc := range arcs { fmt.Fprintf(dotf, " \"%v\" -> \"%v\" [label=\"%v\\n(%.3g kg)\"];\n", arc.Src, arc.Dst, arc.Commod, arc.Quantity) } fmt.Fprintln(dotf, "}") dotf.Close() var buf bytes.Buffer cmd := exec.Command("dot", "-Tsvg", dotname) cmd.Stdout = &buf err = cmd.Run() if err != nil { http.Error(w, err.Error(), http.StatusInternalServerError) log.Print(err) return } rs.Flowgraph = buf.String() select { case <-kill: return default: } // create agents table rs.Agents, err = query.AllAgents(db, simid, "") if err != nil { http.Error(w, err.Error(), http.StatusInternalServerError) log.Print(err) return } // create Material transactions table sql := ` SELECT TransactionId,Time,SenderId,ReceiverId,tr.ResourceId,Commodity,NucId,MassFrac*Quantity FROM Transactions AS tr INNER JOIN Resources AS res ON tr.ResourceId = res.ResourceId INNER JOIN Compositions AS cmp ON res.QualId = cmp.QualId WHERE tr.SimId = ? AND cmp.SimId = res.SimId AND res.SimId = tr.SimId AND res.Type = 'Material'; ` rows, err := db.Query(sql, simid) if err != nil { http.Error(w, err.Error(), http.StatusInternalServerError) log.Print(err) return } for rows.Next() { t := &Trans{} if err := rows.Scan(&t.Id, &t.Time, &t.Sender, &t.Receiver, &t.ResourceId, &t.Commod, &t.Nuc, &t.Qty); err != nil { http.Error(w, err.Error(), http.StatusInternalServerError) log.Print(err) return } rs.TransMats = append(rs.TransMats, t) } if err := rows.Err(); err != nil { http.Error(w, err.Error(), http.StatusInternalServerError) log.Print(err) return } // create Material transactions table sql = ` SELECT TransactionId,Time,SenderId,ReceiverId,tr.ResourceId,Commodity,Quality,Quantity FROM Transactions AS tr INNER JOIN Resources AS res ON tr.ResourceId = res.ResourceId INNER JOIN Products AS pd ON res.QualId = pd.QualId WHERE tr.SimId = ? AND pd.SimId = res.SimId AND res.SimId = tr.SimId AND res.Type = 'Product'; ` rows, err = db.Query(sql, simid) if err != nil { http.Error(w, err.Error(), http.StatusInternalServerError) log.Print(err) return } for rows.Next() { t := &ProdTrans{} if err := rows.Scan(&t.Id, &t.Time, &t.Sender, &t.Receiver, &t.ResourceId, &t.Commod, &t.Quality, &t.Qty); err != nil { http.Error(w, err.Error(), http.StatusInternalServerError) log.Print(err) return } rs.TransProds = append(rs.TransProds, t) } if err := rows.Err(); err != nil { http.Error(w, err.Error(), http.StatusInternalServerError) log.Print(err) return } select { case <-kill: return default: } // render all results and save page rs.Uid = uid resultTmpl.Execute(w, rs) f, err := os.Create(uid + ".html") if err != nil { http.Error(w, err.Error(), http.StatusInternalServerError) log.Print(err) return } defer f.Close() resultTmpl.Execute(f, rs) close(kill) }
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 }