/* * Apply the function "supply" to all active * interfaces with a link-local address. */ void supplyall(struct sockaddr_in6 *sin6, int rtstate, struct interface *skipif, boolean_t splith) { struct interface *ifp; for (ifp = ifnet; ifp != NULL; ifp = ifp->int_next) { if ((ifp->int_flags & RIP6_IFF_UP) == 0) continue; if (ifp->int_flags & RIP6_IFF_NORTEXCH) { if (tracing & OUTPUT_BIT) { (void) fprintf(ftrace, "Suppress sending RIPng response packet " "on %s (no route exchange on interface)\n", ifp->int_name); (void) fflush(ftrace); } continue; } if (ifp->int_sock == -1) continue; if (ifp == skipif) continue; if (!IN6_IS_ADDR_LINKLOCAL(&ifp->int_addr)) continue; supply(sin6, ifp, rtstate, splith); } }
void EmbedderOptimalFlexDraw::optimizeOverEmbeddings( StaticPlanarSPQRTree &T, node parent, node mu, int bends, NodeArray<int> cost[], NodeArray<long long> embedding[]) { cost[bends][mu] = numeric_limits<int>::max(); long long embeddingsCount = T.numberOfNodeEmbeddings(mu); for (long long currentEmbedding = 0; currentEmbedding < embeddingsCount; ++currentEmbedding) { T.embed(mu, currentEmbedding); Skeleton &skeleton = T.skeleton(mu); Graph skeletonGraph = skeleton.getGraph(); ConstCombinatorialEmbedding skeletonEmbedding(skeletonGraph); NodeArray<node> vertexNode(skeletonGraph); EdgeArray<node> edgeNode(skeletonGraph); FaceArray<node> faceNode(skeletonEmbedding); Graph N; EdgeArray<int> upper(N); EdgeArray<int> perUnitCost(N); NodeArray<int> supply(N); createNetwork( parent, mu, bends, cost, embedding, skeleton, edgeNode, N, upper, perUnitCost, supply); EdgeArray<int> lower(N, 0); EdgeArray<int> flow(N); NodeArray<int> dual(N); m_minCostFlowComputer.get().call(N, lower, upper, perUnitCost, supply, flow, dual); int currentCost = 0; for (edge e = N.firstEdge(); e != nullptr; e = e->succ()) currentCost += perUnitCost[e] * flow[e]; for (adjEntry adj = mu->firstAdj(); adj != nullptr; adj = adj->succ()) currentCost += cost[0][adj->twinNode()]; if (currentCost < cost[bends][mu]) { cost[bends][mu] = currentCost; embedding[bends][mu] = currentEmbedding; } } }
void FuelEntity::_supplyAll(int number) { if (number--) { auto tank=fuelTank.back(); fuelTank.pop_back(); tank->supply(0); CallFunc* call=CallFunc::create(CC_CALLBACK_0(FuelEntity::_supplyAll, this,number)); tank->runAction(Sequence::create(DelayTime::create(0.05),call, NULL)); } }
int main(int argc, char **argv) { EventList eventlist; ProcessList processes; long ev_EndSimulation = 0; Traverser traverser(&eventlist); ColdTestCell cell1(&eventlist,&traverser); ColdTestCell cell2(&eventlist, &traverser); ColdTestCell cell3(&eventlist, &traverser); Source supply(&eventlist, &traverser); Sink derig(&eventlist); processes.push_back(&traverser); processes.push_back(&cell1); processes.push_back(&cell2); processes.push_back(&cell3); processes.push_back(&supply); processes.push_back(&derig); // Connect up production layout traverser.cell1(&cell1); traverser.cell2(&cell2); traverser.cell3(&cell3); traverser.infeed(&supply); traverser.outfeed(&derig); // Initialise the processes eventlist.push(new Event(100000, nullptr, ev_EndSimulation)); // End Simulation Event bool change; do { change = false; for (ProcessList::iterator i = processes.begin(); i != processes.end(); i++) change |= (*i)->run(); // Run each process until no change } while (change); // Run the event management loop. while (Event *event = eventlist.top()) { eventlist.pop(); // Remove the top element from the list SimulationTime += event->getTime(); // Advance simulation time if (event->getEventType() == ev_EndSimulation) break; event->getProcess()->HandleEvent(event); delete event; // no longer needed } }
// main program: int sc_main (int argc, char *argv[]) { sc_core::sc_set_time_resolution(1.0, sc_core::SC_NS); sc_core::sc_signal <double> angle; ab_signal <electrical, parallel> mains; ab_signal <electrical, series> output; sc_core::sc_clock pwm("PWM", sc_core::sc_time(0.1, sc_core::SC_MS)); sc_core::sc_trace_file *f = create_tab_trace_file("TRACES", 5e-6); mains.trace(f, "MAINS"); output.trace(f, "OUTPUT"); generator <double> signal_source("SOURCE1", sine(10, 100, 0, 1024)); signal_source(angle); source <electrical> supply("GENERATOR1", cfg::across); supply.input(angle); supply.port(mains); mosfet_2p m("M1", 0.75, 2.5); m(mains, output, pwm); m.port[0] <<= 2 ohm; m.port[1] <<= 2 ohm; R_load r("R1", 2); r.port <<= 2 ohm; r(output); diode_1p d("D1", 0.8, 2); d.port <<= 2 ohm; d(-output); sc_core::sc_start(sc_core::sc_time(10e-3, sc_core::SC_SEC)); close_tab_trace_file(f); return 0; }
void OptimalRanking::doCall( const Graph& G, NodeArray<int> &rank, EdgeArray<bool> &reversed, const EdgeArray<int> &length, const EdgeArray<int> &costOrig) { MinCostFlowReinelt<int> mcf; // construct min-cost flow problem GraphCopy GC; GC.createEmpty(G); // compute connected component of G NodeArray<int> component(G); int numCC = connectedComponents(G,component); // intialize the array of lists of nodes contained in a CC Array<List<node> > nodesInCC(numCC); for(node v : G.nodes) nodesInCC[component[v]].pushBack(v); EdgeArray<edge> auxCopy(G); rank.init(G); for(int i = 0; i < numCC; ++i) { GC.initByNodes(nodesInCC[i], auxCopy); makeLoopFree(GC); for(edge e : GC.edges) if(reversed[GC.original(e)]) GC.reverseEdge(e); // special cases: if(GC.numberOfNodes() == 1) { rank[GC.original(GC.firstNode())] = 0; continue; } else if(GC.numberOfEdges() == 1) { edge e = GC.original(GC.firstEdge()); rank[e->source()] = 0; rank[e->target()] = length[e]; continue; } EdgeArray<int> lowerBound(GC,0); EdgeArray<int> upperBound(GC,mcf.infinity()); EdgeArray<int> cost(GC); NodeArray<int> supply(GC); for(edge e : GC.edges) cost[e] = -length[GC.original(e)]; for(node v : GC.nodes) { int s = 0; edge e; forall_adj_edges(e,v) { if(v == e->source()) s += costOrig[GC.original(e)]; else s -= costOrig[GC.original(e)]; } supply[v] = s; } OGDF_ASSERT(isAcyclic(GC) == true); // find min-cost flow EdgeArray<int> flow(GC); NodeArray<int> dual(GC); #ifdef OGDF_DEBUG bool feasible = #endif mcf.call(GC, lowerBound, upperBound, cost, supply, flow, dual); OGDF_ASSERT(feasible); for(node v : GC.nodes) rank[GC.original(v)] = dual[v]; } }
/* send all of the routing table or just do a flash update */ void rip_bcast(int flash) { #ifdef _HAVE_SIN_LEN static struct sockaddr_in dst = {sizeof(dst), AF_INET, 0, {0}, {0}}; #else static struct sockaddr_in dst = {AF_INET}; #endif struct interface *ifp; enum output_type type; int vers; struct timeval rtime; need_flash = 0; intvl_random(&rtime, MIN_WAITTIME, MAX_WAITTIME); no_flash = rtime; timevaladd(&no_flash, &now); if (rip_sock < 0) return; trace_act("send %s and inhibit dynamic updates for %.3f sec", flash ? "dynamic update" : "all routes", rtime.tv_sec + ((float)rtime.tv_usec)/1000000.0); for (ifp = ifnet; ifp != NULL; ifp = ifp->int_next) { /* Skip interfaces not doing RIP. * Do try broken interfaces to see if they have healed. */ if (IS_RIP_OUT_OFF(ifp->int_state)) continue; /* skip turned off interfaces */ if (!iff_up(ifp->int_if_flags)) continue; vers = (ifp->int_state & IS_NO_RIPV1_OUT) ? RIPv2 : RIPv1; if (ifp->int_if_flags & IFF_BROADCAST) { /* ordinary, hardware interface */ dst.sin_addr.s_addr = ifp->int_brdaddr; if (vers == RIPv2 && !(ifp->int_state & IS_NO_RIP_MCAST)) { type = OUT_MULTICAST; } else { type = OUT_BROADCAST; } } else if (ifp->int_if_flags & IFF_POINTOPOINT) { /* point-to-point hardware interface */ dst.sin_addr.s_addr = ifp->int_dstaddr; type = OUT_UNICAST; } else if (ifp->int_state & IS_REMOTE) { /* remote interface */ dst.sin_addr.s_addr = ifp->int_addr; type = OUT_UNICAST; } else { /* ATM, HIPPI, etc. */ continue; } supply(&dst, ifp, type, flash, vers, 1); } update_seqno++; /* all routes are up to date */ }
void EmbedderOptimalFlexDraw::call(Graph &G, adjEntry &adjExternal) { StaticPlanarSPQRTree T(G); NodeArray<int> cost[4]; NodeArray<long long> embedding[4]; for (int bends = 0; bends < 4; ++bends) { cost[bends].init(T.tree()); embedding[bends].init(T.tree()); } int minCost = numeric_limits<int>::max(); node minCostRoot; long long minCostEmbedding; for (node root = T.tree().firstNode(); root != nullptr; root = root->succ()) { T.rootTreeAt(root); for (adjEntry adj = root->firstAdj(); adj != nullptr; adj = adj->succ()) computePrincipalSplitComponentCost(T, cost, embedding, root, adj->twinNode()); optimizeOverEmbeddings(T, nullptr, root, 0, cost, embedding); if (cost[0][root] < minCost) { minCost = cost[0][root]; minCostEmbedding = embedding[0][root]; minCostRoot = root; } } T.rootTreeAt(minCostRoot); T.embed(minCostRoot, minCostEmbedding); for (adjEntry adj = minCostRoot->firstAdj(); adj != nullptr; adj = adj->succ()) computePrincipalSplitComponentCost(T, cost, embedding, minCostRoot, adj->twinNode()); Skeleton &skeleton = T.skeleton(minCostRoot); Graph skeletonGraph = skeleton.getGraph(); ConstCombinatorialEmbedding skeletonEmbedding(skeletonGraph); EdgeArray<node> edgeNode(skeletonGraph); Graph N; EdgeArray<int> upper(N); EdgeArray<int> perUnitCost(N); NodeArray<int> supply(N); createNetwork( nullptr, minCostRoot, 0, cost, embedding, skeleton, edgeNode, N, upper, perUnitCost, supply); EdgeArray<int> lower(N, 0); EdgeArray<int> flow(N); NodeArray<int> dual(N); m_minCostFlowComputer.get().call(N, lower, upper, perUnitCost, supply, flow, dual); for (node mu = T.tree().firstNode(); mu != nullptr; mu = mu->succ()) { if (mu == minCostRoot) continue; int bends = 0; for (adjEntry adj = edgeNode[T.skeleton(mu).referenceEdge()]->firstAdj(); adj != nullptr; adj = adj->succ()) bends += abs(flow[adj->theEdge()]); T.embed(mu, embedding[bends][mu]); } T.embed(G); ConstCombinatorialEmbedding graphEmbedding(G); adjExternal = graphEmbedding.externalFace()->firstAdj(); }
int main(int argc, char **argv) { IloEnv env; try { IloInt i, j; IloNumArray capacity(env), fixedCost(env); FloatMatrix cost(env); IloInt nbLocations; IloInt nbClients; const char* filename = "../../../examples/data/facility.dat"; if (argc > 1) filename = argv[1]; ifstream file(filename); if (!file) { cerr << "ERROR: could not open file '" << filename << "' for reading" << endl; cerr << "usage: " << argv[0] << " <file>" << endl; throw(-1); } file >> capacity >> fixedCost >> cost; nbLocations = capacity.getSize(); nbClients = cost.getSize(); IloBool consistentData = (fixedCost.getSize() == nbLocations); for(i = 0; consistentData && (i < nbClients); i++) consistentData = (cost[i].getSize() == nbLocations); if (!consistentData) { cerr << "ERROR: data file '" << filename << "' contains inconsistent data" << endl; throw(-1); } IloNumVarArray open(env, nbLocations, 0, 1, ILOINT); NumVarMatrix supply(env, nbClients); for(i = 0; i < nbClients; i++) supply[i] = IloNumVarArray(env, nbLocations, 0, 1, ILOINT); IloModel model(env); for(i = 0; i < nbClients; i++) model.add(IloSum(supply[i]) == 1); for(j = 0; j < nbLocations; j++) { IloExpr v(env); for(i = 0; i < nbClients; i++) v += supply[i][j]; model.add(v <= capacity[j] * open[j]); v.end(); } IloExpr obj = IloScalProd(fixedCost, open); for(i = 0; i < nbClients; i++) { obj += IloScalProd(cost[i], supply[i]); } model.add(IloMinimize(env, obj)); obj.end(); IloCplex cplex(env); cplex.extract(model); cplex.solve(); cplex.out() << "Solution status: " << cplex.getStatus() << endl; IloNum tolerance = cplex.getParam( IloCplex::Param::MIP::Tolerances::Integrality); cplex.out() << "Optimal value: " << cplex.getObjValue() << endl; for(j = 0; j < nbLocations; j++) { if (cplex.getValue(open[j]) >= 1 - tolerance) { cplex.out() << "Facility " << j << " is open, it serves clients "; for(i = 0; i < nbClients; i++) { if (cplex.getValue(supply[i][j]) >= 1 - tolerance) cplex.out() << i << " "; } cplex.out() << endl; } } } catch(IloException& e) { cerr << " ERROR: " << e << endl; } catch(...) { cerr << " ERROR" << endl; } env.end(); return 0; }
int main(int argc, char** argv) { if (argc <= 1) { cerr << "Usage: " << argv[0] << " <model>" << endl; cerr << " model = 0 -> convex piecewise linear model, " << endl; cerr << " model = 1 -> concave piecewise linear model. [default]" << endl; } IloBool convex; if (argc <= 1) convex = IloFalse; else convex = atoi(argv[1]) == 0 ? IloTrue : IloFalse; IloEnv env; try { IloInt i, j; IloModel model(env); IloInt nbDemand = 4; IloInt nbSupply = 3; IloNumArray supply(env, nbSupply, 1000.0, 850.0, 1250.0); IloNumArray demand(env, nbDemand, 900.0, 1200.0, 600.0, 400.); NumVarMatrix x(env, nbSupply); NumVarMatrix y(env, nbSupply); for(i = 0; i < nbSupply; i++) { x[i] = IloNumVarArray(env, nbDemand, 0.0, IloInfinity, ILOFLOAT); y[i] = IloNumVarArray(env, nbDemand, 0.0, IloInfinity, ILOFLOAT); } for(i = 0; i < nbSupply; i++) { // supply must meet demand model.add(IloSum(x[i]) == supply[i]); } for(j = 0; j < nbDemand; j++) { // demand must meet supply IloExpr v(env); for(i = 0; i < nbSupply; i++) v += x[i][j]; model.add(v == demand[j]); v.end(); } if (convex) { for(i = 0; i < nbSupply; i++) { for(j = 0; j < nbDemand; j++) { model.add(y[i][j] == IloPiecewiseLinear(x[i][j], IloNumArray(env, 2, 200.0, 400.0), IloNumArray(env, 3, 30.0, 80.0, 130.0), 0.0, 0.0)); } } } else { for(i = 0; i < nbSupply; i++) { for(j = 0; j < nbDemand; j++) { model.add(y[i][j] == IloPiecewiseLinear(x[i][j], IloNumArray(env, 2, 200.0, 400.0), IloNumArray(env, 3, 120.0, 80.0, 50.0), 0.0, 0.0)); } } } IloExpr obj(env); for(i = 0; i < nbSupply; i++) { obj += IloSum(y[i]); } model.add(IloMinimize(env, obj)); obj.end(); IloCplex cplex(env); cplex.extract(model); cplex.exportModel("transport.lp"); cplex.solve(); env.out() << "Solution status: " << cplex.getStatus() << endl; env.out() << " - Solution: " << endl; for(i = 0; i < nbSupply; i++) { env.out() << " " << i << ": "; for(j = 0; j < nbDemand; j++) { env.out() << cplex.getValue(x[i][j]) << "\t"; } env.out() << endl; } env.out() << " Cost = " << cplex.getObjValue() << endl; } catch (IloException& e) { cerr << "ERROR: " << e.getMessage() << endl; } catch (...) { cerr << "Error" << endl; } env.end(); return 0; } // END main