public void earnIncome() { // If they didn't empty their inventory, lower price // Model.instance().showNumOfProducers(); String message; if (Model.instance().findProducer(producedCommodity, this) && Model.instance().getTick() > 1) { // cph Decrease price by changeProp to represent unsold inventory expPrice[producedCommodity] /= changeProp; if (producedCommodity == GOOD) { message = "Due to surplus inventory, price of " + Integer.toString(GOOD) + " falls to " + Double.toString(expPrice[producedCommodity]); debug(message, PRICE); } } /*if((producedCommodity != 5) && (Model.instance().getNumProducers(producedCommodity)>15)){ producedCommodity = 5; }*/ if (Model.instance().generateSwitch() < Model.SWITCH_PROZ && Model.instance().getTick() > 1) { // Remove from current production arrayList int com = 0; double max = 0; for (int i = 0; i < Commodity.NUM_COMM; i++) { if (expPrice[i] > max) { com = i; max = expPrice[i]; } } Model.instance().removeFromProducers(producedCommodity, this); producedCommodity = com; } if (Model.instance().findProducer(producedCommodity, this)) { } else { // Add to new production arrayList Model.instance().addToProducers(producedCommodity, this); } if (producedCommodity == 0) { // System.out.println("I produce A!"); } else { // System.out.println("I produce something else!"); } // if(age > 3 && producedCommodity == 1){ // }else{ commoditiesHeld[producedCommodity] += amountProduced; // Units? Commodity.getCommNum(producedCommodity).produce(amountProduced); totalMoney += money; makeBudget(); // } }
private void selectProducer(Human seller, int good) { double price = seller.expPrice[good]; double quantity = budgetExp[good] / price; double otherQuantity = seller.budgetExp[good] / price; // seller.chokeQuant[good]; String message; /*double quantity = chokeQuant[good]; double otherQuantity = seller.chokeQuant[good]; //How much is the buyer willing to buy at the price for(int i=0; i<Commodity.NUM_COMM; i++){ if(i!=good){ quantity -= demandSlope[i]*expPrice[i]; }else{ quantity -= demandSlope[good]*price; } }*/ /*for(int k=0; k<Commodity.NUM_COMM; k++){ otherQuantity -= seller.demandSlope[k]*seller.expPrice[k]; }*/ double firstQuantity = quantity; if (otherQuantity > (seller.commoditiesHeld[good] - quantity)) { quantity = (seller.commoditiesHeld[good] - quantity); // Remove seller from producer arrayList // cph Seller sold inventory, raises price to reflect scarcity of his good seller.expPrice[good] *= changeProp; if (good == GOOD) { message = "Producer of good " + Integer.toString(GOOD) + " ran out, price rises to " + Double.toString(seller.expPrice[good]) + " wanted to hold quantity " + Double.toString(quantity) + " not " + Double.toString(firstQuantity); debug(message, PRICE); } Model.instance().removeFromProducers(seller.producedCommodity, seller); debug("removing", false); } double diff = price - expPrice[good]; diff /= 10; double tempUnDiff = expPrice[good]; // expPrice[good]+=diff; if (good == 2) { message = "2 price changed from " + Double.toString(tempUnDiff) + " to " + Double.toString(expPrice[good]); debug(message, TRADE); message = "2 price changed by " + Double.toString(diff); debug(message, TRADE); // System.out.printf("2 price changed from %f to %f\n", tempUnDiff, expPrice[good]); // System.out.printf("2 price changed by %f\n", diff); } // If they bought, raise seller's price if (quantity > 0) { // && money >= price*quantity){ // System.out.printf("We bought %f at %f!\n",quantity, price); seller.commoditiesHeld[good] -= quantity; commoditiesHeld[good] += quantity; budgetExp[good] -= quantity * price; seller.money += quantity * price; money -= quantity * price; Commodity.getCommNum(good).reportSale(quantity); totalSpent += quantity * price; // seller.expPrice[good]*=1.01; } else { // System.out.printf("We didn't buy at %f!\n", price); } }
// Talk to three producers for each good and select one to buy from // Lower the prices of the other two public void checkThreeProducers() { // TODO lots of things REALLY // makeBudget(); String message; // Model.instance().showNumOfProducers(); tradingPartners = new ArrayList<Human>(); ArrayList<Integer> random = new ArrayList<Integer>(); for (int l = 0; l < Commodity.NUM_COMM; l++) { random.add(l); } // Collections.shuffle(random); for (int p = 0; p < Commodity.NUM_COMM; p++) { int i = random.get(p); if (i == GOOD) { buyers++; // System.out.printf("%d agents have tried to buy the good\n", buyers); } int numTrades = 0; // System.out.printf("Human trade budget %f num prod %d numTrades %d\n", budgetExp[i], // Model.instance().getNumProducers(i),numTrades); while (Model.instance().getNumProducers(i) > 0 && budgetExp[i] > 0.01) { // && numTrades < 3){ // System.out.printf("Human trade budget is %f and num prod is %d\n", budgetExp[i], // Model.instance().getNumProducers(i)); numTrades++; tradingPartners.clear(); // System.out.println("Maybe stuck in a loop\n"); // System.out.printf("Budget for good %d is %f\n", i, budgetExp[i]); // int i = p; int numt = 3; int psize = Model.instance().getNumProducers(i); if (i == GOOD) { // System.out.printf("Num producers is %d\n",psize); } if (psize < 3) { numt = psize; } int cheapestProducer = 0; double lowestPrice = 0; double avgPrice = 0; double sumPrice = 0; // System.out.printf("psize is %d\n", psize); for (int k = 0; k < numt; k++) { Human toAdd = Model.instance().getProducerOfGood(i); if (tradingPartners.contains(toAdd)) { // k--; } else { tradingPartners.add(toAdd); } } // System.out.printf("tp size is %d\n",tradingPartners.size()); lowestPrice = tradingPartners.get(0).expPrice[i]; for (int k = 0; k < tradingPartners.size(); k++) { sumPrice += tradingPartners.get(k).expPrice[i]; if (tradingPartners.get(k).expPrice[i] < lowestPrice) { cheapestProducer = k; lowestPrice = tradingPartners.get(k).expPrice[i]; } } avgPrice = sumPrice / tradingPartners.size(); double diff = avgPrice - expPrice[i]; diff /= buyerChange; // cph change buyer's price to reflect prices they believe sellers are offering expPrice[i] += diff; for (int k = 0; k < tradingPartners.size(); k++) { if (k != cheapestProducer) { // tradingPartners.get(k).expPrice[i]*=.99; if (i == 2) { message = "no deal, price falls to " + Double.toString(expPrice[i]); debug(message, TRADE); } } else { selectProducer(tradingPartners.get(k), i); } } } // cph increase buyer's price by changeProp in the event of no producers to reflect scarcity // of the good // expPrice[i]*=changeProp; if (i == GOOD) { message = "no producers, price of good " + Integer.toString(GOOD) + " rises to " + Double.toString(expPrice[i]); // debug(message, PRICE); } } }