示例#1
0
        public void Add(SocialSpace sosialspace)
        {
            fileStream.WriteLine("<table>");
            fileStream.WriteLine("<tr><td style='width:20px; height:20px; border:none; background-color:white'></td>");
            for (int c = 0; c < sosialspace.Rows; c++)
            {
                fileStream.WriteLine($"<td class='indexc'>{c}</td>");
            }
            fileStream.WriteLine("</tr>");

            for (int r = 0; r < sosialspace.Rows; r++)
            {
                fileStream.WriteLine("<tr>");
                fileStream.WriteLine($"<td class='indexr'>{r}</td>");
                for (int c = 0; c < sosialspace.Cols; c++)
                {
                    agent = sosialspace[r, c];
                    if (agent == null)
                    {
                        fileStream.WriteLine("<td></td>");
                        continue;
                    }
                    fileStream.WriteLine($"<td class='{(agent.Contrib?"contrib":"noncontrib")}'>" + agent.Id + "</td>");
                }

                fileStream.WriteLine("</tr>");
            }

            fileStream.WriteLine("</table>");
            fileStream.WriteLine("<br />");
        }
示例#2
0
        public void OutputRelations(int iteration, SocialSpace socialspace)
        {
            using (SpreadsheetDocument document = SpreadsheetDocument.Create(FolderName + $"/OutputRelations/Iteration{iteration}.xlsx", SpreadsheetDocumentType.Workbook))
            {
                WorkbookPart workbookPart = document.AddWorkbookPart();
                workbookPart.Workbook = new Workbook();
                WorksheetPart worksheetPart = workbookPart.AddNewPart <WorksheetPart>();

                FileVersion fv = new FileVersion();
                fv.ApplicationName      = "Microsoft Office Excel";
                worksheetPart.Worksheet = new Worksheet(new SheetData());
                WorkbookStylesPart wbsp = workbookPart.AddNewPart <WorkbookStylesPart>();

                wbsp.Stylesheet = new Stylesheet();
                wbsp.Stylesheet.Save();

                Columns lstColumns          = worksheetPart.Worksheet.GetFirstChild <Columns>();
                Boolean needToInsertColumns = false;
                if (lstColumns == null)
                {
                    lstColumns          = new Columns();
                    needToInsertColumns = true;
                }
                lstColumns.Append(new Column()
                {
                    Min = 1, Max = 2, Width = 20, CustomWidth = true
                });

                if (needToInsertColumns)
                {
                    worksheetPart.Worksheet.InsertAt(lstColumns, 0);
                }

                Sheets sheets = workbookPart.Workbook.AppendChild(new Sheets());
                Sheet  sheet  = new Sheet()
                {
                    Id = workbookPart.GetIdOfPart(worksheetPart), SheetId = 1, Name = "OutputRelations"
                };
                sheets.Append(sheet);

                SheetData sheetData = worksheetPart.Worksheet.GetFirstChild <SheetData>();

                Row exRow = new Row();
                sheetData.Append(exRow);

                InsertCell(exRow, "AgentID1", CellValues.String);
                InsertCell(exRow, "AgentID2", CellValues.String);

                List <(String, String)> relations = new List <(String, String)>();
                for (int row = 0; row < socialspace.Rows; row++)
                {
                    for (int col = 0; col < socialspace.Cols; col++)
                    {
                        if (socialspace[row, col] == null)
                        {
                            continue;
                        }

                        int rowNeighbourSpot, colNeighbourSpot;

                        for (int r = row - 1; r <= row + 1; r++)
                        {
                            rowNeighbourSpot = r;

                            // if we go beyond the bounds of the array, take a spot on the other side
                            if (rowNeighbourSpot < 0)
                            {
                                rowNeighbourSpot = socialspace.Rows - 1;
                            }
                            if (rowNeighbourSpot >= socialspace.Rows)
                            {
                                rowNeighbourSpot = 0;
                            }

                            for (int c = col - 1; c <= col + 1; c++)
                            {
                                colNeighbourSpot = c;

                                // if we go beyond the bounds of the array, take a spot on the other side
                                if (colNeighbourSpot < 0)
                                {
                                    colNeighbourSpot = socialspace.Cols - 1;
                                }
                                if (colNeighbourSpot >= socialspace.Cols)
                                {
                                    colNeighbourSpot = 0;
                                }

                                if (c == col && r == row)
                                {
                                    continue;
                                }

                                if (socialspace[rowNeighbourSpot, colNeighbourSpot] != null)
                                {
                                    relations.Add((socialspace[row, col].Id, socialspace[rowNeighbourSpot, colNeighbourSpot].Id));
                                }
                            }
                        }
                    }
                }

                //IEnumerable<(String, String)> OrderResult = relations.OrderBy(e => Convert.ToInt32(e.Item1));
                IEnumerable <(String, String)> OrderResult = relations.OrderBy(e => e.Item1);
                foreach ((String, String)item in OrderResult)
                {
                    exRow = new Row();
                    sheetData.Append(exRow);
                    InsertCell(exRow, item.Item1, CellValues.String);
                    InsertCell(exRow, item.Item2, CellValues.String);
                }

                workbookPart.Workbook.Save();
                document.Close();
            }
        }