-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathobjective_function.java
More file actions
169 lines (156 loc) · 4.7 KB
/
Copy pathobjective_function.java
File metadata and controls
169 lines (156 loc) · 4.7 KB
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
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
import java.io.*;
import java.util.ArrayList;
import java.util.Random;
public class objective_function {
public static void createInput(int m,int n){
//m for j, n for i
Random ra=new Random();
try{
BufferedWriter wr=new BufferedWriter(new FileWriter(new File("input.txt")));
wr.write(m+" "+n);
wr.newLine();
String s="";
ArrayList<Integer> x=new ArrayList<>();
for(int j=0;j<m;j++){
if(ra.nextInt(3)==0){
s=s.concat(1+" ");
x.add(j);
}
else{
s=s.concat(0+" ");
}
}
s=s.substring(0,s.length()-1);
wr.write(s);
wr.newLine();
int xs=x.size();
int count=0;
for(int i=0;i<n;i++){
s="";
boolean set=false;
for(int j=0;j<m;j++){
if(x.contains(j)){
count+=1;
if((!set)&&((ra.nextInt(xs)==0)||(count==xs))) {
s=s.concat(1 + " ");
set=true;
}
else
s=s.concat(0+" ");
}else
s=s.concat(0+" ");
}
s=s.substring(0,s.length()-1);
wr.write(s);
wr.newLine();
wr.flush();
}
s="";
for(int j=0;j<m;j++){
s=s.concat(ra.nextInt(3000)+1+" ");
}
s=s.substring(0,s.length()-1);
wr.write(s);
wr.newLine();
for(int i=0;i<n;i++){
s="";
for(int j=0;j<m;j++){
s=s.concat(ra.nextInt(10)+1+" ");
}
s=s.substring(0,s.length()-1);
wr.write(s);
wr.newLine();
wr.flush();
}
s="";
for(int i=0;i<n;i++){
s=s.concat(ra.nextInt(30)+1+" ");
}
s=s.substring(0,s.length()-1);
wr.write(s);
wr.flush();
wr.close();
} catch (IOException e) {
e.printStackTrace();
}
}
public static void main(String[] args) {
int[] x;//xj m
int[][] y;//yij
int[] f;//fj
int[] h;//hi n
int[][] c;//cij
createInput(20,300);
try {
BufferedReader re = new BufferedReader(new FileReader(new File("input.txt")));
String s = re.readLine();
int[] v= getStringValues(s);
int m=v[0];
int n=v[1];
x=new int[m];
y=new int[n][m];
f=new int[m];
h=new int[n];
c=new int[n][m];
s = re.readLine();
v= getStringValues(s);
for(int j=0;j<m;j++){
x[j]=v[j];
}
for(int i=0;i<n;i++){
s = re.readLine();
v= getStringValues(s);
for(int j=0;j<m;j++){
y[i][j]=v[j];
}
}
s = re.readLine();
v= getStringValues(s);
for(int j=0;j<m;j++){
f[j]=v[j];
}
for(int i=0;i<n;i++){
s = re.readLine();
v= getStringValues(s);
for(int j=0;j<m;j++){
c[i][j]=v[j];
}
}
s = re.readLine();
v= getStringValues(s);
for(int i=0;i<n;i++){
h[i]=v[i];
}
System.out.println(functionValue(x,y,f,h,c));
} catch (FileNotFoundException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
}
}
public static int[] getStringValues(String s){
String[] ss=s.split(" ");
ArrayList<Integer> values=new ArrayList<Integer>();
for(int i=0;i<ss.length;i++){
if(!ss[i].equals(""))
values.add(Integer.valueOf(ss[i]));
}
int[] ans=new int[values.size()];
for(int i=0;i<values.size();i++){
ans[i]=values.get(i);
}
return ans;
}
public static int functionValue(int[] x,int[][] y,int[] f,int[] h,int[][] c){
int ans=0;
for(int j=0;j<x.length;j++){
ans+=x[j]*f[j];
}
for(int i=0;i<h.length;i++){
for(int j=0;j<x.length;j++){
ans+=y[i][j]*c[i][j]*h[i];
}
}
return ans;
}
}