Speed up development with full-stack environments for every branch.

Learn More

Hypercapitalism Sample Economy [Node.js and Hello World]

Forked from Hello World for Node.js.

64 Runs 167 Views 1 Copies

Run a sample economy using Hypercapitalsim. Read more at http://hypercapital.info

Saved

Saved

afat 1

afat
published 2 years ago

    (function() {
      var Capital, Wallet, atest, currentNameIndex, fixTest, http, pl, randomWalk, runSampleClosedEconomy, runSampleClosedEconomyWithWageGlutton, runSampleClosedEconomyWithWageTemprance, runSampleClosedEconomyWithWageTempranceAndTax, runSampleClosedEconomyWithWageTempranceAndTaxMeansOfRedistribute, simplest, _;
    
      http = require("http");
    
      Wallet = require("./Wallet.js");
    
      Capital = require("./Capital.js");
    
      _ = require("underscore");
    
      http.createServer(function(req, res) {
        console.log('starting up');
    
        /*
        Adjust test items here
         */
    
        /*ERScale is the amount we multiply the Economic Rent Distribution by. Increase the number to make good capitalists great. */
        var AGDP, APoverty, ARent, CummulativeGDPChange, CummulativePovertyChange, CummulativeRentChange, DemurrageTest, DistFunction, ERScale, ERVariance, Entities, FirstGDP, FirstPoverty, FirstRent, GDPChange, LastGDP, LastPoverty, LastPovertyMonths, LastRent, Months, PayFunction, PovertyChange, RentChange, StandardMonthlyCost, StartingCash, TestEachLevel, TotalGDP, TotalPovertyMonths, TotalRent, UseReason, demurrage, i, message, output, result, _i, _j, _len;
        ERScale = Math.floor(Math.random() * (10 - 1)) + 1;
    
        /*ERVariance is used to determine the standard deviation  of our Economic Rent Distribution.  Increase it it to make great capitalists very unique and very dominant */
        ERVariance = Math.floor(Math.random() * (28 - 1)) + 1;
    
        /*DemurrageTest are the demurrage levels to test */
        DemurrageTest = [0, 0.1, 0.2, 0.3, 0.4, 0.5, 0.6];
    
        /*number of months to test */
        Months = 300;
    
        /*number of Entities */
        Entities = 10;
    
        /*StartingCash is the Amount of cash to give each node */
        StartingCash = Math.floor(Math.random() * (100000 - 0)) + 0;
    
        /*StandardMonthlyCost is the Amount each person needs to spend on basic necessities. */
        StandardMonthlyCost = Math.floor(Math.random() * ((StartingCash / 12) - 0)) + 0;
    
        /*The number of times you want to run each simulation at each demurrage level */
        TestEachLevel = 5;
    
        /* UseReason will determine if the model attempts to use reason to find nodes with better future value.  You can set this to false to simulate non-hypercapitalistic redistribution of wealth */
        UseReason = true;
    
        /* DistFunction is the distribution model to use.  change to "even" if you want to test out non-hypercapitalistic distribution */
        DistFunction = "hypercapital";
    
        /* this is the function used by the wallets to distribute cash.  Change to "altpayToWallet" if you don't want prefs to be traced and want to simulate non-hypercapitalistic redistribution of wealth */
        PayFunction = "payToWallet";
    
        /*
        set true == true these to run the standard test
         */
        if (true === false) {
          ERScale = 4;
          ERVariance = 6;
          DemurrageTest = [0, 0.1, 0.2, 0.3, 0.4, 0.5, 0.6];
          Months = 300;
          Entities = 10;
          StartingCash = 1000;
          StandardMonthlyCost = 50;
          TestEachLevel = 5;
          UseReason = true;
          DistFunction = "hypercapital";
          PayFunction = "payToWallet";
        }
    
        /*
        Hesitate to change anything below
         */
        message = "";
        message = message + "Starting Cash:" + StartingCash + "\n";
        message = message + "MonthlyCost:" + StandardMonthlyCost + "\n";
        message = message + "ERScale:" + ERScale + "\n";
        message = message + "ERVariance:" + ERVariance + "\n";
        global.retainedPercentage = 1;
        output = "";
        message = message + "Demurrage,GDP,Rent,PovertyMonths,ChangeGDP,ChangeRent,ChangePoverty,%GDP,Total%GDP,%Rent,Total%Rent,%Poverty,Total%Poverty" + "\n";
        LastGDP = 0;
        LastRent = 0;
        LastPovertyMonths = 0;
        CummulativeGDPChange = 0;
        CummulativeRentChange = 0;
        CummulativePovertyChange = 0;
        FirstGDP = 0;
        FirstRent = 0;
        FirstPoverty = 0;
        for (_i = 0, _len = DemurrageTest.length; _i < _len; _i++) {
          demurrage = DemurrageTest[_i];
          console.log(demurrage);
          TotalGDP = 0;
          TotalRent = 0;
          TotalPovertyMonths = 0;
          GDPChange = 0;
          RentChange = 0;
          PovertyChange = 0;
          for (i = _j = 1; 1 <= TestEachLevel ? _j <= TestEachLevel : _j >= TestEachLevel; i = 1 <= TestEachLevel ? ++_j : --_j) {
            console.log('test' + i);
            result = randomWalk({
              entities: Entities,
              months: Months,
              demurrage: demurrage,
              retaining: 1,
              tax: 0,
              ERScale: ERScale,
              ERVariance: ERVariance,
              StartingCash: StartingCash,
              StandardMonthly: StandardMonthlyCost,
              PayFunction: PayFunction,
              DistFunction: DistFunction,
              UseReason: UseReason
            });
            TotalGDP = result.GDP + TotalGDP;
            TotalRent = result.Rent + TotalRent;
            TotalPovertyMonths = result.MissedOpps + TotalPovertyMonths;
            output = output + result.output;
          }
          AGDP = LastGDP;
          ARent = LastRent;
          APoverty = LastPoverty;
          LastGDP = Math.round(TotalGDP / 5);
          LastRent = Math.round(TotalRent / 5);
          LastPoverty = Math.round(TotalPovertyMonths / 5);
          if (AGDP !== 0) {
            GDPChange = (LastGDP - AGDP) / AGDP;
            RentChange = (LastRent - ARent) / ARent;
            PovertyChange = (LastPoverty - APoverty) / APoverty;
            CummulativeGDPChange = (LastGDP - FirstGDP) / FirstGDP;
            CummulativeRentChange = (LastRent - FirstRent) / FirstRent;
            CummulativePovertyChange = (LastPoverty - FirstPoverty) / FirstPoverty;
          } else {
            FirstGDP = LastGDP;
            FirstRent = LastRent;
            FirstPoverty = LastPoverty;
          }
          message = message + demurrage + "," + LastGDP + ',' + LastRent + ',' + LastPoverty + ',' + (LastGDP - AGDP) + ',' + (LastRent - ARent) + ',' + (LastPoverty - APoverty) + ',' + GDPChange.toFixed(3) + ',' + CummulativeGDPChange.toFixed(3) + "," + RentChange.toFixed(3) + ',' + CummulativeRentChange.toFixed(3) + "," + PovertyChange.toFixed(3) + ',' + CummulativePovertyChange.toFixed(3) + "\n";
        }
        output = message + output;
        res.writeHead(200, {
          "Content-Type": "text/plain"
        });
        res.end(output);
      }).listen(80);
    
      console.log("Server listening on port 5557");
    
      currentNameIndex = 0;
    
      pl = (function(_this) {
        return function(line) {
          return _this.output = _this.output + line + "\n";
        };
      })(this);
    
      this.output = "";
    
      randomWalk = (function(_this) {
        return function(data) {
          var GDP, MissedGDP, MissedOpps, RentCollected, balances, currentTick, distribution, endDemurrage, even, gaussian, i, money_supply, nodes, orderedNodes, startDemurrage, thisNode, total, x, _i, _j, _ref, _ref1;
          _this.output = "";
          if (data.ERScale == null) {
            data.ERScale = 4;
          }
          if (data.entities == null) {
            data.entities = 10;
          }
          if (data.months == null) {
            data.months = 10;
          }
          if (data.demurrage != null) {
            global.demurragePerYear = data.demurrage;
          }
          if (typeof dataretained !== "undefined" && dataretained !== null) {
            global.retainedPercentage = data.retained;
          }
          endDemurrage = (new Date()).getTime();
          startDemurrage = endDemurrage - (global.ticksPerDay * 365 * 30);
          GDP = 0;
          MissedGDP = 0;
          RentCollected = 0;
          MissedOpps = 0;
          nodes = [];
          gaussian = require('gaussian');
          distribution = gaussian(data.entities / 2, (data.entities / data.ERVariance) * (data.entities / data.ERVariance));
          for (x = _i = 0, _ref = data.entities; 0 <= _ref ? _i < _ref : _i > _ref; x = 0 <= _ref ? ++_i : --_i) {
            console.log(x);
            thisNode = new Wallet.Wallet({
              name: "n" + x,
              lastDemurrage: startDemurrage,
              balance: data.StartingCash
            });
            thisNode.pER = distribution.pdf(x) * data.ERScale;
            thisNode.pReason = x / data.entities;
            nodes.push(thisNode);
          }
          orderedNodes = _.sortBy(nodes, function(item) {
            return item.pER;
          });
          orderedNodes = orderedNodes.reverse();
          pl("Name,Rent,Reason");
          nodes.map(function(o) {
            return pl(o.name + "," + o.pER.toFixed(2) + ',' + o.pReason.toFixed(2));
          });
          pl('Demmurage:' + data.demurrage);
          pl('Month,GDP,MissedGDP,MissedOps,Rent,MoneySupply');
          balances = "month";
          _.map(nodes, function(o) {
            return balances = balances + ',' + o.name;
          });
          balances = balances + "\n";
          for (i = _j = 1, _ref1 = data.months; 1 <= _ref1 ? _j <= _ref1 : _j >= _ref1; i = 1 <= _ref1 ? ++_j : --_j) {
            currentTick = startDemurrage + (i * global.ticksPerDay * 30);
            if (data.DistFunction === 'even') {
              total = 0;
              _.map(nodes, function(o) {
                var tax;
                tax = o.balance * data.demurrage / 12;
                o.balance = o.balance - tax;
                return total = total + tax;
              });
              even = total / data.entities;
              _.map(nodes, function(o) {
                return o.balance = o.balance + even;
              });
            } else {
              _.map(nodes, function(o) {
                return o.makeCurrent(currentTick);
              });
            }
            _.map(nodes, function(aNode) {
              var amount, disposableCash, foundTarget, possibleNode, targetNode, test, with_rent, _k, _len;
              foundTarget = null;
              if (data.UseReason) {
                for (_k = 0, _len = orderedNodes.length; _k < _len; _k++) {
                  possibleNode = orderedNodes[_k];
                  if (foundTarget === null && possibleNode.name !== aNode.name) {
                    test = Math.random();
                    if (test < aNode.pReason) {
                      foundTarget = possibleNode;
                    }
                  }
                }
                if (foundTarget === null) {
                  targetNode = Math.floor(Math.random() * data.entities);
                  foundTarget = nodes[targetNode];
                }
              } else {
                targetNode = Math.floor(Math.random() * data.entities);
                foundTarget = nodes[targetNode];
              }
              if (foundTarget.name !== aNode.name) {
                disposableCash = aNode.balance - (data.StandardMonthly * 12);
                amount = (((Math.floor(Math.random() * 50) + 25) / 100) * disposableCash / 12) + data.StandardMonthly;
                with_rent = amount * (1 + foundTarget.pER);
                if (with_rent < aNode.balance) {
                  aNode[data.PayFunction](foundTarget, with_rent, currentTick + 1);
                  GDP = GDP + with_rent;
                  return RentCollected = RentCollected + (with_rent - amount);
                } else {
                  console.log(aNode.name + ' couldnt pay in month ' + i);
                  MissedOpps = MissedOpps + 1;
                  return MissedGDP = MissedGDP + with_rent;
                }
              }
            });
            money_supply = 0;
            _.map(nodes, function(aNode) {
              return money_supply = money_supply + aNode.balance;
            });
    
            /*
            if money_supply < (data.entities * data.StartingCash) - 2
              missing = (data.entities * data.StartingCash) - money_supply - 2
              adding = missing / data.entities
              _.map nodes,(aNode) =>
                aNode.balance = aNode.balance + adding
              money_supply = 0
              _.map nodes,(aNode) =>
                money_supply = money_supply + aNode.balance
             */
            _.map(nodes, function(o) {
              return o.makeCurrent(currentTick);
            });
            pl(i + ',' + Math.round(GDP) + ',' + Math.round(MissedGDP) + ',' + MissedOpps + ',' + Math.round(RentCollected) + ',' + Math.round(money_supply));
            balances = balances + i;
            _.map(nodes, function(o) {
              return balances = balances + ',' + Math.round(o.balance);
            });
            balances = balances + "\n";
          }
          _this.output = _this.output + balances;
          return {
            output: _this.output,
            GDP: GDP,
            MissedGDP: MissedGDP,
            MissedOpps: MissedOpps,
            Rent: RentCollected
          };
        };
      })(this);
    
    
    }).call(this);
    
    //# sourceMappingURL=item.js.map
    
    Please login/signup to get access to the terminal.

    Your session has timed out.

    Dismiss (the page may not function properly).