public Dictionary <EdgePair, Double> Solve(FCTPGraph graph) { Dictionary <EdgePair, Double> ep = new Dictionary <EdgePair, Double>(); ep = solver.Solve(graph); return(ep); }
static void Main(string[] args) { String filename = @"C:\Users\Augusto Baffa\workspace\FixedCostTransportationGame\rsc\test\N104.DAT"; FCTPGraph g = InstanceConverter.convert(filename); RuntimeSolver solver = new RuntimeSolver(); Dictionary <EdgePair, double> l = solver.Solve(g); foreach (EdgePair ep in l.Keys) { Console.Out.WriteLine(ep.ToString() + "\t" + l[ep]); } }
/** * Use an external solver to compute the flows based on the transportation * problem */ private Dictionary <EdgePair, Double> solveTP(Dictionary <EdgePair, Double> values) { List <Edge> edges = makeEdgesFromBids(values); FCTPGraph Gcap = new FCTPGraph(G.Name); Gcap.AddAllSources(G.Sources); Gcap.AddAllSinks(G.Sinks); Gcap.AddAllEdges(edges); Dictionary <EdgePair, Double> flow = solver.Solve(Gcap); return(flow); }
private void init(FCTPGraph instance, double U, String[] players) { this.G = instance; this.U = U; this.P = new Dictionary <EdgePair, List <Bid> >(); this.payoff = new Dictionary <String, Double>(); this.flow = new Dictionary <EdgePair, Double>(); this.W = new Dictionary <EdgePair, List <String> >(); solver = new RuntimeSolver(); foreach (String player in players) { payoff.Add(player, 0.0); } }
public void matchOn(FileInfo child, List <string> players) { FCTPGraph problem = InstanceConverter.convert( child.FullName); log("Match " + problem.Name); //Initialize Edge Auction EdgeAuction auction = new EdgeAuction(problem, GamePlay.U, players); // GamePlay.U - Gets Constant Factor K = 20 (U = K on problem description) //int edgesPerRound = computeEdgesPerRound(problem, players.Count); //log(edgesPerRound + " edges per round"); //String msg = "instance " + problem.Name + " " + edgesPerRound; playRound(child.Name, auction, GamePlay.round); // GamePlay.round - Gets the current round number. Dictionary <String, Double> instancePayoffs = auction.getPayoffMap(); foreach (HandleClient c in GamePlay.socket.ClientList) // GamePlay.socket.ClientList - Gets list of players { String s = c.player.name; if (instancePayoffs.ContainsKey(c.player.name)) { c.player.SetScore(instancePayoffs[s]); if (!payoffs.ContainsKey(s)) { payoffs.Add(s, instancePayoffs[s]); } else { payoffs[s] = payoffs[s] + instancePayoffs[s]; } } } log("PARTIAL"); String partial = getSortedPayoffMapMsg(payoffs); log(partial); }
/**Reads a .DAT file with the graph description and returns the graph*/ public static FCTPGraph convert(String srcPath) { StreamReader reader = new StreamReader(srcPath); List <Edge> edges = new List <Edge>(); List <Node> sources = new List <Node>(); List <Node> sinks = new List <Node>(); // HEADER reader.ReadLine();//instance // INSTANCE INFO (name, sources, sinks) String info = reader.ReadLine(); Scanner sc = new Scanner(info); String name = sc.next(); sc.Close(); // ARCS reader.ReadLine();//ARCS String line = reader.ReadLine(); StringBuilder edgesBuilder = new StringBuilder(line + "\n"); while (!line.Equals("S")) { line = reader.ReadLine(); if (!line.Equals("S")) { edgesBuilder.Append(line + "\n"); } } sc = new Scanner(edgesBuilder.ToString()); while (sc.hasNext()) { int source = sc.nextInt(); int sink = sc.nextInt(); double varCost = sc.nextDouble(); double fixedCost = sc.nextDouble(); sc.nextLine(); edges.Add(new Edge(source, sink, varCost, fixedCost)); } sc.Close(); // SOURCE NODES line = reader.ReadLine(); StringBuilder sourceBuilder = new StringBuilder(line + "\n"); while (!line.Equals("D")) { line = reader.ReadLine(); if (!line.Equals("D")) { sourceBuilder.Append(line + "\n"); } } sc = new Scanner(sourceBuilder.ToString()); while (sc.hasNext()) { int source = sc.nextInt(); double resource = sc.nextDouble(); sources.Add(new Node(source, resource)); //sc.nextLine(); } sc.Close(); line = reader.ReadLine(); StringBuilder sinkBuilder = new StringBuilder(line + "\n"); while ((line = reader.ReadLine()) != null) { //line = reader.ReadLine(); if (!line.Equals("END")) { sinkBuilder.Append(line + "\n"); } } sc = new Scanner(sinkBuilder.ToString()); while (sc.hasNext()) { int sink = sc.nextInt(); double demand = sc.nextDouble(); sinks.Add(new Node(sink, demand)); //sc.nextLine(); } sc.Close(); reader.Close(); FCTPGraph ti = new FCTPGraph(name, sources, sinks, edges); return(ti); }
public EdgeAuction(FCTPGraph instance, double U, String[] players) { init(instance, U, players); }
private TPSolver solver; // Linear solver public EdgeAuction(FCTPGraph instance, double U, List <String> players) { String[] nameArray = players.ToArray(); init(instance, U, nameArray); }
public Dictionary <EdgePair, Double> Solve(FCTPGraph ti) { Dictionary <EdgePair, Double> edgeFlows = new Dictionary <EdgePair, Double>(); Dictionary <String, Edge> edgeMap = new Dictionary <String, Edge>(); Dictionary <String, INumVar> varMap = new Dictionary <String, INumVar>(); String currentVar = ""; try { //Model Cplex cplex = new Cplex(); IModel model = cplex.GetModel(); //model.Set(GRB.StringAttr.ModelName, "tranportation"); ILinearNumExpr expr = cplex.LinearNumExpr(); //edges foreach (Edge e in ti.Edges) { String xij = edgeVarName(e.Source, e.Sink); edgeMap.Add(xij, e); INumVar var = cplex.NumVar(0, System.Double.MaxValue); var.Name = xij; varMap.Add(xij, var); expr.AddTerm(e.C, var); } //objective min Sum c_{ij} x_{ij} model.Add(cplex.Minimize(expr)); //supply constraints foreach (Node ssource in ti.Sources) { List <INumExpr> sourceConstraint = new List <INumExpr>(); foreach (Node ssink in ti.Sinks) { String name = edgeVarName(ssource.Id, ssink.Id); sourceConstraint.Add(varMap[name]); } cplex.AddEq(cplex.Sum(sourceConstraint.ToArray()), ssource.Amount); } //demand constraints foreach (Node dsink in ti.Sinks) { List <INumExpr> sinkConstraint = new List <INumExpr>(); foreach (Node dsource in ti.Sources) { String name = edgeVarName(dsource.Id, dsink.Id); sinkConstraint.Add(varMap[name]); } cplex.AddEq(cplex.Sum(sinkConstraint.ToArray()), dsink.Amount); } cplex.SetParam(Cplex.BooleanParam.Threads, 1); cplex.ExportModel("mipTranportationCplex.lp"); bool status = cplex.Solve(); Console.WriteLine("Status: " + status); foreach (String s in edgeMap.Keys) { currentVar = s; double flow = cplex.GetValue(varMap[s]); Edge e = edgeMap[s]; EdgePair ep = new EdgePair(e.Source, e.Sink); edgeFlows.Add(ep, flow); } cplex.End(); } catch (ILOG.Concert.Exception e) { Console.Out.WriteLine(currentVar + " - is current var"); Console.Out.WriteLine(e.ToString()); } return(edgeFlows); }
public Dictionary <EdgePair, Double> Solve(FCTPGraph ti) { Dictionary <EdgePair, Double> edgeFlows = new Dictionary <EdgePair, Double>(); Dictionary <String, Edge> edgeMap = new Dictionary <String, Edge>(); Dictionary <String, GRBVar> varMap = new Dictionary <String, GRBVar>(); String currentVar = ""; try { //Model GRBEnv env = new GRBEnv(); env.Set(GRB.IntParam.LogToConsole, 0); GRBModel model = new GRBModel(env); model.Set(GRB.StringAttr.ModelName, "tranportation"); //edges foreach (Edge e in ti.Edges) { String xij = edgeVarName(e.Source, e.Sink); edgeMap.Add(xij, e); GRBVar var = model.AddVar(0, GRB.INFINITY, e.C, GRB.CONTINUOUS, xij); varMap.Add(xij, var); } //objective min Sum cij xij model.Set(GRB.IntAttr.ModelSense, GRB.MINIMIZE); //integrate variables model.Update(); //supply constraints foreach (Node source in ti.Sources) { GRBLinExpr sourceConstraint = new GRBLinExpr(); foreach (Node sink in ti.Sinks) { String name = edgeVarName(source.Id, sink.Id); sourceConstraint.AddTerm(1, varMap[name]); } model.AddConstr(sourceConstraint, GRB.EQUAL, source.Amount, ""); } //demand constraints foreach (Node sink in ti.Sinks) { GRBLinExpr sinkConstraint = new GRBLinExpr(); foreach (Node source in ti.Sources) { String name = edgeVarName(source.Id, sink.Id); sinkConstraint.AddTerm(1, varMap[name]); } model.AddConstr(sinkConstraint, GRB.EQUAL, sink.Amount, ""); } //update constraints model.Update(); model.Write("mipTranportationGurobi.lp"); env.Set(GRB.IntParam.Threads, 1); model.Optimize(); bool status = model.Get(GRB.IntAttr.Status) == GRB.Status.OPTIMAL; Console.WriteLine("Status: " + status); foreach (String s in edgeMap.Keys) { currentVar = s; double flow = varMap[s].Get(GRB.DoubleAttr.X); Edge e = edgeMap[s]; EdgePair ep = new EdgePair(e.Source, e.Sink); edgeFlows.Add(ep, flow); } model.Dispose(); } catch (GRBException e) { Console.Out.WriteLine(currentVar + " - is current var"); Console.Out.WriteLine(e.ErrorCode); Console.Out.WriteLine(e.ToString()); } return(edgeFlows); }