-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmain.go
122 lines (107 loc) · 4.75 KB
/
main.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
package main
import (
"github.com/yarntime/data_generator/mkp"
"fmt"
"os"
"bufio"
)
var normalInstances []mkp.InstanceType
var difficultInstances = []string{"UncorrelatedSpannerInstances", "WeaklyCorrelatedSpannerInstances", "StronglyCorrelatedSpannerInstances",
"MultipleStronglyCorrelatedInstances", "ProfitCeilingInstances", "CircleInstances"}
var normalBags []mkp.BagType
var R = []int{1000, 10000}
var NMPair = [][]int{{2000, 200}, {2000, 400}, {2000, 800}, {4000, 200}, {4000, 400}, {4000, 800}, {8000, 200}, {8000, 400}, {8000, 800}}
var BASE_DIR = "./generated_data/"
func init() {
normalInstances = make([]mkp.InstanceType, 0)
normalInstances = append(normalInstances, &mkp.UncorrelatedInstances{})
normalInstances = append(normalInstances, &mkp.WeaklyCorrelatedInstances{})
normalInstances = append(normalInstances, &mkp.StronglyCorrelatedInstances{})
normalInstances = append(normalInstances, &mkp.InverseStronglyCorrelatedInstances{})
normalInstances = append(normalInstances, &mkp.AlmostSctronglyCorrelatedInstances{})
normalInstances = append(normalInstances, &mkp.SubsetSumInstances{})
normalInstances = append(normalInstances, &mkp.UncorrelatedInstancesWithSimilarWeights{})
normalBags = make([]mkp.BagType, 0)
normalBags = append(normalBags, &mkp.SimilarBag{})
normalBags = append(normalBags, &mkp.DiSimilarBag{})
}
func main() {
for i := 0; i < len(R); i++ {
for j := 0; j < len(NMPair); j++ {
for k := 0; k < len(normalInstances); k++ {
weights, profits := normalInstances[k].GenerateInstance(R[i], NMPair[j][0])
similarCapacities := normalBags[0].GenerateCapacity(weights, profits, NMPair[j][1])
path, name := getFileName(R[i], NMPair[j][0], NMPair[j][1], normalInstances[k].GetName(), normalBags[0].GetName())
writeToFile(path, name, weights, profits, similarCapacities)
dissimilarCapacities := normalBags[1].GenerateCapacity(weights, profits, NMPair[j][1])
path, name = getFileName(R[i], NMPair[j][0], NMPair[j][1], normalInstances[k].GetName(), normalBags[1].GetName())
writeToFile(path, name, weights, profits, dissimilarCapacities)
}
for k := 0; k < len(difficultInstances); k++ {
weights, profits := getDifficultInstances(R[i], NMPair[j][0], NMPair[j][1], difficultInstances[k])
similarCapacities := normalBags[0].GenerateCapacity(weights, profits, NMPair[j][1])
path, name := getFileName(R[i], NMPair[j][0], NMPair[j][1], difficultInstances[k], normalBags[0].GetName())
writeToFile(path, name, weights, profits, similarCapacities)
dissimilarCapacities := normalBags[1].GenerateCapacity(weights, profits, NMPair[j][1])
path, name = getFileName(R[i], NMPair[j][0], NMPair[j][1], difficultInstances[k], normalBags[1].GetName())
writeToFile(path, name, weights, profits, dissimilarCapacities)
}
}
}
}
func getDifficultInstances(r int, n int, m int, dateType string) ([]int, []int) {
var weights []int
var profits []int
switch dateType {
case "UncorrelatedSpannerInstances":
instanceType := &mkp.UncorrelatedSpannerInstances{}
weights, profits = instanceType.GenerateInstance(r, n, 2, 10)
case "WeaklyCorrelatedSpannerInstances":
instanceType := &mkp.WeaklyCorrelatedSpannerInstances{}
weights, profits = instanceType.GenerateInstance(r, n, 2, 10)
case "StronglyCorrelatedSpannerInstances":
instanceType := &mkp.WeaklyCorrelatedSpannerInstances{}
weights, profits = instanceType.GenerateInstance(r, n, 2, 10)
case "MultipleStronglyCorrelatedInstances":
instanceType := &mkp.MultipleStronglyCorrelatedInstances{}
weights, profits = instanceType.GenerateInstance(r, n, 3 * r / 10, 2 * r / 10, 6)
case "ProfitCeilingInstances":
instanceType := &mkp.ProfitCeilingInstances{}
weights, profits = instanceType.GenerateInstance(r, n, 3)
case "CircleInstances":
instanceType := &mkp.CircleInstances{}
weights, profits = instanceType.GenerateInstance(r, n, 2.0 / 3.0)
}
return weights, profits
}
func getFileName(r int, n int, m int, instanceType string, bagType string) (string, string) {
return fmt.Sprintf("%s/%s/", BASE_DIR, instanceType), fmt.Sprintf("%s_%d_%d_%d.txt", bagType, r, n, m)
}
func writeToFile(path string, name string, weights []int, profits []int, capacities []int) {
if b, _ := PathExists(path); !b {
os.MkdirAll(path, os.ModePerm)
}
f, err := os.OpenFile(path + "/" + name, os.O_CREATE, 0777)
if err != nil {
fmt.Printf("%s\n", err.Error())
}
defer f.Close()
w := bufio.NewWriter(f)
for i := 0; i < len(weights); i++ {
w.WriteString(fmt.Sprintf("%d,%d\n", weights[i], profits[i]))
}
for i := 0; i < len(capacities); i++ {
w.WriteString(fmt.Sprintf("%d\n", capacities[i]))
}
w.Flush()
}
func PathExists(path string) (bool, error) {
_, err := os.Stat(path)
if err == nil {
return true, nil
}
if os.IsNotExist(err) {
return false, nil
}
return false, err
}