/* * Copyright (C) 2007 pluknet. All rights reserved. * * Redistribution and use in source and binary forms, with or without * modification, are permitted provided that the following conditions * are met: * 1. Redistributions of source code must retain the above copyright * notice, this list of conditions and the following disclaimer. * 2. Redistributions in binary form must reproduce the above copyright * notice, this list of conditions and the following disclaimer in the * documentation and/or other materials provided with the distribution. * * THIS SOFTWARE IS PROVIDED BY AUTHOR AND CONTRIBUTORS ``AS IS'' AND * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE * ARE DISCLAIMED. IN NO EVENT SHALL AUTHOR OR CONTRIBUTORS BE LIABLE * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF * SUCH DAMAGE. */ #include #include #include int main(void) { const int purchase_max = 15; const int r = 10; /* producers */ const int m = 100; /* consumers */ int i, j; int a[r]; /* delta were sold per day */ int b[r]; /* remain product to day.begin */ int c[r]; /* remain product to day.end */ int R[r]; /* delta need to buy */ int l[r]; /* remaining min when need to buy */ const int G = 30; /* period */ int s = 0; /* day of period */ int A = 0; /* cumul. total request to this day */ int H = 0; /* cumul. total neg. response to this day */ srandomdev(); for (i = 0; i < r; ++i) { b[i] = purchase_max * m; R[i] = b[i] * 1 / 2; l[i] = b[i] * 1 / 3; } while (s++ < G) for (i = 0; i < r; ++i) { a[i] = 0; for (j = 0; j < m; ++j) a[i] += random() & purchase_max; A += a[i]; c[i] = b[i] - a[i]; printf("s=%d i=%d | b=%d, a=%d, c=%d\n", s, i, b[i], a[i], c[i]); if (c[i] < 0) { b[i] = R[i]; H += -c[i]; } else if (c[i] < l[i]) b[i] = c[i] + R[i]; else b[i] = c[i]; } printf("H=%d A=%d, H/A=%f\n", H, A, (double)H/A); return(0); }