public ContentValues generateValuesMarket(Market market) { ContentValues values = new ContentValues(); values.put(Market.MarketEntry.COLUMN_NAME, market.getName()); values.put(Market.MarketEntry.COLUMN_LATITUDE, market.getLatitude()); values.put(Market.MarketEntry.COLUMN_LONGITUDE, market.getLongitude()); values.put(Market.MarketEntry.COLUMN_ADDRESS, market.getAddress()); return values; }
public void actionPerformed(ActionEvent e) { // --SEND/GET UPDATE(s) FROM SERVER-- Called whenever timer goes off (every 5 sec.) // ==== SEND ALL OF THE MESSAGES WE HAVE ==== comm.sendOutboxMessages(); // ==== SEND A REQUEST FOR A UNIVERSAL UPDATE ==== comm.sendMessage("REQUEST"); // ==== RECIEVE THE UNIVERSE/ROSTER/MARKET UPDATES ==== ArrayList<String> responses = comm.getAllMessages(); // ==== HERE IS WHERE WE UNPACK THE UPDATES FROM THE SERVER ==== for (int q = 0; q < responses.size(); q++) { String header = responses.get(q).substring(0, 4); if (header.equals("UNIV")) { theUniverse.unpack(responses.get(q)); Debug.msg("Unpacked a Universe"); theDisplay.updateDisplay(); } else if (header.equals("ROST")) { Roster.unpack(responses.get(q)); } else if (header.equals("MARK")) { theMarket.unpack(responses.get(q)); } else if (header.equals("SERV")) { Vector inParsed = ParseUtil.parseStringBySign(responses.get(q), '-'); myPlayerNum = Integer.parseInt((String) inParsed.elementAt(1)); } else Debug.msg("Unknown header recieved in ClientMain.actionPerformed() "); } }
/** Scheduler. Determine what action is called for, and do it. */ public boolean pickAndExecuteAnAction() { if (state == State.rcvdPayment) { for (int i = 0; i < marketBills.size(); i++) { Bill b = marketBills.get(i); if (b.cust == customer) { ProcessPayment(b, amt); UpdateInventory(b); return true; } } } // print("no customers left: "+market.noCustomers()); // print("shiftdone? "+shiftDone); if (shiftDone) { if (market.noCustomers()) { ShiftDone(); shiftDone = false; } return true; } if (state == State.left) { StopWork(); return true; } return false; }
private void goToLine() { synchronized (this) { System.out.println( "Customer " + customerNumber + " made all purchases and going to the line"); Market.addCustomerToLine(this); } }
/** * Take action based on the current sentiment and the inherited traits of the agent. The genes * involved are the following: * * <ol> * <li value="23">gb1 Fraction of its available cash the agent is willing to invest at each * period; * <li value="24">gb2 Minimum threshold for asset; * <li value="25">gb3 Incentive to buy if asset below threshold; * <li value="26">gb4 Parameter for price concession * <li value="26">gb5 Parameter for price concession adaptation * <li value="28">ga1 Fraction of available asset the agent is willing to divest at each period * <li value="29">ga2 Maximum threshold for asset * <li value="30">ga3 Incentive to sell if asset above threshold * <li value="31">ga4 Parameter for price concession * <li value="32">ga5 Parameter for price concession adaptation * </ol> */ @Override public void trade() { updateSentiment(); // Check if there are unfilled orders: boolean firstTimeBid = true; boolean firstTimeAsk = true; Iterator<Order> i = orders.iterator(); while (i.hasNext()) { Order o = i.next(); if (o.sign() == Order.BUY) firstTimeBid = false; else firstTimeAsk = false; cancel(o); } if (!orders.isEmpty()) throw new RuntimeException("Orders not cancelled!"); if (resAsset != 0 || resCash != 0) throw new RuntimeException("resAsset = " + resAsset + ", resCash = " + resCash); if (sentiment > 0.0) { // buy double bidValue = cash * genotype.gene(gb1); if (asset * Market.instrument("XYZ").price() < genotype.gene(gb2) * netAssetValue()) bidValue += cash * (1.0 - genotype.gene(gb1)) * genotype.gene(gb3) * sentiment; if (firstTimeBid) bidConcession = sentiment + genotype.gene(gb4); else bidConcession += genotype.gene(gb5); int price = (int) Math.round(Market.instrument("XYZ").price() * bidConcession); if (price > 1073741824) // 2^30 price = 1073741824; if (price < 1) price = 1; int qty = (int) Math.floor(bidValue / price); if (qty > 0) buy(qty, price); } else if (sentiment < 0.0) { // sell double quantity = asset * genotype.gene(ga1); if (asset * Market.instrument("XYZ").price() > genotype.gene(ga2) * netAssetValue()) // sentiment<0, therefore -= actually becomes a += ! quantity -= asset * (1.0 - genotype.gene(ga1)) * genotype.gene(ga3) * sentiment; if (firstTimeAsk) askConcession = genotype.gene(ga4) - sentiment; else askConcession += genotype.gene(ga5); int price = (int) Math.round(Market.instrument("XYZ").price() / askConcession); int qty = (int) quantity; if (qty > 0) sell(qty, price); } }
/** Method used to see if this curve will accept the bid. */ public Point negotiate(Point p) { Point currentPoint = curve.getPoint(currentIndex++); Point nextPoint = null; if (currentIndex < curve.size()) { nextPoint = curve.getPoint(currentIndex); } double distX = 0, distY = 0; Rectangle tol1 = null, tol2 = null; if (nextPoint != null) { distX = nextPoint.getQuantity() - currentPoint.getQuantity(); distY = nextPoint.getQuantity() - currentPoint.getQuantity(); tol2 = new Rectangle( nextPoint.getQuantity() - distX / 2, nextPoint.getCost() - distY / 2, distX, distY); } tol1 = new Rectangle( currentPoint.getQuantity() - distX / 2, currentPoint.getCost() - distY / 2, distX, distY); if (tol2 != null) { if (Market.equilibrium(p, tol1, tol2)) { return p; } } else { if (Market.equilibrium(p, tol1)) { return p; } } return currentPoint; }
/** * Update the agent's sentiment. The new sentiment is based on an average of the values of the * technical indicators, weighted by the genetically determined degrees to which the agents trusts * them. A kind of mental inertia is accounted for by taking the previous mental state as a term * of the average. */ private void updateSentiment() { double sum = sentiment; double totalWeight = 1.0; for (int i = 1; i <= 22; i++) { double signal = (double) Market.signal(i); sum += signal * genotype.gene(i); totalWeight += genotype.gene(i); } sentiment = sum / totalWeight; if (sentiment < -1.0 || sentiment > 1.0) throw new RuntimeException("sentiment = " + sentiment); }
public void sell() { for (Company x : _market.getCompanies()) { if (x.getRisk() > _confidence || x.numLeft() < 3 || x.getRisk() < _confidence - 15) { if (_stocks.get(x) == 1) { _stocks.remove(x); } else { _stocks.put(x, _stocks.get(x) - 1); } _money += x.getPrice(); x.gain(); break; } else { _happy = true; } } }
public void invest() { _oldmoney = _money; // assuming this goes first. for (Company y : _market.getCompanies()) { if (y.getRisk() < _confidence && y.numLeft() > 0 && y.getPrice() < _money) { if (_stocks.containsKey(y)) { _stocks.put(y, (_stocks.get(y) + 1)); } else { _stocks.put(y, 1); } y.sell(); _money -= y.getPrice(); break; } else { _happy = true; } } }
public ArrayList<Market> getMarkets() { ArrayList<Market> markets = new ArrayList<Market>(); SQLiteDatabase dataBase = dbHelper.getReadableDatabase(); Cursor cursor = dataBase.query(Market.MarketEntry.TABLE_NAME, null, null, null, null, null, null); if (cursor.moveToFirst()) { while (cursor.isAfterLast() == false) { Market market = new Market(); market.setIdMarket(cursor.getInt(Market.MarketEntry.INDEX_ENTRY_ID)); market.setName(cursor.getString(Market.MarketEntry.INDEX_NAME)); market.setLatitude(Double.valueOf(cursor.getString(Market.MarketEntry.INDEX_LATITUDE))); market.setLongitude(Double.valueOf(cursor.getString(Market.MarketEntry.INDEX_LONGITUDE))); market.setAddress(cursor.getString(Market.MarketEntry.INDEX_ADDRESS)); markets.add(market); cursor.moveToNext(); } } return markets; }
public void StopWork() { state = State.idle; person.msgStopWork(wage); market.removeMe(this); }
public Bank getBank(Market market) { return bankByMarketMap.get(market.getOrganizationId()); }
@Override public void goOut() { Market.incrementServedCustomers(); System.out.println("Customer " + customerNumber + " has left the market"); }